andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import ch.ethz.ssh2.Connection; |
| 20 | import ch.ethz.ssh2.Session; |
| 21 | import com.google.common.base.Preconditions; |
| 22 | import org.onosproject.netconf.NetconfDeviceInfo; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 23 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 24 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 25 | import org.onosproject.netconf.NetconfException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 26 | import org.onosproject.netconf.NetconfSession; |
| 27 | import org.slf4j.Logger; |
| 28 | import org.slf4j.LoggerFactory; |
| 29 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 30 | import java.io.IOException; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 31 | import java.util.ArrayList; |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 32 | import java.util.Collections; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 33 | import java.util.HashMap; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 34 | import java.util.List; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 35 | import java.util.Map; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 36 | import java.util.Optional; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 37 | import java.util.concurrent.CompletableFuture; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 38 | import java.util.concurrent.ExecutionException; |
| 39 | import java.util.concurrent.TimeUnit; |
| 40 | import java.util.concurrent.TimeoutException; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 41 | import java.util.concurrent.atomic.AtomicInteger; |
| 42 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Implementation of a NETCONF session to talk to a device. |
| 46 | */ |
| 47 | public class NetconfSessionImpl implements NetconfSession { |
| 48 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 49 | private static final Logger log = LoggerFactory |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 50 | .getLogger(NetconfSessionImpl.class); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 51 | |
| 52 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 53 | private static final int CONNECTION_TIMEOUT = 0; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 54 | private static final String ENDPATTERN = "]]>]]>"; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 55 | private static final String MESSAGE_ID_STRING = "message-id"; |
| 56 | private static final String HELLO = "hello"; |
| 57 | private static final String NEW_LINE = "\n"; |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 58 | private static final int FUTURE_REPLY_TIMEOUT = 5000; |
| 59 | private static final String ERROR = "ERROR "; |
| 60 | private static final String END_OF_RPC_OPEN_TAG = "\">"; |
| 61 | private static final String EQUAL = "="; |
| 62 | private static final String NUMBER_BETWEEN_QUOTES_MATCHER = "\"+([0-9]+)+\""; |
| 63 | private static final String XML_HEADER = |
| 64 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 65 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 66 | private final AtomicInteger messageIdInteger = new AtomicInteger(0); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 67 | private Connection netconfConnection; |
| 68 | private NetconfDeviceInfo deviceInfo; |
| 69 | private Session sshSession; |
| 70 | private boolean connectionActive; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 71 | private List<String> deviceCapabilities = |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 72 | Collections.singletonList("urn:ietf:params:netconf:base:1.0"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 73 | private String serverCapabilities; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 74 | private NetconfStreamHandler t; |
| 75 | private Map<Integer, CompletableFuture<String>> replies; |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 76 | private List<String> errorReplies; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 77 | |
| 78 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 79 | public NetconfSessionImpl(NetconfDeviceInfo deviceInfo) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 80 | this.deviceInfo = deviceInfo; |
| 81 | connectionActive = false; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 82 | replies = new HashMap<>(); |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 83 | errorReplies = new ArrayList<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 84 | startConnection(); |
| 85 | } |
| 86 | |
| 87 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 88 | private void startConnection() throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 89 | if (!connectionActive) { |
| 90 | netconfConnection = new Connection(deviceInfo.ip().toString(), deviceInfo.port()); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 91 | try { |
| 92 | netconfConnection.connect(null, CONNECTION_TIMEOUT, 5000); |
| 93 | } catch (IOException e) { |
| 94 | throw new NetconfException("Cannot open a connection with device" + deviceInfo, e); |
| 95 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 96 | boolean isAuthenticated; |
| 97 | try { |
| 98 | if (deviceInfo.getKeyFile() != null) { |
| 99 | isAuthenticated = netconfConnection.authenticateWithPublicKey( |
| 100 | deviceInfo.name(), deviceInfo.getKeyFile(), |
| 101 | deviceInfo.password()); |
| 102 | } else { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 103 | log.debug("Authenticating to device {} with username {}", |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 104 | deviceInfo.getDeviceId(), deviceInfo.name()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 105 | isAuthenticated = netconfConnection.authenticateWithPassword( |
| 106 | deviceInfo.name(), deviceInfo.password()); |
| 107 | } |
| 108 | } catch (IOException e) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 109 | log.error("Authentication connection to device " + |
| 110 | deviceInfo.getDeviceId() + " failed:" + |
| 111 | e.getMessage()); |
| 112 | throw new NetconfException("Authentication connection to device " + |
| 113 | deviceInfo.getDeviceId() + " failed", e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | connectionActive = true; |
| 117 | Preconditions.checkArgument(isAuthenticated, |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 118 | "Authentication to device {} with username " + |
| 119 | "{} Failed", |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 120 | deviceInfo.getDeviceId(), deviceInfo.name()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 121 | startSshSession(); |
| 122 | } |
| 123 | } |
| 124 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 125 | private void startSshSession() throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 126 | try { |
| 127 | sshSession = netconfConnection.openSession(); |
| 128 | sshSession.startSubSystem("netconf"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 129 | t = new NetconfStreamThread(sshSession.getStdout(), sshSession.getStdin(), |
| 130 | sshSession.getStderr(), deviceInfo, |
| 131 | new NetconfSessionDelegateImpl()); |
| 132 | this.addDeviceOutputListener(new NetconfDeviceOutputEventListenerImpl(deviceInfo)); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 133 | sendHello(); |
| 134 | } catch (IOException e) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 135 | log.error("Failed to create ch.ethz.ssh2.Session session:" + |
| 136 | e.getMessage()); |
| 137 | throw new NetconfException("Failed to create ch.ethz.ssh2.Session session with device" + |
| 138 | deviceInfo, e); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 142 | private void sendHello() throws NetconfException { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 143 | serverCapabilities = sendRequest(createHelloString()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private String createHelloString() { |
| 147 | StringBuilder hellobuffer = new StringBuilder(); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 148 | hellobuffer.append(XML_HEADER); |
| 149 | hellobuffer.append("\n"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 150 | hellobuffer.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
| 151 | hellobuffer.append(" <capabilities>\n"); |
| 152 | deviceCapabilities.forEach( |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 153 | cap -> hellobuffer.append(" <capability>") |
| 154 | .append(cap) |
| 155 | .append("</capability>\n")); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 156 | hellobuffer.append(" </capabilities>\n"); |
| 157 | hellobuffer.append("</hello>\n"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 158 | hellobuffer.append(ENDPATTERN); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 159 | return hellobuffer.toString(); |
| 160 | |
| 161 | } |
| 162 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 163 | private void checkAndRestablishSession() throws NetconfException { |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 164 | if (sshSession.getState() != 2) { |
| 165 | try { |
| 166 | startSshSession(); |
| 167 | } catch (IOException e) { |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 168 | log.debug("The connection with {} had to be reopened", deviceInfo.getDeviceId()); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 169 | try { |
| 170 | startConnection(); |
| 171 | } catch (IOException e2) { |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 172 | log.error("No connection {} for device", netconfConnection, e2); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 173 | throw new NetconfException("Cannot re-open the connection with device" + deviceInfo, e); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 179 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 180 | public String requestSync(String request) throws NetconfException { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 181 | if (!request.contains(ENDPATTERN)) { |
| 182 | request = request + NEW_LINE + ENDPATTERN; |
| 183 | } |
| 184 | String reply = sendRequest(request); |
| 185 | return checkReply(reply) ? reply : ERROR + reply; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 189 | public CompletableFuture<String> request(String request) { |
| 190 | CompletableFuture<String> ftrep = t.sendMessage(request); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 191 | replies.put(messageIdInteger.get(), ftrep); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 192 | return ftrep; |
| 193 | } |
| 194 | |
| 195 | private String sendRequest(String request) throws NetconfException { |
| 196 | checkAndRestablishSession(); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 197 | request = formatRequestMessageId(request); |
| 198 | request = formatXmlHeader(request); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 199 | CompletableFuture<String> futureReply = request(request); |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 200 | messageIdInteger.incrementAndGet(); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 201 | String rp; |
| 202 | try { |
| 203 | rp = futureReply.get(FUTURE_REPLY_TIMEOUT, TimeUnit.MILLISECONDS); |
| 204 | } catch (InterruptedException | ExecutionException | TimeoutException e) { |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 205 | throw new NetconfException("No matching reply for request " + request, e); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 206 | } |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 207 | log.debug("Result {} from request {} to device {}", rp, request, deviceInfo); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 208 | return rp.trim(); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 211 | private String formatRequestMessageId(String request) { |
| 212 | if (request.contains(MESSAGE_ID_STRING)) { |
| 213 | //FIXME if application provieds his own counting of messages this fails that count |
| 214 | request = request.replaceFirst(MESSAGE_ID_STRING + EQUAL + NUMBER_BETWEEN_QUOTES_MATCHER, |
| 215 | MESSAGE_ID_STRING + EQUAL + "\"" + messageIdInteger.get() + "\""); |
| 216 | } else if (!request.contains(MESSAGE_ID_STRING) && !request.contains(HELLO)) { |
| 217 | //FIXME find out a better way to enforce the presence of message-id |
| 218 | request = request.replaceFirst(END_OF_RPC_OPEN_TAG, "\" " + MESSAGE_ID_STRING + EQUAL + "\"" |
| 219 | + messageIdInteger.get() + "\"" + ">"); |
| 220 | } |
| 221 | return request; |
| 222 | } |
| 223 | |
| 224 | private String formatXmlHeader(String request) { |
| 225 | if (!request.contains(XML_HEADER)) { |
| 226 | //FIXME if application provieds his own XML header of different type there is a clash |
| 227 | request = XML_HEADER + "\n" + request; |
| 228 | } |
| 229 | return request; |
| 230 | } |
| 231 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 232 | @Override |
| 233 | public String get(String request) throws NetconfException { |
| 234 | return requestSync(request); |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public String getConfig(String targetConfiguration) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 239 | return getConfig(targetConfiguration, null); |
| 240 | } |
| 241 | |
| 242 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 243 | public String getConfig(String targetConfiguration, String configurationSchema) throws NetconfException { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 244 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
| 245 | rpc.append("<rpc "); |
| 246 | rpc.append(MESSAGE_ID_STRING); |
| 247 | rpc.append(EQUAL); |
| 248 | rpc.append("\""); |
| 249 | rpc.append(messageIdInteger.get()); |
| 250 | rpc.append("\" "); |
| 251 | rpc.append("xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 252 | rpc.append("<get-config>\n"); |
| 253 | rpc.append("<source>\n"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 254 | rpc.append("<").append(targetConfiguration).append("/>"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 255 | rpc.append("</source>"); |
| 256 | if (configurationSchema != null) { |
| 257 | rpc.append("<filter type=\"subtree\">\n"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 258 | rpc.append(configurationSchema).append("\n"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 259 | rpc.append("</filter>\n"); |
| 260 | } |
| 261 | rpc.append("</get-config>\n"); |
| 262 | rpc.append("</rpc>\n"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 263 | rpc.append(ENDPATTERN); |
| 264 | String reply = sendRequest(rpc.toString()); |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 265 | return checkReply(reply) ? reply : "ERROR " + reply; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 269 | public boolean editConfig(String newConfiguration) throws NetconfException { |
| 270 | newConfiguration = newConfiguration + ENDPATTERN; |
| 271 | return checkReply(sendRequest(newConfiguration)); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | @Override |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 275 | public boolean editConfig(String targetConfiguration, String mode, String newConfiguration) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 276 | throws NetconfException { |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 277 | newConfiguration = newConfiguration.trim(); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 278 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
| 279 | rpc.append("<rpc "); |
| 280 | rpc.append(MESSAGE_ID_STRING); |
| 281 | rpc.append(EQUAL); |
| 282 | rpc.append("\""); |
| 283 | rpc.append(messageIdInteger.get()); |
| 284 | rpc.append("\" "); |
| 285 | rpc.append("xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 286 | rpc.append("<edit-config>\n"); |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 287 | rpc.append("<target>"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 288 | rpc.append("<").append(targetConfiguration).append("/>"); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 289 | rpc.append("</target>\n"); |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 290 | rpc.append("<default-operation>"); |
| 291 | rpc.append(mode); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 292 | rpc.append("</default-operation>\n"); |
| 293 | rpc.append("<config>\n"); |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 294 | rpc.append(newConfiguration); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 295 | rpc.append("</config>\n"); |
| 296 | rpc.append("</edit-config>\n"); |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 297 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 298 | rpc.append(ENDPATTERN); |
Andrea Campanella | 50d2521 | 2016-02-26 13:06:23 -0800 | [diff] [blame] | 299 | log.info(rpc.toString()); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 300 | return checkReply(sendRequest(rpc.toString())); |
Andrea Campanella | f4fd035 | 2015-12-14 17:03:05 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | @Override |
Andrea Campanella | 1cd641b | 2015-12-07 17:28:34 -0800 | [diff] [blame] | 304 | public boolean copyConfig(String targetConfiguration, String newConfiguration) |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 305 | throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 306 | newConfiguration = newConfiguration.trim(); |
| 307 | if (!newConfiguration.startsWith("<configuration>")) { |
| 308 | newConfiguration = "<configuration>" + newConfiguration |
| 309 | + "</configuration>"; |
| 310 | } |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 311 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 312 | rpc.append("<rpc>"); |
| 313 | rpc.append("<copy-config>"); |
| 314 | rpc.append("<target>"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 315 | rpc.append("<").append(targetConfiguration).append("/>"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 316 | rpc.append("</target>"); |
| 317 | rpc.append("<source>"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 318 | rpc.append("<").append(newConfiguration).append("/>"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 319 | rpc.append("</source>"); |
| 320 | rpc.append("</copy-config>"); |
| 321 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 322 | rpc.append(ENDPATTERN); |
| 323 | return checkReply(sendRequest(rpc.toString())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 327 | public boolean deleteConfig(String targetConfiguration) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 328 | if (targetConfiguration.equals("running")) { |
| 329 | log.warn("Target configuration for delete operation can't be \"running\"", |
| 330 | targetConfiguration); |
| 331 | return false; |
| 332 | } |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 333 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 334 | rpc.append("<rpc>"); |
| 335 | rpc.append("<delete-config>"); |
| 336 | rpc.append("<target>"); |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 337 | rpc.append("<").append(targetConfiguration).append("/>"); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 338 | rpc.append("</target>"); |
| 339 | rpc.append("</delete-config>"); |
| 340 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 341 | rpc.append(ENDPATTERN); |
| 342 | return checkReply(sendRequest(rpc.toString())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 346 | public boolean lock() throws NetconfException { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 347 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 348 | rpc.append("<rpc>"); |
| 349 | rpc.append("<lock>"); |
| 350 | rpc.append("<target>"); |
| 351 | rpc.append("<candidate/>"); |
| 352 | rpc.append("</target>"); |
| 353 | rpc.append("</lock>"); |
| 354 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 355 | rpc.append(ENDPATTERN); |
| 356 | return checkReply(sendRequest(rpc.toString())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 360 | public boolean unlock() throws NetconfException { |
Andrea Campanella | b029b9e | 2016-01-29 11:05:36 -0800 | [diff] [blame] | 361 | StringBuilder rpc = new StringBuilder(XML_HEADER); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 362 | rpc.append("<rpc>"); |
| 363 | rpc.append("<unlock>"); |
| 364 | rpc.append("<target>"); |
| 365 | rpc.append("<candidate/>"); |
| 366 | rpc.append("</target>"); |
| 367 | rpc.append("</unlock>"); |
| 368 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 369 | rpc.append(ENDPATTERN); |
| 370 | return checkReply(sendRequest(rpc.toString())); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | @Override |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 374 | public boolean close() throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 375 | return close(false); |
| 376 | } |
| 377 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 378 | private boolean close(boolean force) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 379 | StringBuilder rpc = new StringBuilder(); |
| 380 | rpc.append("<rpc>"); |
| 381 | if (force) { |
| 382 | rpc.append("<kill-configuration/>"); |
| 383 | } else { |
| 384 | rpc.append("<close-configuration/>"); |
| 385 | } |
| 386 | rpc.append("<close-configuration/>"); |
| 387 | rpc.append("</rpc>"); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 388 | rpc.append(ENDPATTERN); |
| 389 | return checkReply(sendRequest(rpc.toString())) || close(true); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public String getSessionId() { |
| 394 | if (serverCapabilities.contains("<session-id>")) { |
| 395 | String[] outer = serverCapabilities.split("<session-id>"); |
| 396 | Preconditions.checkArgument(outer.length != 1, |
| 397 | "Error in retrieving the session id"); |
| 398 | String[] value = outer[1].split("</session-id>"); |
| 399 | Preconditions.checkArgument(value.length != 1, |
| 400 | "Error in retrieving the session id"); |
| 401 | return value[0]; |
| 402 | } else { |
| 403 | return String.valueOf(-1); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public String getServerCapabilities() { |
| 409 | return serverCapabilities; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public void setDeviceCapabilities(List<String> capabilities) { |
| 414 | deviceCapabilities = capabilities; |
| 415 | } |
| 416 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 417 | @Override |
| 418 | public void addDeviceOutputListener(NetconfDeviceOutputEventListener listener) { |
| 419 | t.addDeviceEventListener(listener); |
| 420 | } |
| 421 | |
| 422 | @Override |
| 423 | public void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener) { |
| 424 | t.removeDeviceEventListener(listener); |
| 425 | } |
| 426 | |
| 427 | private boolean checkReply(String reply) throws NetconfException { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 428 | if (reply != null) { |
| 429 | if (!reply.contains("<rpc-error>")) { |
| 430 | return true; |
| 431 | } else if (reply.contains("<ok/>") |
| 432 | || (reply.contains("<rpc-error>") |
| 433 | && reply.contains("warning"))) { |
| 434 | return true; |
| 435 | } |
| 436 | } |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 437 | log.warn("Device " + deviceInfo + "has error in reply {}", reply); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 441 | public class NetconfSessionDelegateImpl implements NetconfSessionDelegate { |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 442 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 443 | @Override |
Andreas Papazois | d4712e2 | 2016-02-10 15:59:55 +0200 | [diff] [blame] | 444 | public void notify(NetconfDeviceOutputEvent event) { |
| 445 | Optional<Integer> messageId = event.getMessageID(); |
| 446 | if (!messageId.isPresent()) { |
| 447 | errorReplies.add(event.getMessagePayload()); |
| 448 | log.error("Device " + event.getDeviceInfo() + |
| 449 | " sent error reply " + event.getMessagePayload()); |
| 450 | return; |
| 451 | } |
| 452 | CompletableFuture<String> completedReply = |
| 453 | replies.get(messageId.get()); |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 454 | completedReply.complete(event.getMessagePayload()); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 455 | } |
| 456 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 457 | } |