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