use of org.onosproject.netconf.NetconfDeviceInfo in project onos by opennetworkinglab.
the class FujitsuNetconfControllerMock method setUp.
/**
* Sets up initial test environment.
*
* @param listener listener to be added
* @return driver handler
* @throws NetconfException when there is a problem
*/
public FujitsuDriverHandlerAdapter setUp(FujitsuNetconfSessionListenerTest listener) throws NetconfException {
try {
NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD, IpAddress.valueOf(VOLT_DEVICE_IP), VOLT_DEVICE_PORT);
NetconfDevice netconfDevice = connectDevice(deviceInfo.getDeviceId());
FujitsuNetconfSessionMock session = (FujitsuNetconfSessionMock) netconfDevice.getSession();
session.setListener(listener);
DeviceId deviceId = deviceInfo.getDeviceId();
DefaultDriver driver = new DefaultDriver(VOLT_DRIVER_NAME, new ArrayList<>(), "Fujitsu", "1.0", "1.0", ImmutableMap.of(), ImmutableMap.of());
DefaultDriverData driverData = new DefaultDriverData(driver, deviceId);
FujitsuDriverHandlerAdapter driverHandler;
driverHandler = new FujitsuDriverHandlerAdapter(driverData);
driverHandler.setUp(this);
return driverHandler;
} catch (NetconfException e) {
throw new NetconfException("Cannot create a device ", e);
}
}
use of org.onosproject.netconf.NetconfDeviceInfo 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.NetconfDeviceInfo in project onos by opennetworkinglab.
the class FujitsuNetconfControllerMock method connectDevice.
@Override
public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
if (netconfDeviceMap.containsKey(deviceId)) {
log.debug("Device {} is already present", deviceId);
return netconfDeviceMap.get(deviceId);
} else {
log.debug("Creating NETCONF device {}", deviceId);
String ip;
int port;
String[] info = deviceId.toString().split(":");
if (info.length == 3) {
ip = info[1];
port = Integer.parseInt(info[2]);
} else {
ip = Arrays.asList(info).stream().filter(el -> !el.equals(info[0]) && !el.equals(info[info.length - 1])).reduce((t, u) -> t + ":" + u).get();
log.debug("ip v6 {}", ip);
port = Integer.parseInt(info[info.length - 1]);
}
try {
NetconfDeviceInfo deviceInfo = new NetconfDeviceInfo(VOLT_DEVICE_USERNAME, VOLT_DEVICE_PASSWORD, IpAddress.valueOf(ip), port);
NetconfDevice netconfDevice = new FujitsuNetconfDeviceMock(deviceInfo);
netconfDeviceMap.put(deviceInfo.getDeviceId(), netconfDevice);
return netconfDevice;
} catch (NullPointerException e) {
throw new NetconfException("Cannot connect a device " + deviceId, e);
}
}
}
use of org.onosproject.netconf.NetconfDeviceInfo in project onos by opennetworkinglab.
the class MockNetconfController method connectDevice.
@Override
public NetconfDevice connectDevice(DeviceId deviceId) throws NetconfException {
NetconfDevice mockNetconfDevice = null;
String[] nameParts = deviceId.uri().toASCIIString().split(":");
IpAddress ipAddress = Ip4Address.valueOf(nameParts[1]);
int port = Integer.parseInt(nameParts[2]);
NetconfDeviceInfo ncdi = new NetconfDeviceInfo("mock", "mock", ipAddress, port, null);
mockNetconfDevice = new MockNetconfDevice(ncdi);
devicesMap.put(deviceId, mockNetconfDevice);
return mockNetconfDevice;
}
use of org.onosproject.netconf.NetconfDeviceInfo in project onos by opennetworkinglab.
the class NetconfControllerImpl method createDeviceInfo.
private NetconfDeviceInfo createDeviceInfo(DeviceId deviceId) throws NetconfException {
Device device = deviceService.getDevice(deviceId);
String ip, path = null;
int port;
if (device != null) {
ip = device.annotations().value("ipaddress");
port = Integer.parseInt(device.annotations().value("port"));
} else {
Triple<String, Integer, Optional<String>> info = extractIpPortPath(deviceId);
ip = info.getLeft();
port = info.getMiddle();
path = (info.getRight().isPresent() ? info.getRight().get() : null);
}
try {
DeviceKey deviceKey = deviceKeyService.getDeviceKey(DeviceKeyId.deviceKeyId(deviceId.toString()));
if (deviceKey.type() == DeviceKey.Type.USERNAME_PASSWORD) {
UsernamePassword usernamepasswd = deviceKey.asUsernamePassword();
return new NetconfDeviceInfo(usernamepasswd.username(), usernamepasswd.password(), IpAddress.valueOf(ip), port, path);
} else if (deviceKey.type() == DeviceKey.Type.SSL_KEY) {
String username = deviceKey.annotations().value(AnnotationKeys.USERNAME);
String password = deviceKey.annotations().value(AnnotationKeys.PASSWORD);
String sshkey = deviceKey.annotations().value(AnnotationKeys.SSHKEY);
return new NetconfDeviceInfo(username, password, IpAddress.valueOf(ip), port, path, sshkey);
} else {
log.error("Unknown device key for device {}", deviceId);
throw new NetconfException("Unknown device key for device " + deviceId);
}
} catch (NullPointerException e) {
log.error("No Device Key for device {}, {}", deviceId, e);
throw new NetconfException("No Device Key for device " + deviceId, e);
}
}
Aggregations