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