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