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