andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 1 | /* |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 2 | * Copyright 2019-present Open Networking Foundation |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 17 | package org.onosproject.netconf.ctl.impl; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 18 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 19 | |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 20 | import org.apache.commons.lang3.tuple.Triple; |
| 21 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 22 | import com.google.common.annotations.Beta; |
| 23 | import com.google.common.collect.Lists; |
| 24 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 25 | import org.onosproject.cluster.ClusterService; |
| 26 | import org.onosproject.cluster.ControllerNode; |
| 27 | import org.onosproject.cluster.NodeId; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 28 | import org.osgi.service.component.annotations.Activate; |
| 29 | import org.osgi.service.component.annotations.Component; |
| 30 | import org.osgi.service.component.annotations.Deactivate; |
| 31 | import org.osgi.service.component.annotations.Modified; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 34 | |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 35 | import org.bouncycastle.jce.provider.BouncyCastleProvider; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 36 | import org.onlab.packet.IpAddress; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 37 | import org.onlab.util.KryoNamespace; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 38 | import org.onosproject.cfg.ComponentConfigService; |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 39 | import org.onosproject.mastership.MastershipService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 40 | import org.onosproject.net.AnnotationKeys; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 41 | import org.onosproject.net.Device; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 42 | import org.onosproject.net.DeviceId; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 43 | import org.onosproject.net.MastershipRole; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceService; |
Himanshu Ranjan | 7c2ee3c | 2017-02-13 05:10:08 -0600 | [diff] [blame] | 46 | import org.onosproject.net.key.DeviceKey; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 47 | import org.onosproject.net.key.DeviceKeyId; |
| 48 | import org.onosproject.net.key.DeviceKeyService; |
| 49 | import org.onosproject.net.key.UsernamePassword; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 50 | import org.onosproject.netconf.NetconfController; |
| 51 | import org.onosproject.netconf.NetconfDevice; |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 52 | import org.onosproject.netconf.NetconfDeviceFactory; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 53 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 54 | import org.onosproject.netconf.NetconfDeviceListener; |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 55 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 56 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 57 | import org.onosproject.netconf.NetconfException; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 58 | import org.onosproject.netconf.NetconfProxyMessage; |
| 59 | import org.onosproject.netconf.NetconfProxyMessageHandler; |
| 60 | import org.onosproject.netconf.NetconfSession; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 61 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
| 62 | import org.onosproject.netconf.config.NetconfSshClientLib; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 63 | import org.onosproject.store.cluster.messaging.ClusterCommunicationService; |
| 64 | import org.onosproject.store.cluster.messaging.MessageSubject; |
| 65 | import org.onosproject.store.serializers.KryoNamespaces; |
| 66 | import org.onosproject.store.service.Serializer; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 67 | import org.osgi.service.component.ComponentContext; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 68 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 69 | import org.slf4j.Logger; |
| 70 | import org.slf4j.LoggerFactory; |
| 71 | |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 72 | import java.security.Security; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 73 | |
| 74 | import java.util.ArrayList; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 75 | import java.util.Dictionary; |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 76 | import java.util.LinkedHashSet; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 77 | import java.util.Map; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 78 | import java.util.Optional; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 79 | import java.util.Set; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 80 | import java.util.concurrent.CompletableFuture; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 81 | import java.util.concurrent.ConcurrentHashMap; |
| 82 | import java.util.concurrent.CopyOnWriteArraySet; |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 83 | import java.util.concurrent.CountDownLatch; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 84 | import java.util.concurrent.ExecutionException; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 85 | import java.util.concurrent.ExecutorService; |
| 86 | import java.util.concurrent.Executors; |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 87 | import java.util.concurrent.locks.Lock; |
| 88 | import java.util.concurrent.locks.ReentrantLock; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 89 | import java.util.concurrent.TimeUnit; |
| 90 | import java.util.concurrent.TimeoutException; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 91 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 92 | import static org.onlab.util.Tools.get; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 93 | import static org.onlab.util.Tools.getIntegerProperty; |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 94 | import static org.onlab.util.Tools.groupedThreads; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 95 | import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.*; |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 96 | import static org.onosproject.netconf.NetconfDeviceInfo.extractIpPortPath; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 97 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 98 | /** |
| 99 | * The implementation of NetconfController. |
| 100 | */ |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 101 | @Component(immediate = true, service = NetconfController.class, |
| 102 | property = { |
| 103 | NETCONF_CONNECT_TIMEOUT + ":Integer=" + NETCONF_CONNECT_TIMEOUT_DEFAULT, |
| 104 | NETCONF_REPLY_TIMEOUT + ":Integer=" + NETCONF_REPLY_TIMEOUT_DEFAULT, |
| 105 | NETCONF_IDLE_TIMEOUT + ":Integer=" + NETCONF_IDLE_TIMEOUT_DEFAULT, |
| 106 | SSH_LIBRARY + "=" + SSH_LIBRARY_DEFAULT, |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 107 | SSH_KEY_PATH + "=" + SSH_KEY_PATH_DEFAULT, |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 108 | }) |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 109 | public class NetconfControllerImpl implements NetconfController { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 110 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 111 | /** Time (in seconds) to wait for a NETCONF connect. */ |
| 112 | protected static int netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 113 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 114 | /** Time (in seconds) waiting for a NetConf reply. */ |
| 115 | protected static int netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 116 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 117 | /** Time (in seconds) SSH session will close if no traffic seen. */ |
| 118 | protected static int netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 119 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 120 | /** SSH client library to use. */ |
| 121 | protected static String sshLibrary = SSH_LIBRARY_DEFAULT; |
| 122 | |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 123 | /** Private SSH Key File Path to use. */ |
| 124 | protected static String sshKeyPath = SSH_KEY_PATH_DEFAULT; |
| 125 | |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 126 | protected NetconfSshClientLib sshClientLib = NetconfSshClientLib.APACHE_MINA; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 127 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 128 | private static final MessageSubject SEND_REQUEST_SUBJECT_STRING = |
| 129 | new MessageSubject("netconf-session-master-send-message-string"); |
| 130 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 131 | private static final MessageSubject SEND_REPLY_SUBJECT_STRING = |
| 132 | new MessageSubject("netconf-session-master-send-reply-message-string"); |
| 133 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 134 | private static final MessageSubject SEND_REQUEST_SUBJECT_SET_STRING = |
| 135 | new MessageSubject("netconf-session-master-send-message-set-string"); |
| 136 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 137 | private static final MessageSubject SEND_REPLY_SUBJECT_SET_STRING = |
| 138 | new MessageSubject("netconf-session-master-send-reply-message-set-string"); |
| 139 | |
| 140 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 142 | protected ComponentConfigService cfgService; |
| 143 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 144 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 145 | protected DeviceService deviceService; |
| 146 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 147 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 148 | protected DeviceKeyService deviceKeyService; |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 149 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 150 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 151 | protected NetworkConfigRegistry netCfgService; |
| 152 | |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 153 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 154 | protected MastershipService mastershipService; |
| 155 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 156 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 157 | protected ClusterCommunicationService clusterCommunicator; |
| 158 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 159 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 160 | protected ClusterService clusterService; |
| 161 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 162 | public static final Logger log = LoggerFactory |
| 163 | .getLogger(NetconfControllerImpl.class); |
| 164 | |
Andrea Campanella | 8b1cb67 | 2016-01-25 13:58:58 -0800 | [diff] [blame] | 165 | private Map<DeviceId, NetconfDevice> netconfDeviceMap = new ConcurrentHashMap<>(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 166 | |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 167 | private Map<DeviceId, Lock> netconfCreateMutex = new ConcurrentHashMap<>(); |
| 168 | |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 169 | private final NetconfDeviceOutputEventListener downListener = new DeviceDownEventListener(); |
| 170 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 171 | protected Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 172 | protected NetconfDeviceFactory deviceFactory = new DefaultNetconfDeviceFactory(); |
| 173 | |
| 174 | protected NetconfProxyMessageHandler netconfProxyMessageHandler = new NetconfProxyMessageHandlerImpl(); |
| 175 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 176 | |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 177 | protected final ExecutorService executor = |
| 178 | Executors.newCachedThreadPool(groupedThreads("onos/netconfdevicecontroller", |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 179 | "connection-reopen-%d", log)); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 180 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 181 | private final ExecutorService remoteRequestExecutor = |
| 182 | Executors.newCachedThreadPool(); |
| 183 | |
| 184 | protected NodeId localNodeId; |
| 185 | |
| 186 | private CountDownLatch countDownLatch; |
| 187 | |
| 188 | private ArrayList<String> replyArguments = new ArrayList<>(); |
| 189 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 190 | public static final Serializer SERIALIZER = Serializer.using( |
| 191 | KryoNamespace.newBuilder() |
| 192 | .register(KryoNamespaces.API) |
| 193 | .register(NetconfProxyMessage.class) |
| 194 | .register(NetconfProxyMessage.SubjectType.class) |
| 195 | .register(DefaultNetconfProxyMessage.class) |
| 196 | .register(String.class) |
| 197 | .build("NetconfProxySession")); |
| 198 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 199 | @Activate |
| 200 | public void activate(ComponentContext context) { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 201 | cfgService.registerProperties(getClass()); |
| 202 | modified(context); |
Yuta HIGUCHI | e976174 | 2017-09-10 15:10:19 -0700 | [diff] [blame] | 203 | Security.addProvider(new BouncyCastleProvider()); |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 204 | clusterCommunicator.<NetconfProxyMessage>addSubscriber( |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 205 | SEND_REQUEST_SUBJECT_STRING, |
| 206 | SERIALIZER::decode, |
| 207 | this::handleProxyMessage, |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 208 | remoteRequestExecutor); |
| 209 | clusterCommunicator.<NetconfProxyMessage>addSubscriber( |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 210 | SEND_REQUEST_SUBJECT_SET_STRING, |
| 211 | SERIALIZER::decode, |
| 212 | this::handleProxyMessage, |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 213 | remoteRequestExecutor); |
| 214 | clusterCommunicator.<NetconfProxyMessage>addSubscriber( |
| 215 | SEND_REPLY_SUBJECT_STRING, |
| 216 | SERIALIZER::decode, |
| 217 | this::handleProxyReplyMessage, |
| 218 | remoteRequestExecutor); |
| 219 | clusterCommunicator.<NetconfProxyMessage>addSubscriber( |
| 220 | SEND_REPLY_SUBJECT_SET_STRING, |
| 221 | SERIALIZER::decode, |
| 222 | this::handleProxyReplyMessage, |
| 223 | remoteRequestExecutor); |
| 224 | |
| 225 | localNodeId = Optional.ofNullable(clusterService.getLocalNode()) |
| 226 | .map(ControllerNode::id) |
| 227 | .orElseGet(() -> new NodeId("nullNodeId")); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 228 | log.info("Started"); |
| 229 | } |
| 230 | |
| 231 | @Deactivate |
| 232 | public void deactivate() { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 233 | netconfDeviceMap.values().forEach(device -> { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 234 | if (device.isMasterSession()) { |
| 235 | try { |
| 236 | device.getSession().removeDeviceOutputListener(downListener); |
| 237 | } catch (NetconfException e) { |
| 238 | log.error("removeDeviceOutputListener Failed {}", e.getMessage()); |
| 239 | } |
| 240 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 241 | device.disconnect(); |
| 242 | }); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 243 | clusterCommunicator.removeSubscriber(SEND_REQUEST_SUBJECT_STRING); |
| 244 | clusterCommunicator.removeSubscriber(SEND_REQUEST_SUBJECT_SET_STRING); |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 245 | clusterCommunicator.removeSubscriber(SEND_REPLY_SUBJECT_STRING); |
| 246 | clusterCommunicator.removeSubscriber(SEND_REPLY_SUBJECT_SET_STRING); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 247 | cfgService.unregisterProperties(getClass(), false); |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 248 | netconfDeviceListeners.clear(); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 249 | netconfDeviceMap.clear(); |
Holger Schulz | 092cbbf | 2017-08-31 17:52:30 +0200 | [diff] [blame] | 250 | Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 251 | log.info("Stopped"); |
| 252 | } |
| 253 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 254 | @Modified |
| 255 | public void modified(ComponentContext context) { |
| 256 | if (context == null) { |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 257 | netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
| 258 | netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
| 259 | netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
| 260 | sshLibrary = SSH_LIBRARY_DEFAULT; |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 261 | sshKeyPath = SSH_KEY_PATH_DEFAULT; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 262 | sshClientLib = NetconfSshClientLib.APACHE_MINA; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 263 | log.info("No component configuration"); |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | Dictionary<?, ?> properties = context.getProperties(); |
| 268 | |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 269 | String newSshLibrary; |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 270 | String newSshKeyPath; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 271 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 272 | int newNetconfReplyTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 273 | properties, NETCONF_REPLY_TIMEOUT, netconfReplyTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 274 | int newNetconfConnectTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 275 | properties, NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 276 | int newNetconfIdleTimeout = getIntegerProperty( |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 277 | properties, NETCONF_IDLE_TIMEOUT, netconfIdleTimeout); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 278 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 279 | newSshLibrary = get(properties, SSH_LIBRARY); |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 280 | newSshKeyPath = get(properties, SSH_KEY_PATH); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 281 | |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 282 | if (newNetconfConnectTimeout < 0) { |
| 283 | log.warn("netconfConnectTimeout is invalid - less than 0"); |
| 284 | return; |
| 285 | } else if (newNetconfReplyTimeout <= 0) { |
| 286 | log.warn("netconfReplyTimeout is invalid - 0 or less."); |
| 287 | return; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 288 | } else if (newNetconfIdleTimeout <= 0) { |
| 289 | log.warn("netconfIdleTimeout is invalid - 0 or less."); |
| 290 | return; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 293 | netconfReplyTimeout = newNetconfReplyTimeout; |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 294 | netconfConnectTimeout = newNetconfConnectTimeout; |
Sean Condon | 7347de9 | 2017-07-21 12:17:25 +0100 | [diff] [blame] | 295 | netconfIdleTimeout = newNetconfIdleTimeout; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 296 | if (newSshLibrary != null) { |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 297 | sshLibrary = newSshLibrary; |
| 298 | sshClientLib = NetconfSshClientLib.getEnum(newSshLibrary); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 299 | } |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 300 | if (newSshKeyPath != null) { |
| 301 | sshKeyPath = newSshKeyPath; |
| 302 | } |
| 303 | log.info("Settings: {} = {}, {} = {}, {} = {}, {} = {}, {} = {}", |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 304 | NETCONF_REPLY_TIMEOUT, netconfReplyTimeout, |
| 305 | NETCONF_CONNECT_TIMEOUT, netconfConnectTimeout, |
| 306 | NETCONF_IDLE_TIMEOUT, netconfIdleTimeout, |
Anurag Chadha | 4673237 | 2021-05-10 17:32:59 +0530 | [diff] [blame] | 307 | SSH_LIBRARY, sshLibrary, |
| 308 | SSH_KEY_PATH, sshKeyPath); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 309 | } |
| 310 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 311 | @Override |
| 312 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 313 | if (!netconfDeviceListeners.contains(listener)) { |
| 314 | netconfDeviceListeners.add(listener); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 320 | netconfDeviceListeners.remove(listener); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public NetconfDevice getNetconfDevice(DeviceId deviceInfo) { |
| 325 | return netconfDeviceMap.get(deviceInfo); |
| 326 | } |
| 327 | |
| 328 | @Override |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 329 | public NetconfDevice getNetconfDevice(IpAddress ip, int port, String path) { |
| 330 | return getNetconfDevice(DeviceId.deviceId( |
| 331 | String.format("netconf:%s:%d%s", |
| 332 | ip.toString(), port, (path != null && !path.isEmpty() ? "/" + path : "")))); |
| 333 | } |
| 334 | |
| 335 | @Override |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 336 | public NetconfDevice getNetconfDevice(IpAddress ip, int port) { |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame] | 337 | return getNetconfDevice(ip, port, null); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 341 | public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 342 | return connectDevice(deviceId, true); |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public NetconfDevice connectDevice(DeviceId deviceId, boolean isMaster) throws NetconfException { |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 347 | NetconfDeviceConfig netCfg = netCfgService.getConfig( |
| 348 | deviceId, NetconfDeviceConfig.class); |
| 349 | NetconfDeviceInfo deviceInfo = null; |
| 350 | |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 351 | /* |
| 352 | * A bit of an ugly race condition can be found here. It is possible |
| 353 | * that this method is called to create a connection to device A and |
| 354 | * while that device is in the process of being created another call |
| 355 | * to this method for A will be invoked. Since the first call to |
| 356 | * create A has not been completed device A is not in the the |
| 357 | * netconfDeviceMap yet. |
| 358 | * |
| 359 | * To prevent this situation a mutex is introduced so that the first |
| 360 | * call will be allowed to complete before the second is processed. |
| 361 | * The mutex is based on the device ID, so that it should be still |
| 362 | * possible to connect to different devices concurrently. |
| 363 | */ |
| 364 | Lock mutex; |
| 365 | synchronized (netconfCreateMutex) { |
| 366 | mutex = netconfCreateMutex.get(deviceId); |
| 367 | if (mutex == null) { |
| 368 | mutex = new ReentrantLock(); |
| 369 | netconfCreateMutex.put(deviceId, mutex); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 370 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 371 | } |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 372 | mutex.lock(); |
| 373 | try { |
| 374 | if (netconfDeviceMap.containsKey(deviceId)) { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 375 | //If not master or already has session: return, otherwise create device again. |
| 376 | if (!isMaster || netconfDeviceMap.get(deviceId).isMasterSession()) { |
| 377 | log.debug("Device {} is already present", deviceId); |
| 378 | return netconfDeviceMap.get(deviceId); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (netCfg != null) { |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 383 | log.debug("Device {} is present in NetworkConfig", deviceId); |
Anurag Chadha | b6a6bcb | 2021-03-26 14:43:41 +0530 | [diff] [blame] | 384 | deviceInfo = new NetconfDeviceInfo(netCfg, deviceId); |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 385 | } else { |
| 386 | log.debug("Creating NETCONF device {}", deviceId); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 387 | deviceInfo = createDeviceInfo(deviceId); |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 388 | } |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 389 | NetconfDevice netconfDevice = createDevice(deviceInfo, isMaster); |
| 390 | if (isMaster) { |
| 391 | netconfDevice.getSession().addDeviceOutputListener(downListener); |
| 392 | } |
| 393 | return netconfDevice; |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 394 | } finally { |
Andrea Campanella | c535b67 | 2019-02-25 16:25:35 +0100 | [diff] [blame] | 395 | |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 396 | mutex.unlock(); |
| 397 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 398 | } |
| 399 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 400 | @Override |
| 401 | public NodeId getLocalNodeId() { |
| 402 | return localNodeId; |
| 403 | } |
| 404 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 405 | private NetconfDeviceInfo createDeviceInfo(DeviceId deviceId) throws NetconfException { |
| 406 | Device device = deviceService.getDevice(deviceId); |
| 407 | String ip, path = null; |
| 408 | int port; |
| 409 | if (device != null) { |
| 410 | ip = device.annotations().value("ipaddress"); |
| 411 | port = Integer.parseInt(device.annotations().value("port")); |
| 412 | } else { |
| 413 | Triple<String, Integer, Optional<String>> info = extractIpPortPath(deviceId); |
| 414 | ip = info.getLeft(); |
| 415 | port = info.getMiddle(); |
| 416 | path = (info.getRight().isPresent() ? info.getRight().get() : null); |
| 417 | } |
| 418 | try { |
| 419 | DeviceKey deviceKey = deviceKeyService.getDeviceKey( |
| 420 | DeviceKeyId.deviceKeyId(deviceId.toString())); |
| 421 | if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) { |
| 422 | UsernamePassword usernamepasswd = deviceKey.asUsernamePassword(); |
| 423 | |
| 424 | return new NetconfDeviceInfo(usernamepasswd.username(), |
| 425 | usernamepasswd.password(), |
| 426 | IpAddress.valueOf(ip), |
| 427 | port, |
| 428 | path); |
| 429 | |
| 430 | } else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) { |
| 431 | String username = deviceKey.annotations().value(AnnotationKeys.USERNAME); |
| 432 | String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD); |
| 433 | String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY); |
| 434 | |
| 435 | return new NetconfDeviceInfo(username, |
| 436 | password, |
| 437 | IpAddress.valueOf(ip), |
| 438 | port, |
| 439 | path, |
| 440 | sshkey); |
| 441 | } else { |
| 442 | log.error("Unknown device key for device {}", deviceId); |
| 443 | throw new NetconfException("Unknown device key for device " + deviceId); |
| 444 | } |
| 445 | } catch (NullPointerException e) { |
| 446 | log.error("No Device Key for device {}, {}", deviceId, e); |
| 447 | throw new NetconfException("No Device Key for device " + deviceId, e); |
| 448 | } |
| 449 | } |
| 450 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 451 | @Override |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 452 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
| 453 | if (!netconfDeviceMap.containsKey(deviceId)) { |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 454 | log.debug("Device {} is not present", deviceId); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 455 | } else { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 456 | stopDevice(deviceId, remove); |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 460 | private void stopDevice(DeviceId deviceId, boolean remove) { |
David K. Bainbridge | 9b582b0 | 2019-02-01 16:04:05 -0800 | [diff] [blame] | 461 | Lock mutex; |
| 462 | synchronized (netconfCreateMutex) { |
| 463 | mutex = netconfCreateMutex.remove(deviceId); |
| 464 | } |
| 465 | NetconfDevice nc; |
| 466 | if (mutex == null) { |
| 467 | log.warn("Unexpected stoping a device that has no lock"); |
| 468 | nc = netconfDeviceMap.remove(deviceId); |
| 469 | } else { |
| 470 | mutex.lock(); |
| 471 | try { |
| 472 | nc = netconfDeviceMap.remove(deviceId); |
| 473 | } finally { |
| 474 | mutex.unlock(); |
| 475 | } |
| 476 | } |
David K. Bainbridge | 1070de9 | 2019-01-29 22:23:11 -0800 | [diff] [blame] | 477 | if (nc != null) { |
| 478 | nc.disconnect(); |
| 479 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 480 | if (remove) { |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 481 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 482 | l.deviceRemoved(deviceId); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public void removeDevice(DeviceId deviceId) { |
| 489 | if (!netconfDeviceMap.containsKey(deviceId)) { |
| 490 | log.warn("Device {} is not present", deviceId); |
| 491 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 492 | l.deviceRemoved(deviceId); |
| 493 | } |
| 494 | } else { |
zhongguo zhao | 78eab37 | 2018-08-27 16:22:39 +0800 | [diff] [blame] | 495 | stopDevice(deviceId, true); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Andrea Campanella | 101417d | 2015-12-11 17:58:07 -0800 | [diff] [blame] | 499 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 500 | return createDevice(deviceInfo, true); |
| 501 | } |
| 502 | |
| 503 | private NetconfDevice createDevice(NetconfDeviceInfo deviceInfo, |
| 504 | boolean isMaster) throws NetconfException { |
| 505 | NetconfDevice netconfDevice = deviceFactory.createNetconfDevice(deviceInfo, isMaster); |
Andrea Campanella | 087ceb9 | 2015-12-07 09:58:34 -0800 | [diff] [blame] | 506 | netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 507 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 508 | l.deviceAdded(deviceInfo.getDeviceId()); |
| 509 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 510 | return netconfDevice; |
| 511 | } |
| 512 | |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 513 | |
| 514 | @Override |
| 515 | public Map<DeviceId, NetconfDevice> getDevicesMap() { |
| 516 | return netconfDeviceMap; |
| 517 | } |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 518 | |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 519 | @Override |
| 520 | public Set<DeviceId> getNetconfDevices() { |
| 521 | return netconfDeviceMap.keySet(); |
| 522 | } |
| 523 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 524 | private void unicastRpcToMaster(NetconfProxyMessage proxyMessage, NodeId receiverId) { |
| 525 | MessageSubject messageSubject; |
| 526 | |
| 527 | switch (proxyMessage.subjectType()) { |
| 528 | case GET_DEVICE_CAPABILITIES_SET: |
| 529 | messageSubject = SEND_REQUEST_SUBJECT_SET_STRING; |
| 530 | break; |
| 531 | default: |
| 532 | messageSubject = SEND_REQUEST_SUBJECT_STRING; |
| 533 | break; |
| 534 | } |
| 535 | |
| 536 | clusterCommunicator |
| 537 | .unicast(proxyMessage, |
| 538 | messageSubject, |
| 539 | SERIALIZER::encode, |
| 540 | receiverId); |
| 541 | } |
| 542 | |
| 543 | private void unicastReplyToSender(NetconfProxyMessage proxyMessage, NodeId receiverId) { |
| 544 | MessageSubject messageSubject; |
| 545 | |
| 546 | switch (proxyMessage.subjectType()) { |
| 547 | case GET_DEVICE_CAPABILITIES_SET: |
| 548 | messageSubject = SEND_REPLY_SUBJECT_SET_STRING; |
| 549 | break; |
| 550 | default: |
| 551 | messageSubject = SEND_REPLY_SUBJECT_STRING; |
| 552 | break; |
| 553 | } |
| 554 | |
| 555 | clusterCommunicator |
| 556 | .unicast(proxyMessage, |
| 557 | messageSubject, |
| 558 | SERIALIZER::encode, |
| 559 | receiverId); |
| 560 | } |
| 561 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 562 | @Override |
| 563 | public <T> CompletableFuture<T> executeAtMaster(NetconfProxyMessage proxyMessage) throws NetconfException { |
| 564 | DeviceId deviceId = proxyMessage.deviceId(); |
| 565 | if (deviceService.getRole(deviceId).equals(MastershipRole.MASTER)) { |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 566 | return handleProxyMessage(proxyMessage); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 567 | } else { |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 568 | return relayMessageToMaster(proxyMessage); |
| 569 | } |
| 570 | } |
| 571 | |
Anurag Chadha | 924f01f | 2020-12-04 19:48:56 +0530 | [diff] [blame] | 572 | @Override |
| 573 | public <T> boolean pingDevice(DeviceId deviceId) { |
| 574 | NetconfProxyMessage proxyMessage = new DefaultNetconfProxyMessage( |
| 575 | NetconfProxyMessage.SubjectType.GET_DEVICE_CAPABILITIES_SET, deviceId, null, localNodeId); |
| 576 | CompletableFuture<T> reply; |
| 577 | if (deviceService.getRole(deviceId).equals(MastershipRole.MASTER)) { |
| 578 | reply = handleProxyMessage(proxyMessage); |
| 579 | } else { |
| 580 | reply = relayMessageToMaster(proxyMessage); |
| 581 | } |
| 582 | try { |
| 583 | T deviceCapabilities = reply.get(); |
| 584 | log.debug("Get device capabilities from device : {} -> {}", deviceId, deviceCapabilities); |
| 585 | } catch (InterruptedException | ExecutionException e) { |
| 586 | log.error("Error while getting device capabilities for device : {}", deviceId); |
| 587 | log.error("Error details : ", e); |
| 588 | return false; |
| 589 | } |
| 590 | return true; |
| 591 | } |
| 592 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 593 | public <T> CompletableFuture<T> relayMessageToMaster(NetconfProxyMessage proxyMessage) { |
| 594 | DeviceId deviceId = proxyMessage.deviceId(); |
| 595 | |
| 596 | countDownLatch = new CountDownLatch(1); |
| 597 | unicastRpcToMaster(proxyMessage, mastershipService.getMasterFor(deviceId)); |
| 598 | |
| 599 | try { |
| 600 | countDownLatch.await(netconfReplyTimeout, TimeUnit.SECONDS); |
| 601 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 602 | switch (proxyMessage.subjectType()) { |
| 603 | case GET_DEVICE_CAPABILITIES_SET: |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 604 | Set<String> forReturnValue = new LinkedHashSet<>(replyArguments); |
| 605 | return CompletableFuture.completedFuture((T) forReturnValue); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 606 | default: |
Anurag Chadha | 01ab40a | 2020-07-13 13:38:08 +0530 | [diff] [blame] | 607 | String returnValue = null; |
| 608 | if (!replyArguments.isEmpty()) { |
| 609 | returnValue = Optional.ofNullable(replyArguments.get(0)).orElse(null); |
| 610 | } |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 611 | return CompletableFuture.completedFuture((T) returnValue); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 612 | } |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 613 | } catch (InterruptedException e) { |
| 614 | log.error("InterruptedOccured while awaiting because of {}", e); |
| 615 | CompletableFuture<T> errorFuture = new CompletableFuture<>(); |
| 616 | errorFuture.completeExceptionally(e); |
| 617 | return errorFuture; |
| 618 | } catch (Exception e) { |
| 619 | log.error("Exception occured because of {}", e); |
| 620 | CompletableFuture<T> errorFuture = new CompletableFuture<>(); |
| 621 | errorFuture.completeExceptionally(e); |
| 622 | return errorFuture; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 623 | } |
| 624 | } |
| 625 | |
| 626 | private <T> CompletableFuture<T> handleProxyMessage(NetconfProxyMessage proxyMessage) { |
Anurag Chadha | 924f01f | 2020-12-04 19:48:56 +0530 | [diff] [blame] | 627 | countDownLatch = new CountDownLatch(1); |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 628 | try { |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 629 | switch (proxyMessage.subjectType()) { |
| 630 | case GET_DEVICE_CAPABILITIES_SET: |
| 631 | return CompletableFuture.completedFuture( |
| 632 | (T) netconfProxyMessageHandler.handleIncomingSetMessage(proxyMessage)); |
| 633 | default: |
| 634 | return CompletableFuture.completedFuture( |
| 635 | netconfProxyMessageHandler.handleIncomingMessage(proxyMessage)); |
| 636 | } |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 637 | } catch (NetconfException e) { |
| 638 | CompletableFuture<T> errorFuture = new CompletableFuture<>(); |
| 639 | errorFuture.completeExceptionally(e); |
| 640 | return errorFuture; |
| 641 | } |
| 642 | } |
| 643 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 644 | private <T> CompletableFuture<T> handleProxyReplyMessage(NetconfProxyMessage replyMessage) { |
| 645 | try { |
| 646 | switch (replyMessage.subjectType()) { |
| 647 | case GET_DEVICE_CAPABILITIES_SET: |
| 648 | return CompletableFuture.completedFuture( |
| 649 | (T) netconfProxyMessageHandler.handleReplySetMessage(replyMessage)); |
| 650 | default: |
| 651 | return CompletableFuture.completedFuture( |
| 652 | netconfProxyMessageHandler.handleReplyMessage(replyMessage)); |
| 653 | |
| 654 | } |
| 655 | } catch (NetconfException e) { |
| 656 | CompletableFuture<T> errorFuture = new CompletableFuture<>(); |
| 657 | errorFuture.completeExceptionally(e); |
| 658 | return errorFuture; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 663 | /** |
| 664 | * Netconf Proxy Message Handler Implementation class. |
| 665 | */ |
| 666 | private class NetconfProxyMessageHandlerImpl implements NetconfProxyMessageHandler { |
| 667 | |
| 668 | @Override |
| 669 | public <T> T handleIncomingMessage(NetconfProxyMessage proxyMessage) throws NetconfException { |
| 670 | //TODO: Should throw Netconf Exception in error cases? |
| 671 | DeviceId deviceId = proxyMessage.deviceId(); |
| 672 | NetconfProxyMessage.SubjectType subjectType = proxyMessage.subjectType(); |
| 673 | NetconfSession secureTransportSession; |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 674 | |
Anurag Chadha | 924f01f | 2020-12-04 19:48:56 +0530 | [diff] [blame] | 675 | if (netconfDeviceMap.get(deviceId) != null && netconfDeviceMap.get(deviceId).isMasterSession()) { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 676 | secureTransportSession = netconfDeviceMap.get(deviceId).getSession(); |
| 677 | } else { |
| 678 | throw new NetconfException("Ssh session not present"); |
| 679 | } |
| 680 | T reply = null; |
| 681 | ArrayList<String> arguments = Lists.newArrayList(proxyMessage.arguments()); |
| 682 | try { |
| 683 | switch (subjectType) { |
| 684 | case RPC: |
| 685 | reply = (T) secureTransportSession.rpc(arguments.get(0)) |
| 686 | .get(netconfReplyTimeout, TimeUnit.SECONDS); |
| 687 | break; |
| 688 | case REQUEST_SYNC: |
| 689 | reply = (T) secureTransportSession.requestSync(arguments.get(0)); |
| 690 | break; |
| 691 | case START_SUBSCRIPTION: |
| 692 | secureTransportSession.startSubscription(arguments.get(0)); |
| 693 | break; |
| 694 | case END_SUBSCRIPTION: |
| 695 | secureTransportSession.endSubscription(); |
| 696 | break; |
| 697 | case REQUEST: |
| 698 | reply = (T) secureTransportSession.request(arguments.get(0)) |
| 699 | .get(netconfReplyTimeout, TimeUnit.SECONDS); |
| 700 | break; |
| 701 | case GET_SESSION_ID: |
| 702 | reply = (T) secureTransportSession.getSessionId(); |
| 703 | break; |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 704 | case GET_DEVICE_CAPABILITIES_SET: |
| 705 | reply = (T) secureTransportSession.getDeviceCapabilitiesSet(); |
| 706 | break; |
| 707 | default: |
| 708 | log.error("Not yet supported for session method {}", subjectType); |
| 709 | } |
| 710 | } catch (InterruptedException e) { |
| 711 | Thread.currentThread().interrupt(); |
| 712 | throw new NetconfException(e.getMessage(), e.getCause()); |
| 713 | } catch (ExecutionException | TimeoutException e) { |
| 714 | throw new NetconfException(e.getMessage(), e.getCause()); |
| 715 | } |
| 716 | |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 717 | ArrayList<String> returnArgument = new ArrayList<String>(); |
| 718 | Optional.ofNullable(reply).ifPresent(r -> returnArgument.add((String) r)); |
| 719 | |
| 720 | DefaultNetconfProxyMessage replyMessage = new DefaultNetconfProxyMessage( |
| 721 | subjectType, |
| 722 | deviceId, |
| 723 | returnArgument, |
| 724 | localNodeId); |
| 725 | |
| 726 | unicastReplyToSender(replyMessage, proxyMessage.senderId()); |
| 727 | |
| 728 | |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 729 | return reply; |
| 730 | } |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 731 | |
| 732 | @Override |
| 733 | public <T> T handleReplyMessage(NetconfProxyMessage replyMessage) { |
| 734 | replyArguments = new ArrayList<>(replyMessage.arguments()); |
| 735 | countDownLatch.countDown(); |
| 736 | return (T) Optional.ofNullable(replyArguments.get(0)).orElse(null); |
| 737 | } |
| 738 | |
| 739 | @Override |
| 740 | public Set<String> handleIncomingSetMessage(NetconfProxyMessage proxyMessage) throws NetconfException { |
| 741 | DeviceId deviceId = proxyMessage.deviceId(); |
| 742 | NetconfProxyMessage.SubjectType subjectType = proxyMessage.subjectType(); |
| 743 | NetconfSession secureTransportSession; |
| 744 | |
Anurag Chadha | 924f01f | 2020-12-04 19:48:56 +0530 | [diff] [blame] | 745 | if (netconfDeviceMap.get(deviceId) != null && netconfDeviceMap.get(deviceId).isMasterSession()) { |
gyewan.an | 3c99ee7 | 2019-02-18 15:53:55 +0900 | [diff] [blame] | 746 | secureTransportSession = netconfDeviceMap.get(deviceId).getSession(); |
| 747 | } else { |
| 748 | throw new NetconfException("SSH session not present"); |
| 749 | } |
| 750 | |
| 751 | Set<String> reply = secureTransportSession.getDeviceCapabilitiesSet(); |
| 752 | ArrayList<String> returnArgument = new ArrayList<String>(reply); |
| 753 | |
| 754 | DefaultNetconfProxyMessage replyMessage = new DefaultNetconfProxyMessage( |
| 755 | subjectType, |
| 756 | deviceId, |
| 757 | returnArgument, |
| 758 | localNodeId); |
| 759 | |
| 760 | unicastReplyToSender(replyMessage, proxyMessage.senderId()); |
| 761 | return reply; |
| 762 | } |
| 763 | |
| 764 | @Override |
| 765 | public Set<String> handleReplySetMessage(NetconfProxyMessage replyMessage) { |
| 766 | replyArguments = new ArrayList<>(replyMessage.arguments()); |
| 767 | countDownLatch.countDown(); |
| 768 | |
| 769 | return new LinkedHashSet<>(replyArguments); |
| 770 | |
| 771 | } |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 772 | } |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 773 | |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 774 | /** |
| 775 | * Device factory for the specific NetconfDeviceImpl. |
| 776 | * |
| 777 | * @deprecated in 1.14.0 |
| 778 | */ |
| 779 | @Deprecated |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 780 | private class DefaultNetconfDeviceFactory implements NetconfDeviceFactory { |
| 781 | |
| 782 | @Override |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 783 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException { |
| 784 | return createNetconfDevice(netconfDeviceInfo, true); |
| 785 | } |
| 786 | |
| 787 | @Beta |
| 788 | @Override |
| 789 | public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo, |
| 790 | boolean isMaster) |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 791 | throws NetconfException { |
gyewan.an | 91d7e7e | 2019-01-17 15:12:48 +0900 | [diff] [blame] | 792 | if (isMaster) { |
| 793 | log.info("Creating NETCONF session to {} with {}", |
| 794 | netconfDeviceInfo.getDeviceId(), NetconfSshClientLib.APACHE_MINA); |
| 795 | } |
| 796 | return new DefaultNetconfDevice(netconfDeviceInfo, isMaster, NetconfControllerImpl.this); |
Andrea Campanella | 950310c | 2016-02-12 18:14:38 -0800 | [diff] [blame] | 797 | } |
| 798 | } |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 799 | |
Yuta HIGUCHI | f708910 | 2017-05-03 10:31:46 -0700 | [diff] [blame] | 800 | //Listener for closed session with devices, gets triggered when devices goes down |
| 801 | // or sends the end pattern ]]>]]> |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 802 | private class DeviceDownEventListener implements NetconfDeviceOutputEventListener { |
| 803 | |
| 804 | @Override |
| 805 | public void event(NetconfDeviceOutputEvent event) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 806 | DeviceId did = event.getDeviceInfo().getDeviceId(); |
Andrea Campanella | a2a6c3c | 2018-12-11 12:56:38 +0100 | [diff] [blame] | 807 | if (event.type().equals(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED) || |
| 808 | !mastershipService.isLocalMaster(did)) { |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 809 | removeDevice(did); |
| 810 | } else if (event.type().equals(NetconfDeviceOutputEvent.Type.SESSION_CLOSED)) { |
| 811 | log.info("Trying to reestablish connection with device {}", did); |
| 812 | executor.execute(() -> { |
| 813 | try { |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 814 | NetconfDevice device = netconfDeviceMap.get(did); |
| 815 | if (device != null) { |
| 816 | device.getSession().checkAndReestablish(); |
| 817 | log.info("Connection with device {} was reestablished", did); |
Anurag Chadha | dbffc04 | 2022-02-09 21:12:55 +0530 | [diff] [blame^] | 818 | for (NetconfDeviceListener l : netconfDeviceListeners) { |
| 819 | l.netconfConnectionReestablished(did); |
| 820 | } |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 821 | } else { |
| 822 | log.warn("The device {} is not in the system", did); |
| 823 | } |
| 824 | |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 825 | } catch (NetconfException e) { |
| 826 | log.error("The SSH connection with device {} couldn't be " + |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 827 | "reestablished due to {}. " + |
Andrea Campanella | c535b67 | 2019-02-25 16:25:35 +0100 | [diff] [blame] | 828 | "Marking the device as unreachable", did, e.getMessage()); |
Andrea Campanella | c362784 | 2017-04-04 18:06:54 +0200 | [diff] [blame] | 829 | log.debug("Complete exception: ", e); |
| 830 | removeDevice(did); |
| 831 | } |
| 832 | }); |
Andrea Campanella | 2464dc3 | 2016-02-17 17:54:53 -0800 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
| 836 | @Override |
| 837 | public boolean isRelevant(NetconfDeviceOutputEvent event) { |
| 838 | return getDevicesMap().containsKey(event.getDeviceInfo().getDeviceId()); |
| 839 | } |
| 840 | } |
andrea | eb70a94 | 2015-10-16 21:34:46 -0700 | [diff] [blame] | 841 | } |