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