use of org.onosproject.netconf.NetconfDevice 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.NetconfDevice in project onos by opennetworkinglab.
the class NetconfControllerImplTest method testGetNetconfDevice.
@Test
public void testGetNetconfDevice() {
NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
}
use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.
the class NetconfGetConfigCommand method doExecute.
@Override
protected void doExecute() {
deviceId = DeviceId.deviceId(uri);
NetconfController controller = get(NetconfController.class);
checkNotNull(controller, "Netconf controller is null");
NetconfDevice device = controller.getDevicesMap().get(deviceId);
if (device == null) {
print("Netconf device object not found for %s", deviceId);
return;
}
NetconfSession session = device.getSession();
if (session == null) {
print("Netconf session not found for %s", deviceId);
return;
}
try {
CharSequence res = session.asyncGetConfig(datastore(datastore.toLowerCase())).get(timeoutSec, TimeUnit.SECONDS);
print("%s", res);
} catch (NetconfException | InterruptedException | ExecutionException | TimeoutException e) {
log.error("Configuration could not be retrieved", e);
print("Error occurred retrieving configuration");
}
}
use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.
the class NetconfControllerImpl method stopDevice.
private void stopDevice(DeviceId deviceId, boolean remove) {
Lock mutex;
synchronized (netconfCreateMutex) {
mutex = netconfCreateMutex.remove(deviceId);
}
NetconfDevice nc;
if (mutex == null) {
log.warn("Unexpected stoping a device that has no lock");
nc = netconfDeviceMap.remove(deviceId);
} else {
mutex.lock();
try {
nc = netconfDeviceMap.remove(deviceId);
} finally {
mutex.unlock();
}
}
if (nc != null) {
nc.disconnect();
}
if (remove) {
for (NetconfDeviceListener l : netconfDeviceListeners) {
l.deviceRemoved(deviceId);
}
}
}
use of org.onosproject.netconf.NetconfDevice in project onos by opennetworkinglab.
the class NetconfAlarmProvider method deactivate.
@Deactivate
public void deactivate() {
providerRegistry.unregister(this);
idNotificationListenerMap.forEach((id, listener) -> {
NetconfDevice device = controller.getNetconfDevice(id);
if (device.isMasterSession()) {
try {
device.getSession().removeDeviceOutputListener(listener);
} catch (NetconfException e) {
log.error("RemoveDeviceOutputListener Error {}", e.getMessage());
}
}
});
controller.removeDeviceListener(deviceListener);
providerService = null;
log.info("NetconfAlarmProvider Stopped");
}
Aggregations