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