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