Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -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 Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.netconf.ctl.impl; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 25 | import org.apache.sshd.client.SshClient; |
| 26 | import org.apache.sshd.client.channel.ClientChannel; |
| 27 | import org.apache.sshd.client.future.ConnectFuture; |
| 28 | import org.apache.sshd.client.future.OpenFuture; |
| 29 | import org.apache.sshd.client.session.ClientSession; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 30 | import org.apache.sshd.common.FactoryManager; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 31 | import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 32 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 33 | import org.bouncycastle.openssl.PEMKeyPair; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 34 | import org.bouncycastle.openssl.PEMParser; |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 35 | import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 36 | import org.onlab.osgi.DefaultServiceDirectory; |
| 37 | import org.onlab.osgi.ServiceDirectory; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 38 | import org.onlab.util.ItemNotFoundException; |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 39 | import org.onlab.util.SharedExecutors; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
| 41 | import org.onosproject.net.driver.Driver; |
| 42 | import org.onosproject.net.driver.DriverService; |
Yuta HIGUCHI | 4f55c67 | 2018-06-14 18:10:43 -0700 | [diff] [blame] | 43 | import org.onosproject.netconf.AbstractNetconfSession; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 44 | import org.onosproject.netconf.NetconfController; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 45 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 46 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 47 | import org.onosproject.netconf.NetconfDeviceOutputEvent.Type; |
| 48 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 49 | import org.onosproject.netconf.NetconfException; |
| 50 | import org.onosproject.netconf.NetconfSession; |
| 51 | import org.onosproject.netconf.NetconfSessionFactory; |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 52 | import org.onosproject.netconf.NetconfTransportException; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
Yuta HIGUCHI | 1567798 | 2017-08-16 15:50:29 -0700 | [diff] [blame] | 54 | |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 55 | import java.io.CharArrayReader; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 56 | import java.io.IOException; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 57 | import java.security.KeyFactory; |
| 58 | import java.security.KeyPair; |
| 59 | import java.security.NoSuchAlgorithmException; |
| 60 | import java.security.PublicKey; |
| 61 | import java.security.spec.InvalidKeySpecException; |
| 62 | import java.security.spec.X509EncodedKeySpec; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 63 | import java.util.ArrayList; |
| 64 | import java.util.Arrays; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 65 | import java.util.Collection; |
| 66 | import java.util.Collections; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 67 | import java.util.LinkedHashSet; |
| 68 | import java.util.List; |
| 69 | import java.util.Map; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 70 | import java.util.Optional; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 71 | import java.util.Set; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 72 | import java.util.concurrent.CompletableFuture; |
| 73 | import java.util.concurrent.ConcurrentHashMap; |
| 74 | import java.util.concurrent.CopyOnWriteArrayList; |
| 75 | import java.util.concurrent.ExecutionException; |
| 76 | import java.util.concurrent.TimeUnit; |
| 77 | import java.util.concurrent.TimeoutException; |
| 78 | import java.util.concurrent.atomic.AtomicInteger; |
| 79 | import java.util.regex.Matcher; |
| 80 | import java.util.regex.Pattern; |
| 81 | |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 82 | import static java.nio.charset.StandardCharsets.UTF_8; |
| 83 | import static org.slf4j.LoggerFactory.getLogger; |
| 84 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Implementation of a NETCONF session to talk to a device. |
| 87 | */ |
Yuta HIGUCHI | 4f55c67 | 2018-06-14 18:10:43 -0700 | [diff] [blame] | 88 | public class NetconfSessionMinaImpl extends AbstractNetconfSession { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 89 | |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 90 | private static final Logger log = getLogger(NetconfSessionMinaImpl.class); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 91 | |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 92 | /** |
| 93 | * NC 1.0, RFC4742 EOM sequence. |
| 94 | */ |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 95 | private static final String ENDPATTERN = "]]>]]>"; |
| 96 | private static final String MESSAGE_ID_STRING = "message-id"; |
| 97 | private static final String HELLO = "<hello"; |
| 98 | private static final String NEW_LINE = "\n"; |
| 99 | private static final String END_OF_RPC_OPEN_TAG = "\">"; |
| 100 | private static final String EQUAL = "="; |
| 101 | private static final String NUMBER_BETWEEN_QUOTES_MATCHER = "\"+([0-9]+)+\""; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 102 | private static final String SUBTREE_FILTER_CLOSE = "</filter>"; |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 103 | // FIXME hard coded namespace nc |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 104 | private static final String XML_HEADER = |
| 105 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; |
Yuta HIGUCHI | 4f55c67 | 2018-06-14 18:10:43 -0700 | [diff] [blame] | 106 | |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 107 | // FIXME hard coded namespace base10 |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 108 | private static final String SUBSCRIPTION_SUBTREE_FILTER_OPEN = |
| 109 | "<filter xmlns:base10=\"urn:ietf:params:xml:ns:netconf:base:1.0\" base10:type=\"subtree\">"; |
| 110 | |
| 111 | private static final String INTERLEAVE_CAPABILITY_STRING = "urn:ietf:params:netconf:capability:interleave:1.0"; |
| 112 | |
| 113 | private static final String CAPABILITY_REGEX = "<capability>\\s*(.*?)\\s*</capability>"; |
| 114 | private static final Pattern CAPABILITY_REGEX_PATTERN = Pattern.compile(CAPABILITY_REGEX); |
| 115 | |
| 116 | private static final String SESSION_ID_REGEX = "<session-id>\\s*(.*?)\\s*</session-id>"; |
| 117 | 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] | 118 | private static final String HASH = "#"; |
| 119 | private static final String LF = "\n"; |
| 120 | private static final String MSGLEN_REGEX_PATTERN = "\n#\\d+\n"; |
| 121 | private static final String NETCONF_10_CAPABILITY = "urn:ietf:params:netconf:base:1.0"; |
| 122 | private static final String NETCONF_11_CAPABILITY = "urn:ietf:params:netconf:base:1.1"; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 123 | private static final String NETCONF_CLIENT_CAPABILITY = "netconfClientCapability"; |
Laszlo Papp | bdb6408 | 2018-09-11 12:21:29 +0100 | [diff] [blame] | 124 | private static final String NOTIFICATION_STREAM = "notificationStream"; |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 125 | |
| 126 | private static ServiceDirectory directory = new DefaultServiceDirectory(); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 127 | |
| 128 | private String sessionID; |
| 129 | private final AtomicInteger messageIdInteger = new AtomicInteger(1); |
| 130 | protected final NetconfDeviceInfo deviceInfo; |
| 131 | private Iterable<String> onosCapabilities = |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 132 | ImmutableList.of(NETCONF_10_CAPABILITY, NETCONF_11_CAPABILITY); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 133 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 134 | private final Set<String> deviceCapabilities = new LinkedHashSet<>(); |
| 135 | private NetconfStreamHandler streamHandler; |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 136 | // FIXME ONOS-7019 key type should be revised to a String, see RFC6241 |
| 137 | /** |
| 138 | * Message-ID and corresponding Future waiting for response. |
| 139 | */ |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 140 | private Map<Integer, CompletableFuture<String>> replies; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 141 | private List<String> errorReplies; // Not sure why we need this? |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 142 | private boolean subscriptionConnected = false; |
| 143 | private String notificationFilterSchema = null; |
| 144 | |
| 145 | private final Collection<NetconfDeviceOutputEventListener> primaryListeners = |
| 146 | new CopyOnWriteArrayList<>(); |
| 147 | private final Collection<NetconfSession> children = |
| 148 | new CopyOnWriteArrayList<>(); |
| 149 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 150 | private int connectTimeout; |
| 151 | private int replyTimeout; |
| 152 | private int idleTimeout; |
| 153 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 154 | private ClientChannel channel = null; |
| 155 | private ClientSession session = null; |
| 156 | private SshClient client = null; |
| 157 | |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 158 | private boolean disconnected = false; |
| 159 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 160 | public NetconfSessionMinaImpl(NetconfDeviceInfo deviceInfo) throws NetconfException { |
| 161 | this.deviceInfo = deviceInfo; |
| 162 | replies = new ConcurrentHashMap<>(); |
| 163 | errorReplies = new ArrayList<>(); |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 164 | Set<String> capabilities = getClientCapabilites(deviceInfo.getDeviceId()); |
| 165 | if (!capabilities.isEmpty()) { |
| 166 | capabilities.addAll(Sets.newHashSet(onosCapabilities)); |
| 167 | setOnosCapabilities(capabilities); |
| 168 | } |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 169 | // FIXME should not immediately start session on construction |
| 170 | // setOnosCapabilities() is useless due to this behavior |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 171 | startConnection(); |
| 172 | } |
| 173 | |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 174 | public NetconfSessionMinaImpl(NetconfDeviceInfo deviceInfo, List<String> capabilities) throws NetconfException { |
| 175 | this.deviceInfo = deviceInfo; |
| 176 | replies = new ConcurrentHashMap<>(); |
| 177 | errorReplies = new ArrayList<>(); |
| 178 | setOnosCapabilities(capabilities); |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 179 | // FIXME should not immediately start session on construction |
| 180 | // setOnosCapabilities() is useless due to this behavior |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 181 | startConnection(); |
| 182 | } |
| 183 | |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 184 | /** |
| 185 | * Get the list of the netconf client capabilities from device driver property. |
| 186 | * |
| 187 | * @param deviceId the deviceID for which to recover the capabilities from the driver. |
| 188 | * @return the String list of clientCapability property, or null if it is not configured |
| 189 | */ |
| 190 | public Set<String> getClientCapabilites(DeviceId deviceId) { |
| 191 | Set<String> capabilities = new LinkedHashSet<>(); |
| 192 | DriverService driverService = directory.get(DriverService.class); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 193 | try { |
| 194 | Driver driver = driverService.getDriver(deviceId); |
| 195 | if (driver == null) { |
| 196 | return capabilities; |
| 197 | } |
| 198 | String clientCapabilities = driver.getProperty(NETCONF_CLIENT_CAPABILITY); |
| 199 | if (clientCapabilities == null) { |
| 200 | return capabilities; |
| 201 | } |
| 202 | String[] textStr = clientCapabilities.split("\\|"); |
| 203 | capabilities.addAll(Arrays.asList(textStr)); |
| 204 | return capabilities; |
| 205 | } catch (ItemNotFoundException e) { |
| 206 | log.warn("Driver for device {} currently not available", deviceId); |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 207 | return capabilities; |
| 208 | } |
quan PHAM VAN | 32d70e5 | 2018-08-01 17:35:30 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 211 | private void startConnection() throws NetconfException { |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 212 | connectTimeout = deviceInfo.getConnectTimeoutSec().orElse( |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 213 | NetconfControllerImpl.netconfConnectTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 214 | replyTimeout = deviceInfo.getReplyTimeoutSec().orElse( |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 215 | NetconfControllerImpl.netconfReplyTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 216 | idleTimeout = deviceInfo.getIdleTimeoutSec().orElse( |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 217 | NetconfControllerImpl.netconfIdleTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 218 | log.info("Connecting to {} with timeouts C:{}, R:{}, I:{}", deviceInfo, |
| 219 | connectTimeout, replyTimeout, idleTimeout); |
| 220 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 221 | try { |
| 222 | startClient(); |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 223 | } catch (Exception e) { |
| 224 | stopClient(); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 225 | throw new NetconfException("Failed to establish SSH with device " + deviceInfo, e); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | private void startClient() throws IOException { |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 230 | log.info("Creating NETCONF session to {}", |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 231 | deviceInfo.getDeviceId()); |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 232 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 233 | client = SshClient.setUpDefaultClient(); |
Andrea Campanella | 59227c3 | 2019-01-07 14:52:35 +0100 | [diff] [blame] | 234 | if (idleTimeout != NetconfControllerImpl.netconfIdleTimeout) { |
| 235 | client.getProperties().putIfAbsent(FactoryManager.IDLE_TIMEOUT, |
| 236 | TimeUnit.SECONDS.toMillis(idleTimeout)); |
| 237 | client.getProperties().putIfAbsent(FactoryManager.NIO2_READ_TIMEOUT, |
| 238 | TimeUnit.SECONDS.toMillis(idleTimeout + 15L)); |
| 239 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 240 | client.start(); |
| 241 | client.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); |
| 242 | startSession(); |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 243 | |
| 244 | disconnected = false; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Yuta HIGUCHI | 4f55c67 | 2018-06-14 18:10:43 -0700 | [diff] [blame] | 247 | //TODO: Remove the default methods already implemented in NetconfSession |
| 248 | |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 249 | // FIXME blocking |
| 250 | @Deprecated |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 251 | private void startSession() throws IOException { |
| 252 | final ConnectFuture connectFuture; |
| 253 | connectFuture = client.connect(deviceInfo.name(), |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 254 | deviceInfo.ip().toString(), |
| 255 | deviceInfo.port()) |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 256 | .verify(connectTimeout, TimeUnit.SECONDS); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 257 | session = connectFuture.getSession(); |
| 258 | //Using the device ssh key if possible |
| 259 | if (deviceInfo.getKey() != null) { |
Yuta HIGUCHI | b2d0524 | 2017-09-05 15:44:34 -0700 | [diff] [blame] | 260 | try (PEMParser pemParser = new PEMParser(new CharArrayReader(deviceInfo.getKey()))) { |
| 261 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME); |
| 262 | try { |
| 263 | KeyPair kp = converter.getKeyPair((PEMKeyPair) pemParser.readObject()); |
| 264 | session.addPublicKeyIdentity(kp); |
| 265 | } catch (IOException e) { |
| 266 | throw new NetconfException("Failed to authenticate session with device " + |
| 267 | deviceInfo + "check key to be a valid key", e); |
| 268 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 269 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 270 | } else { |
| 271 | session.addPasswordIdentity(deviceInfo.password()); |
| 272 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 273 | session.auth().verify(connectTimeout, TimeUnit.SECONDS); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 274 | Set<ClientSession.ClientSessionEvent> event = session.waitFor( |
| 275 | ImmutableSet.of(ClientSession.ClientSessionEvent.WAIT_AUTH, |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 276 | ClientSession.ClientSessionEvent.CLOSED, |
| 277 | ClientSession.ClientSessionEvent.AUTHED), 0); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 278 | |
| 279 | if (!event.contains(ClientSession.ClientSessionEvent.AUTHED)) { |
| 280 | log.debug("Session closed {} {}", event, session.isClosed()); |
| 281 | throw new NetconfException("Failed to authenticate session with device " + |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 282 | deviceInfo + "check the user/pwd or key"); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 283 | } |
| 284 | openChannel(); |
| 285 | } |
| 286 | |
| 287 | private PublicKey getPublicKey(byte[] keyBytes, String type) |
| 288 | throws NoSuchAlgorithmException, InvalidKeySpecException { |
| 289 | |
| 290 | X509EncodedKeySpec spec = |
| 291 | new X509EncodedKeySpec(keyBytes); |
| 292 | KeyFactory kf = KeyFactory.getInstance(type); |
| 293 | return kf.generatePublic(spec); |
| 294 | } |
| 295 | |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 296 | // FIXME blocking |
| 297 | @Deprecated |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 298 | private void openChannel() throws IOException { |
| 299 | channel = session.createSubsystemChannel("netconf"); |
| 300 | OpenFuture channelFuture = channel.open(); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 301 | if (channelFuture.await(connectTimeout, TimeUnit.SECONDS)) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 302 | if (channelFuture.isOpened()) { |
| 303 | streamHandler = new NetconfStreamThread(channel.getInvertedOut(), channel.getInvertedIn(), |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 304 | channel.getInvertedErr(), deviceInfo, |
| 305 | new NetconfSessionDelegateImpl(), replies); |
Andrea Campanella | 59227c3 | 2019-01-07 14:52:35 +0100 | [diff] [blame] | 306 | primaryListeners.forEach(l -> streamHandler.addDeviceEventListener(l)); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 307 | } else { |
| 308 | throw new NetconfException("Failed to open channel with device " + |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 309 | deviceInfo); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 310 | } |
| 311 | sendHello(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | @Beta |
| 317 | protected void startSubscriptionStream(String filterSchema) throws NetconfException { |
| 318 | boolean openNewSession = false; |
| 319 | if (!deviceCapabilities.contains(INTERLEAVE_CAPABILITY_STRING)) { |
| 320 | log.info("Device {} doesn't support interleave, creating child session", deviceInfo); |
| 321 | openNewSession = true; |
| 322 | |
| 323 | } else if (subscriptionConnected && |
| 324 | notificationFilterSchema != null && |
| 325 | !Objects.equal(filterSchema, notificationFilterSchema)) { |
| 326 | // interleave supported and existing filter is NOT "no filtering" |
| 327 | // and was requested with different filtering schema |
| 328 | log.info("Cannot use existing session for subscription {} ({})", |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 329 | deviceInfo, filterSchema); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 330 | openNewSession = true; |
| 331 | } |
| 332 | |
| 333 | if (openNewSession) { |
| 334 | log.info("Creating notification session to {} with filter {}", |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 335 | deviceInfo, filterSchema); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 336 | NetconfSession child = new NotificationSession(deviceInfo); |
| 337 | |
| 338 | child.addDeviceOutputListener(new NotificationForwarder()); |
| 339 | |
| 340 | child.startSubscription(filterSchema); |
| 341 | children.add(child); |
| 342 | return; |
| 343 | } |
| 344 | |
| 345 | // request to start interleaved notification session |
| 346 | String reply = sendRequest(createSubscriptionString(filterSchema)); |
| 347 | if (!checkReply(reply)) { |
| 348 | throw new NetconfException("Subscription not successful with device " |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 349 | + deviceInfo + " with reply " + reply); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 350 | } |
| 351 | subscriptionConnected = true; |
| 352 | } |
| 353 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 354 | @Beta |
| 355 | @Override |
| 356 | public void startSubscription(String filterSchema) throws NetconfException { |
| 357 | if (!subscriptionConnected) { |
| 358 | notificationFilterSchema = filterSchema; |
| 359 | startSubscriptionStream(filterSchema); |
| 360 | } |
| 361 | streamHandler.setEnableNotifications(true); |
| 362 | } |
| 363 | |
| 364 | @Beta |
| 365 | protected String createSubscriptionString(String filterSchema) { |
| 366 | StringBuilder subscriptionbuffer = new StringBuilder(); |
| 367 | subscriptionbuffer.append("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
| 368 | subscriptionbuffer.append(" <create-subscription\n"); |
| 369 | subscriptionbuffer.append("xmlns=\"urn:ietf:params:xml:ns:netconf:notification:1.0\">\n"); |
Laszlo Papp | bdb6408 | 2018-09-11 12:21:29 +0100 | [diff] [blame] | 370 | DriverService driverService = directory.get(DriverService.class); |
| 371 | Driver driver = driverService.getDriver(deviceInfo.getDeviceId()); |
| 372 | if (driver != null) { |
| 373 | String stream = driver.getProperty(NOTIFICATION_STREAM); |
| 374 | if (stream != null) { |
| 375 | subscriptionbuffer.append(" <stream>"); |
| 376 | subscriptionbuffer.append(stream); |
| 377 | subscriptionbuffer.append("</stream>\n"); |
| 378 | } |
| 379 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 380 | // FIXME Only subtree filtering supported at the moment. |
| 381 | if (filterSchema != null) { |
| 382 | subscriptionbuffer.append(" "); |
| 383 | subscriptionbuffer.append(SUBSCRIPTION_SUBTREE_FILTER_OPEN).append(NEW_LINE); |
| 384 | subscriptionbuffer.append(filterSchema).append(NEW_LINE); |
| 385 | subscriptionbuffer.append(" "); |
| 386 | subscriptionbuffer.append(SUBTREE_FILTER_CLOSE).append(NEW_LINE); |
| 387 | } |
| 388 | subscriptionbuffer.append(" </create-subscription>\n"); |
| 389 | subscriptionbuffer.append("</rpc>\n"); |
| 390 | subscriptionbuffer.append(ENDPATTERN); |
| 391 | return subscriptionbuffer.toString(); |
| 392 | } |
| 393 | |
| 394 | @Override |
| 395 | public void endSubscription() throws NetconfException { |
| 396 | if (subscriptionConnected) { |
| 397 | streamHandler.setEnableNotifications(false); |
| 398 | } else { |
| 399 | throw new NetconfException("Subscription does not exist."); |
| 400 | } |
| 401 | } |
| 402 | |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 403 | private void stopClient() { |
| 404 | if (session != null) { |
| 405 | try { |
| 406 | session.close(); |
| 407 | } catch (IOException ex) { |
| 408 | log.warn("Cannot close session {} {}", sessionID, deviceInfo, ex); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | if (channel != null) { |
| 413 | try { |
| 414 | channel.close(); |
| 415 | } catch (IOException ex) { |
| 416 | log.warn("Cannot close channel {} {}", sessionID, deviceInfo, ex); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (client != null) { |
| 421 | try { |
| 422 | client.close(); |
| 423 | } catch (IOException ex) { |
| 424 | log.warn("Cannot close client {} {}", sessionID, deviceInfo, ex); |
| 425 | } |
| 426 | |
| 427 | client.stop(); |
| 428 | } |
| 429 | } |
| 430 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 431 | private void sendHello() throws NetconfException { |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 432 | String serverHelloResponse = sendRequest(createHelloString(), true); |
| 433 | Matcher capabilityMatcher = CAPABILITY_REGEX_PATTERN.matcher(serverHelloResponse); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 434 | while (capabilityMatcher.find()) { |
| 435 | deviceCapabilities.add(capabilityMatcher.group(1)); |
| 436 | } |
| 437 | sessionID = String.valueOf(-1); |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 438 | Matcher sessionIDMatcher = SESSION_ID_REGEX_PATTERN.matcher(serverHelloResponse); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 439 | if (sessionIDMatcher.find()) { |
| 440 | sessionID = sessionIDMatcher.group(1); |
| 441 | } else { |
| 442 | throw new NetconfException("Missing SessionID in server hello " + |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 443 | "reponse."); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | } |
| 447 | |
| 448 | private String createHelloString() { |
| 449 | StringBuilder hellobuffer = new StringBuilder(); |
| 450 | hellobuffer.append(XML_HEADER); |
| 451 | hellobuffer.append("\n"); |
| 452 | hellobuffer.append("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"); |
| 453 | hellobuffer.append(" <capabilities>\n"); |
| 454 | onosCapabilities.forEach( |
| 455 | cap -> hellobuffer.append(" <capability>") |
| 456 | .append(cap) |
| 457 | .append("</capability>\n")); |
| 458 | hellobuffer.append(" </capabilities>\n"); |
| 459 | hellobuffer.append("</hello>\n"); |
| 460 | hellobuffer.append(ENDPATTERN); |
| 461 | return hellobuffer.toString(); |
| 462 | |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public void checkAndReestablish() throws NetconfException { |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 467 | if (disconnected) { |
| 468 | log.warn("Can't reopen connection for device because of disconnected {}", deviceInfo.getDeviceId()); |
| 469 | throw new NetconfException("Can't reopen connection for device because of disconnected " + deviceInfo); |
| 470 | } |
| 471 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 472 | try { |
zhongguo zhao | 98bb37a | 2018-08-28 16:17:06 +0800 | [diff] [blame] | 473 | if (client.isClosed() || client.isClosing()) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 474 | log.debug("Trying to restart the whole SSH connection with {}", deviceInfo.getDeviceId()); |
| 475 | cleanUp(); |
| 476 | startConnection(); |
zhongguo zhao | 98bb37a | 2018-08-28 16:17:06 +0800 | [diff] [blame] | 477 | } else if (session.isClosed() || session.isClosing()) { |
Kieran McPeake | e1b418f | 2019-05-23 13:42:13 +0100 | [diff] [blame] | 478 | log.debug("Trying to restart the session {} with {}", session, deviceInfo.getDeviceId()); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 479 | cleanUp(); |
| 480 | startSession(); |
zhongguo zhao | 98bb37a | 2018-08-28 16:17:06 +0800 | [diff] [blame] | 481 | } else if (channel.isClosed() || channel.isClosing()) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 482 | log.debug("Trying to reopen the channel with {}", deviceInfo.getDeviceId()); |
| 483 | cleanUp(); |
| 484 | openChannel(); |
Andrea Campanella | 856f313 | 2017-10-23 15:46:36 +0200 | [diff] [blame] | 485 | } else { |
| 486 | return; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 487 | } |
| 488 | if (subscriptionConnected) { |
| 489 | log.debug("Restarting subscription with {}", deviceInfo.getDeviceId()); |
| 490 | subscriptionConnected = false; |
| 491 | startSubscription(notificationFilterSchema); |
| 492 | } |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 493 | } catch (IOException | IllegalStateException e) { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 494 | log.error("Can't reopen connection for device {}", e.getMessage()); |
| 495 | throw new NetconfException("Cannot re-open the connection with device" + deviceInfo, e); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | private void cleanUp() { |
| 500 | //makes sure everything is at a clean state. |
| 501 | replies.clear(); |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 502 | if (streamHandler != null) { |
| 503 | streamHandler.close(); |
| 504 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | @Override |
| 508 | public String requestSync(String request) throws NetconfException { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 509 | return requestSync(request, replyTimeout); |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public String requestSync(String request, int timeout) throws NetconfException { |
| 514 | String reply = sendRequest(request, timeout); |
Kim JeongWoo | 8b03bc5 | 2018-08-10 16:50:23 +0900 | [diff] [blame] | 515 | if (!checkReply(reply)) { |
| 516 | throw new NetconfException("Request not successful with device " |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 517 | + deviceInfo + " with reply " + reply); |
Kim JeongWoo | 8b03bc5 | 2018-08-10 16:50:23 +0900 | [diff] [blame] | 518 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 519 | return reply; |
| 520 | } |
| 521 | |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 522 | |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 523 | // FIXME rename to align with what it actually do |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 524 | |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 525 | /** |
| 526 | * Validate and format netconf message. |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 527 | * - NC1.0 if no EOM sequence present on {@code message}, append. |
| 528 | * - NC1.1 chunk-encode given message unless it already is chunk encoded |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 529 | * |
| 530 | * @param message to format |
| 531 | * @return formated message |
| 532 | */ |
| 533 | private String formatNetconfMessage(String message) { |
| 534 | if (deviceCapabilities.contains(NETCONF_11_CAPABILITY)) { |
| 535 | message = formatChunkedMessage(message); |
| 536 | } else { |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 537 | if (!message.endsWith(ENDPATTERN)) { |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 538 | message = message + NEW_LINE + ENDPATTERN; |
| 539 | } |
| 540 | } |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 541 | return message; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /** |
| 545 | * Validate and format message according to chunked framing mechanism. |
| 546 | * |
| 547 | * @param message to format |
| 548 | * @return formated message |
| 549 | */ |
| 550 | private String formatChunkedMessage(String message) { |
| 551 | if (message.endsWith(ENDPATTERN)) { |
Yuta HIGUCHI | b2d0524 | 2017-09-05 15:44:34 -0700 | [diff] [blame] | 552 | // message given had Netconf 1.0 EOM pattern -> remove |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 553 | message = message.substring(0, message.length() - ENDPATTERN.length()); |
| 554 | } |
| 555 | if (!message.startsWith(LF + HASH)) { |
Yuta HIGUCHI | b2d0524 | 2017-09-05 15:44:34 -0700 | [diff] [blame] | 556 | // chunk encode message |
Yuta HIGUCHI | 1567798 | 2017-08-16 15:50:29 -0700 | [diff] [blame] | 557 | message = LF + HASH + message.getBytes(UTF_8).length + LF + message + LF + HASH + HASH + LF; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 558 | } |
| 559 | return message; |
| 560 | } |
| 561 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 562 | @Override |
| 563 | @Deprecated |
| 564 | public CompletableFuture<String> request(String request) { |
| 565 | return streamHandler.sendMessage(request); |
| 566 | } |
| 567 | |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 568 | /** |
| 569 | * {@inheritDoc} |
| 570 | * <p> |
| 571 | * FIXME Note: as of 1.12.0 |
| 572 | * {@code request} must not include message-id, this method will assign |
| 573 | * and insert message-id on it's own. |
| 574 | * Will require ONOS-7019 to remove this limitation. |
| 575 | */ |
| 576 | @Override |
| 577 | public CompletableFuture<String> rpc(String request) { |
| 578 | |
| 579 | String rpc = request; |
| 580 | // - assign message-id |
| 581 | int msgId = messageIdInteger.incrementAndGet(); |
| 582 | // - re-write request to insert message-id |
| 583 | // FIXME avoid using formatRequestMessageId |
| 584 | rpc = formatRequestMessageId(rpc, msgId); |
| 585 | // - ensure it contains XML header |
| 586 | rpc = formatXmlHeader(rpc); |
| 587 | // - use chunked framing if talking to NC 1.1 device |
| 588 | // FIXME avoid using formatNetconfMessage |
| 589 | rpc = formatNetconfMessage(rpc); |
| 590 | |
| 591 | // TODO session liveness check & recovery |
| 592 | |
| 593 | log.debug("Sending {} to {}", rpc, this.deviceInfo.getDeviceId()); |
| 594 | return streamHandler.sendMessage(rpc, msgId) |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 595 | .handleAsync((reply, t) -> { |
| 596 | if (t != null) { |
| 597 | // secure transport-layer error |
| 598 | // cannot use NetconfException, which is |
| 599 | // checked Exception. |
| 600 | throw new NetconfTransportException(t); |
| 601 | } else { |
| 602 | // FIXME avoid using checkReply, error handling is weird |
| 603 | if (!checkReply(reply)) { |
| 604 | throw new NetconfTransportException("rpc-request not successful with device " |
| 605 | + deviceInfo + " with reply " + reply); |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 606 | } |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 607 | return reply; |
| 608 | } |
| 609 | }, SharedExecutors.getPoolThreadExecutor()); |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 612 | @Override |
| 613 | public int timeoutConnectSec() { |
| 614 | return connectTimeout; |
| 615 | } |
| 616 | |
| 617 | @Override |
| 618 | public int timeoutReplySec() { |
| 619 | return replyTimeout; |
| 620 | } |
| 621 | |
| 622 | @Override |
| 623 | public int timeoutIdleSec() { |
| 624 | return idleTimeout; |
| 625 | } |
| 626 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 627 | private CompletableFuture<String> request(String request, int messageId) { |
| 628 | return streamHandler.sendMessage(request, messageId); |
| 629 | } |
| 630 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 631 | private String sendRequest(String request, boolean isHello) throws NetconfException { |
| 632 | return sendRequest(request, isHello, replyTimeout); |
| 633 | } |
| 634 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 635 | private String sendRequest(String request) throws NetconfException { |
Yuta HIGUCHI | b2d0524 | 2017-09-05 15:44:34 -0700 | [diff] [blame] | 636 | // FIXME probably chunk-encoding too early |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 637 | request = formatNetconfMessage(request); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 638 | return sendRequest(request, false, replyTimeout); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 639 | } |
| 640 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 641 | private String sendRequest(String request, int timeout) throws NetconfException { |
| 642 | // FIXME probably chunk-encoding too early |
| 643 | request = formatNetconfMessage(request); |
| 644 | return sendRequest(request, false, timeout); |
| 645 | } |
| 646 | |
| 647 | private String sendRequest(String request, boolean isHello, int timeout) throws NetconfException { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 648 | checkAndReestablish(); |
| 649 | int messageId = -1; |
| 650 | if (!isHello) { |
| 651 | messageId = messageIdInteger.getAndIncrement(); |
| 652 | } |
Yuta HIGUCHI | b2d0524 | 2017-09-05 15:44:34 -0700 | [diff] [blame] | 653 | // FIXME potentially re-writing chunked encoded String? |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 654 | request = formatXmlHeader(request); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 655 | request = formatRequestMessageId(request, messageId); |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 656 | log.debug("Sending request to NETCONF with timeout {} for {}", |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 657 | replyTimeout, deviceInfo.name()); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 658 | CompletableFuture<String> futureReply = request(request, messageId); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 659 | String rp; |
| 660 | try { |
| 661 | rp = futureReply.get(replyTimeout, TimeUnit.SECONDS); |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 662 | replies.remove(messageId); // Why here??? |
| 663 | } catch (InterruptedException e) { |
| 664 | Thread.currentThread().interrupt(); |
| 665 | throw new NetconfException("Interrupted waiting for reply for request" + request, e); |
| 666 | } catch (TimeoutException e) { |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 667 | throw new NetconfException("Timed out waiting for reply for request " + |
| 668 | request + " after " + replyTimeout + " sec.", e); |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 669 | } catch (ExecutionException e) { |
| 670 | log.warn("Closing session {} for {} due to unexpected Error", sessionID, deviceInfo, e); |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 671 | stopClient(); |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 672 | NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent( |
| 673 | NetconfDeviceOutputEvent.Type.SESSION_CLOSED, |
| 674 | null, "Closed due to unexpected error " + e.getCause(), |
| 675 | Optional.of(-1), deviceInfo); |
| 676 | publishEvent(event); |
| 677 | errorReplies.clear(); // move to cleanUp()? |
| 678 | cleanUp(); |
| 679 | |
| 680 | throw new NetconfException("Closing session " + sessionID + " for " + deviceInfo + |
| 681 | " for request " + request, e); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 682 | } |
| 683 | log.debug("Result {} from request {} to device {}", rp, request, deviceInfo); |
| 684 | return rp.trim(); |
| 685 | } |
| 686 | |
| 687 | private String formatRequestMessageId(String request, int messageId) { |
| 688 | if (request.contains(MESSAGE_ID_STRING)) { |
| 689 | //FIXME if application provides his own counting of messages this fails that count |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 690 | // FIXME assumes message-id is integer. RFC6241 allows anything as long as it is allowed in XML |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 691 | request = request.replaceFirst(MESSAGE_ID_STRING + EQUAL + NUMBER_BETWEEN_QUOTES_MATCHER, |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 692 | MESSAGE_ID_STRING + EQUAL + "\"" + messageId + "\""); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 693 | } else if (!request.contains(MESSAGE_ID_STRING) && !request.contains(HELLO)) { |
| 694 | //FIXME find out a better way to enforce the presence of message-id |
| 695 | request = request.replaceFirst(END_OF_RPC_OPEN_TAG, "\" " + MESSAGE_ID_STRING + EQUAL + "\"" |
| 696 | + messageId + "\"" + ">"); |
| 697 | } |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 698 | request = updateRequestLength(request); |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 699 | return request; |
| 700 | } |
| 701 | |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 702 | private String updateRequestLength(String request) { |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 703 | if (request.contains(LF + HASH + HASH + LF)) { |
| 704 | int oldLen = Integer.parseInt(request.split(HASH)[1].split(LF)[0]); |
| 705 | String rpcWithEnding = request.substring(request.indexOf('<')); |
| 706 | String firstBlock = request.split(MSGLEN_REGEX_PATTERN)[1].split(LF + HASH + HASH + LF)[0]; |
| 707 | int newLen = 0; |
Yuta HIGUCHI | 1567798 | 2017-08-16 15:50:29 -0700 | [diff] [blame] | 708 | newLen = firstBlock.getBytes(UTF_8).length; |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 709 | if (oldLen != newLen) { |
| 710 | return LF + HASH + newLen + LF + rpcWithEnding; |
| 711 | } |
| 712 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 713 | return request; |
| 714 | } |
| 715 | |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 716 | /** |
| 717 | * Ensures xml start directive/declaration appears in the {@code request}. |
Andrea Campanella | 3a36145 | 2019-08-02 10:17:53 +0200 | [diff] [blame^] | 718 | * |
Yuta HIGUCHI | 371667d | 2017-09-05 17:30:51 -0700 | [diff] [blame] | 719 | * @param request RPC request message |
| 720 | * @return XML RPC message |
| 721 | */ |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 722 | private String formatXmlHeader(String request) { |
Sean Condon | 2d64717 | 2017-09-19 12:29:13 +0100 | [diff] [blame] | 723 | if (!request.contains(XML_HEADER)) { |
Yuta HIGUCHI | 1567798 | 2017-08-16 15:50:29 -0700 | [diff] [blame] | 724 | //FIXME if application provides his own XML header of different type there is a clash |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 725 | if (request.startsWith(LF + HASH)) { |
| 726 | request = request.split("<")[0] + XML_HEADER + request.substring(request.split("<")[0].length()); |
| 727 | } else { |
| 728 | request = XML_HEADER + "\n" + request; |
| 729 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 730 | } |
| 731 | return request; |
| 732 | } |
| 733 | |
| 734 | @Override |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 735 | public String getSessionId() { |
| 736 | return sessionID; |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public Set<String> getDeviceCapabilitiesSet() { |
| 741 | return Collections.unmodifiableSet(deviceCapabilities); |
| 742 | } |
| 743 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 744 | @Override |
| 745 | public void setOnosCapabilities(Iterable<String> capabilities) { |
| 746 | onosCapabilities = capabilities; |
| 747 | } |
| 748 | |
| 749 | |
| 750 | @Override |
| 751 | public void addDeviceOutputListener(NetconfDeviceOutputEventListener listener) { |
| 752 | streamHandler.addDeviceEventListener(listener); |
| 753 | primaryListeners.add(listener); |
| 754 | } |
| 755 | |
| 756 | @Override |
| 757 | public void removeDeviceOutputListener(NetconfDeviceOutputEventListener listener) { |
| 758 | primaryListeners.remove(listener); |
| 759 | streamHandler.removeDeviceEventListener(listener); |
| 760 | } |
| 761 | |
Yuta HIGUCHI | 4f55c67 | 2018-06-14 18:10:43 -0700 | [diff] [blame] | 762 | @Override |
| 763 | protected boolean checkReply(String reply) { |
| 764 | // Overridden to record error logs |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 765 | if (reply != null) { |
| 766 | if (!reply.contains("<rpc-error>")) { |
| 767 | log.debug("Device {} sent reply {}", deviceInfo, reply); |
| 768 | return true; |
| 769 | } else if (reply.contains("<ok/>") |
| 770 | || (reply.contains("<rpc-error>") |
| 771 | && reply.contains("warning"))) { |
Yuta HIGUCHI | 6e6c26e | 2017-09-06 14:25:57 -0700 | [diff] [blame] | 772 | // FIXME rpc-error with a warning is considered same as Ok?? |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 773 | log.debug("Device {} sent reply {}", deviceInfo, reply); |
| 774 | return true; |
| 775 | } |
| 776 | } |
| 777 | log.warn("Device {} has error in reply {}", deviceInfo, reply); |
| 778 | return false; |
| 779 | } |
| 780 | |
zhongguo zhao | 78eab37 | 2018-08-27 16:22:39 +0800 | [diff] [blame] | 781 | @Override |
| 782 | public boolean close() throws NetconfException { |
| 783 | try { |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 784 | if (client != null && (client.isClosed() || client.isClosing())) { |
| 785 | return true; |
| 786 | } |
| 787 | |
zhongguo zhao | 78eab37 | 2018-08-27 16:22:39 +0800 | [diff] [blame] | 788 | return super.close(); |
| 789 | } catch (IOException ioe) { |
| 790 | throw new NetconfException(ioe.getMessage()); |
| 791 | } finally { |
DongRyeol Cha | c29f907 | 2018-11-06 14:05:56 +0900 | [diff] [blame] | 792 | disconnected = true; |
| 793 | stopClient(); |
zhongguo zhao | 78eab37 | 2018-08-27 16:22:39 +0800 | [diff] [blame] | 794 | } |
| 795 | } |
| 796 | |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 797 | protected void publishEvent(NetconfDeviceOutputEvent event) { |
| 798 | primaryListeners.forEach(lsnr -> { |
| 799 | if (lsnr.isRelevant(event)) { |
| 800 | lsnr.event(event); |
| 801 | } |
| 802 | }); |
| 803 | } |
| 804 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 805 | static class NotificationSession extends NetconfSessionMinaImpl { |
| 806 | |
| 807 | private String notificationFilter; |
| 808 | |
| 809 | NotificationSession(NetconfDeviceInfo deviceInfo) |
| 810 | throws NetconfException { |
| 811 | super(deviceInfo); |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | protected void startSubscriptionStream(String filterSchema) |
| 816 | throws NetconfException { |
| 817 | |
| 818 | notificationFilter = filterSchema; |
| 819 | requestSync(createSubscriptionString(filterSchema)); |
| 820 | } |
| 821 | |
| 822 | @Override |
| 823 | public String toString() { |
| 824 | return MoreObjects.toStringHelper(getClass()) |
| 825 | .add("deviceInfo", deviceInfo) |
| 826 | .add("sessionID", getSessionId()) |
| 827 | .add("notificationFilter", notificationFilter) |
| 828 | .toString(); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * Listener attached to child session for notification streaming. |
| 834 | * <p> |
| 835 | * Forwards all notification event from child session to primary session |
| 836 | * listeners. |
| 837 | */ |
| 838 | private final class NotificationForwarder |
| 839 | implements NetconfDeviceOutputEventListener { |
| 840 | |
| 841 | @Override |
| 842 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 843 | return event.type() == Type.DEVICE_NOTIFICATION; |
| 844 | } |
| 845 | |
| 846 | @Override |
| 847 | public void event(NetconfDeviceOutputEvent event) { |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 848 | publishEvent(event); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | public class NetconfSessionDelegateImpl implements NetconfSessionDelegate { |
| 853 | |
| 854 | @Override |
| 855 | public void notify(NetconfDeviceOutputEvent event) { |
| 856 | Optional<Integer> messageId = event.getMessageID(); |
| 857 | log.debug("messageID {}, waiting replies messageIDs {}", messageId, |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 858 | replies.keySet()); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 859 | if (!messageId.isPresent()) { |
| 860 | errorReplies.add(event.getMessagePayload()); |
| 861 | log.error("Device {} sent error reply {}", |
Kamil Stasiak | 9f59f44 | 2017-05-02 11:02:24 +0200 | [diff] [blame] | 862 | event.getDeviceInfo(), event.getMessagePayload()); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 863 | return; |
| 864 | } |
David K. Bainbridge | 77d8ca4 | 2019-02-01 11:47:29 -0800 | [diff] [blame] | 865 | // Remove the message as it has been processed. |
| 866 | CompletableFuture<String> completedReply = replies.remove(messageId.get()); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 867 | if (completedReply != null) { |
| 868 | completedReply.complete(event.getMessagePayload()); |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 873 | /** |
| 874 | * @deprecated in 1.14.0 |
| 875 | */ |
| 876 | @Deprecated |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 877 | public static class MinaSshNetconfSessionFactory implements NetconfSessionFactory { |
| 878 | |
| 879 | @Override |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 880 | public NetconfSession createNetconfSession(NetconfDeviceInfo netconfDeviceInfo, |
| 881 | NetconfController netconfController) throws NetconfException { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 882 | return new NetconfSessionMinaImpl(netconfDeviceInfo); |
| 883 | } |
| 884 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 885 | } |