/* * Copyright © 2017 AT&T and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.transportpce.networkmodel.listeners; import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.concurrent.ExecutionException; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.AlarmNotification; import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener; import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.alarm.ProbableCause; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.ResourceType; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.Resource; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.CircuitPack; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Connection; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Degree; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Interface; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.InternalLink; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.PhysicalLink; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Port; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Service; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Shelf; import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Srg; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.alarmsuppression.rev171102.ServiceNodelist; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.alarmsuppression.rev171102.service.nodelist.Nodelist; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.alarmsuppression.rev171102.service.nodelist.nodelist.Nodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.alarmsuppression.rev171102.service.nodelist.nodelist.NodesBuilder; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AlarmNotificationListener implements OrgOpenroadmAlarmListener { private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener.class); private static final String PIPE = "|"; private final DataBroker dataBroker; public AlarmNotificationListener(DataBroker dataBroker) { this.dataBroker = dataBroker; } /** * Callback for alarm-notification. * * @param notification AlarmNotification object */ @Override public void onAlarmNotification(AlarmNotification notification) { List allNodeList = new ArrayList<>(); InstanceIdentifier serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class); try { ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction(); com.google.common.base.Optional serviceListObject = rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get(); if (serviceListObject.isPresent()) { for (Nodelist nodelist : serviceListObject.get().getNodelist()) { allNodeList.addAll(nodelist.getNodes()); } } } catch (InterruptedException | ExecutionException ex) { LOG.warn("Exception thrown while reading Logical Connection Point value from {} {}", ex); } StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId()).append(PIPE); sb.append(buildCause(notification.getProbableCause())); sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE) .append(notification.getType() != null ? notification.getType().toString() : "").append(PIPE) .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE) .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE) .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE); sb.append(buildType(notification)); String message = sb.toString(); Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build(); if (allNodeList.contains(build)) { LOG.info(message); } else { LOG.warn(message); } } private String buildCause(ProbableCause probableCause) { StringBuilder sb = new StringBuilder(); if (probableCause == null) { return "||||"; } sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE) .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "") .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "") .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "") .append(PIPE); return sb.toString(); } @SuppressWarnings("unchecked") private static Optional tryCastToParticularResource(Class resourceClass, Resource resource) { if (resource == null) { LOG.error("Resource is null."); } else if (!resourceClass.isInstance(resource)) { LOG.error("Resource implement different type than expected. Expected {}, actual {}.", resourceClass.getSimpleName(), resource.getClass().getSimpleName()); } else { return Optional.of((T) resource); } return Optional.empty(); } private static String buildType(AlarmNotification notification) { String circuitPack = ""; String connection = ""; String degree = ""; String iface = ""; String internalLink = ""; String physicalLink = ""; String service = ""; String shelf = ""; String sharedRiskGroup = ""; String port = ""; String portCircuitPack = ""; Resource resource = notification.getResource().getResource().getResource(); ResourceType wantedResourceType = notification.getResource().getResourceType(); switch (wantedResourceType.getType()) { case CircuitPack: Optional circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource); if (circuitPackOptional.isPresent()) { circuitPack = circuitPackOptional.get().getCircuitPackName(); } break; case Connection: Optional connectionOptional = tryCastToParticularResource(Connection.class, resource); if (connectionOptional.isPresent()) { connection = connectionOptional.get().getConnectionNumber(); } break; case Degree: Optional degreeOptional = tryCastToParticularResource(Degree.class, resource); if (degreeOptional.isPresent()) { degree = degreeOptional.get().getDegreeNumber().toString(); } break; case Interface: Optional interfaceOptional = tryCastToParticularResource(Interface.class, resource); if (interfaceOptional.isPresent()) { iface = interfaceOptional.get().getInterfaceName(); } break; case InternalLink: Optional internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource); if (internalLinkOptional.isPresent()) { internalLink = internalLinkOptional.get().getInternalLinkName(); } break; case PhysicalLink: Optional physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource); if (physicalLinkOptional.isPresent()) { physicalLink = physicalLinkOptional.get().getPhysicalLinkName(); } break; case Service: Optional serviceOptional = tryCastToParticularResource(Service.class, resource); if (serviceOptional.isPresent()) { service = serviceOptional.get().getServiceName(); } break; case Shelf: Optional shelfOptional = tryCastToParticularResource(Shelf.class, resource); if (shelfOptional.isPresent()) { shelf = shelfOptional.get().getShelfName(); } break; case SharedRiskGroup: Optional sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource); if (sharedRiskGroupOptional.isPresent()) { sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString(); } break; case Port: Optional portContainerOptional = tryCastToParticularResource(Port.class, resource); if (portContainerOptional.isPresent()) { port = portContainerOptional.get().getPort().getPortName(); portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName(); } break; default: LOG.warn("Unknown resource type {}", wantedResourceType); } StringBuilder sb = new StringBuilder(circuitPack); sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface); sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service); sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port); sb.append(PIPE).append(portCircuitPack); return sb.toString(); } }