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