use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.
the class NetconfControllerImplTest method setUp.
@Before
public void setUp() throws Exception {
ctrl = new NetconfControllerImpl();
ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
ctrl.cfgService = cfgService;
ctrl.deviceService = deviceService;
ctrl.deviceKeyService = deviceKeyService;
ctrl.netCfgService = netCfgService;
ctrl.mastershipService = mastershipService;
NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
ctrl.clusterCommunicator = clusterCommunicationService;
ctrl.clusterService = mockClusterService;
// Creating mock devices
deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
// Create a JSON entry just like Network Config accepts
ObjectMapper mapper = new ObjectMapper();
String jsonMessage = "{\n" + " \"ip\":\"" + DEVICE_10_IP + "\",\n" + " \"port\":" + DEVICE_10_PORT + ",\n" + " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" + " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" + " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" + " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" + " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" + " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" + "}";
InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
JsonNode jsonNode = mapper.readTree(jsonStream);
jsonStream.close();
ConfigApplyDelegate delegate = new MockDelegate();
deviceConfig10 = new NetconfDeviceConfig();
deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
device1 = new TestNetconfDevice(deviceInfo1);
deviceId1 = deviceInfo1.getDeviceId();
device2 = new TestNetconfDevice(deviceInfo2);
deviceId2 = deviceInfo2.getDeviceId();
// Adding to the map for testing get device calls.
Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
field1.setAccessible(true);
reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
reflectedDeviceMap.put(deviceId1, device1);
reflectedDeviceMap.put(deviceId2, device2);
// Creating mock events for testing NetconfDeviceOutputEventListener
Field field2 = ctrl.getClass().getDeclaredField("downListener");
field2.setAccessible(true);
reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, null, Optional.of(1), deviceInfo1);
eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, null, Optional.of(2), deviceInfo2);
}
use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.
the class NetconfDeviceProvider method initiateConnection.
// initiating the SSh connection the a given device.
private void initiateConnection(DeviceId deviceId) {
if (!isReachable(deviceId)) {
log.warn("Can't connect to device {}", deviceId);
return;
}
try {
NetconfDevice deviceNetconf = controller.connectDevice(deviceId);
if (deviceNetconf != null) {
// storeDeviceKey(config.sshKey(), config.username(), config.password(), deviceId);
NetconfDeviceConfig config = cfgService.getConfig(deviceId, NetconfDeviceConfig.class);
// getting the device description
DeviceDescription deviceDescription = getDeviceDescription(deviceId, config);
// connecting device to ONOS
log.debug("Connected NETCONF device {}, on {}:{} {} with username {}", deviceId, config.ip(), config.port(), (config.path().isPresent() ? "/" + config.path().get() : ""), config.username());
providerService.deviceConnected(deviceId, deviceDescription);
} else {
mastershipService.relinquishMastership(deviceId);
deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
log.error("Can't connect to NETCONF device {}", deviceId);
}
} catch (Exception e) {
mastershipService.relinquishMastership(deviceId);
deviceKeyAdminService.removeKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
throw new IllegalStateException(new NetconfException("Can't connect to NETCONF device " + deviceId, e));
}
}
use of org.onosproject.netconf.config.NetconfDeviceConfig in project onos by opennetworkinglab.
the class NetconfControllerImpl method connectDevice.
@Override
public NetconfDevice connectDevice(DeviceId deviceId, boolean isMaster) throws NetconfException {
NetconfDeviceConfig netCfg = netCfgService.getConfig(deviceId, NetconfDeviceConfig.class);
NetconfDeviceInfo deviceInfo = null;
/*
* A bit of an ugly race condition can be found here. It is possible
* that this method is called to create a connection to device A and
* while that device is in the process of being created another call
* to this method for A will be invoked. Since the first call to
* create A has not been completed device A is not in the the
* netconfDeviceMap yet.
*
* To prevent this situation a mutex is introduced so that the first
* call will be allowed to complete before the second is processed.
* The mutex is based on the device ID, so that it should be still
* possible to connect to different devices concurrently.
*/
Lock mutex;
synchronized (netconfCreateMutex) {
mutex = netconfCreateMutex.get(deviceId);
if (mutex == null) {
mutex = new ReentrantLock();
netconfCreateMutex.put(deviceId, mutex);
}
}
mutex.lock();
try {
if (netconfDeviceMap.containsKey(deviceId)) {
// If not master or already has session: return, otherwise create device again.
if (!isMaster || netconfDeviceMap.get(deviceId).isMasterSession()) {
log.debug("Device {} is already present", deviceId);
return netconfDeviceMap.get(deviceId);
}
}
if (netCfg != null) {
log.debug("Device {} is present in NetworkConfig", deviceId);
deviceInfo = new NetconfDeviceInfo(netCfg, deviceId);
} else {
log.debug("Creating NETCONF device {}", deviceId);
deviceInfo = createDeviceInfo(deviceId);
}
NetconfDevice netconfDevice = createDevice(deviceInfo, isMaster);
if (isMaster) {
netconfDevice.getSession().addDeviceOutputListener(downListener);
}
return netconfDevice;
} finally {
mutex.unlock();
}
}
Aggregations