use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class PolatisAlarmConfig method translateAlarms.
@Override
public <T> Set<Alarm> translateAlarms(List<T> unparsedAlarms) {
deviceId = handler().data().deviceId();
Set<Alarm> alarms = new HashSet<>();
for (T alarm : unparsedAlarms) {
if (alarm instanceof NetconfDeviceOutputEvent) {
NetconfDeviceOutputEvent event = (NetconfDeviceOutputEvent) alarm;
if (event.type() == NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION) {
String message = event.getMessagePayload();
InputStream in = new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8));
try {
Document doc = XmlEventParser.createDocFromMessage(in);
long timeStamp = XmlEventParser.getEventTime(doc);
Node descriptionNode = XmlEventParser.getDescriptionNode(doc);
while (descriptionNode != null) {
if (descriptionNode.getNodeType() == Node.ELEMENT_NODE) {
String nodeName = descriptionNode.getNodeName();
if (nodeName.equals(ALARM_TYPE_LOS)) {
Node portIdNode = descriptionNode.getChildNodes().item(1);
String portId = portIdNode.getTextContent();
String description = "Loss of Service alarm raised for fibre " + portId;
alarms.add(new DefaultAlarm.Builder(AlarmId.alarmId(deviceId, description), deviceId, description, Alarm.SeverityLevel.MAJOR, timeStamp).build());
descriptionNode = null;
}
} else {
descriptionNode = descriptionNode.getNextSibling();
}
}
} catch (SAXException | IOException | ParserConfigurationException | UnsupportedOperationException | IllegalArgumentException e) {
log.error("Exception thrown translating message from {}.", deviceId, e);
}
}
}
}
return alarms;
}
use of org.onosproject.netconf.NetconfDeviceOutputEvent 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.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class NetconfSessionMinaImpl method sendRequest.
private String sendRequest(String request, boolean isHello, int timeout) throws NetconfException {
checkAndReestablish();
int messageId = -1;
if (!isHello) {
messageId = messageIdInteger.getAndIncrement();
}
// FIXME potentially re-writing chunked encoded String?
request = formatXmlHeader(request);
request = formatRequestMessageId(request, messageId);
int useTimeout = timeout > 0 ? timeout : replyTimeout;
log.debug("Sending request to NETCONF with timeout {} for {}", useTimeout, deviceInfo.name());
CompletableFuture<String> futureReply = request(request, messageId);
String rp;
try {
rp = futureReply.get(useTimeout, TimeUnit.SECONDS);
// Why here???
replies.remove(messageId);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new NetconfException("Interrupted waiting for reply for request" + request, e);
} catch (TimeoutException e) {
throw new NetconfException("Timed out waiting for reply for request " + request + " after " + useTimeout + " sec.", e);
} catch (ExecutionException e) {
log.warn("Closing session {} for {} due to unexpected Error", sessionID, deviceInfo, e);
stopClient();
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.SESSION_CLOSED, null, "Closed due to unexpected error " + e.getCause(), Optional.of(-1), deviceInfo);
publishEvent(event);
// move to cleanUp()?
errorReplies.clear();
cleanUp();
throw new NetconfException("Closing session " + sessionID + " for " + deviceInfo + " for request " + request, e);
}
log.debug("Result {} from request {} to device {}", rp, request, deviceInfo);
return rp.trim();
}
use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class NetconfStreamThread method close.
private void close(String deviceReply) {
log.debug("Netconf device {} socketClosed = true DEVICE_UNREGISTERED {}", netconfDeviceInfo, deviceReply);
if (!deviceReply.equals(ON_REQUEST)) {
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, null, Optional.of(-1), netconfDeviceInfo);
netconfDeviceEventListeners.forEach(listener -> listener.event(event));
}
this.interrupt();
}
use of org.onosproject.netconf.NetconfDeviceOutputEvent in project onos by opennetworkinglab.
the class NetconfStreamThread method dealWithReply.
private void dealWithReply(String deviceReply) {
if (deviceReply.contains(RPC_REPLY) || deviceReply.contains(RPC_ERROR) || deviceReply.contains(HELLO)) {
log.debug("Netconf device {} sessionDelegate.notify() DEVICE_REPLY {} {}", netconfDeviceInfo, getMsgId(deviceReply), deviceReply);
NetconfDeviceOutputEvent event = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_REPLY, null, deviceReply, getMsgId(deviceReply), netconfDeviceInfo);
sessionDelegate.notify(event);
netconfDeviceEventListeners.forEach(listener -> listener.event(event));
} else if (deviceReply.contains(NOTIFICATION_LABEL)) {
log.debug("Netconf device {} DEVICE_NOTIFICATION {} {} {}", netconfDeviceInfo, enableNotifications, getMsgId(deviceReply), deviceReply);
if (enableNotifications) {
log.debug("dispatching to {} listeners", netconfDeviceEventListeners.size());
final String finalDeviceReply = deviceReply;
netconfDeviceEventListeners.forEach(listener -> listener.event(new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, finalDeviceReply, getMsgId(finalDeviceReply), netconfDeviceInfo)));
}
} else {
log.debug("Error on reply from device {} {}", netconfDeviceInfo, deviceReply);
}
}
Aggregations