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