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