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