2 * Copyright © 2017 AT&T and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.transportpce.networkmodel.listeners;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.ServiceNodelist;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.Nodelist;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.Nodes;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.NodesBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.AlarmNotification;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.alarm.ProbableCause;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.ResourceType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.Resource;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.CircuitPack;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Connection;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Degree;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Interface;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.InternalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.PhysicalLink;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Port;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Service;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Shelf;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Srg;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class AlarmNotificationListener {
43 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener.class);
44 private static final String PIPE = "|";
45 private final DataBroker dataBroker;
47 public AlarmNotificationListener(DataBroker dataBroker) {
48 this.dataBroker = dataBroker;
51 public CompositeListener getCompositeListener() {
52 return new CompositeListener(Set.of(
53 new CompositeListener.Component<>(AlarmNotification.class, this::onAlarmNotification)));
57 * Callback for alarm-notification.
59 * @param notification AlarmNotification object
61 private void onAlarmNotification(AlarmNotification notification) {
62 List<Nodes> allNodeList = new ArrayList<>();
63 InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
64 try (ReadTransaction rtx = dataBroker.newReadOnlyTransaction()) {
65 Optional<ServiceNodelist> serviceListObject =
66 rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
67 if (serviceListObject.isPresent()) {
68 for (Nodelist nodelist : serviceListObject.orElseThrow().nonnullNodelist().values()) {
69 allNodeList.addAll(nodelist.nonnullNodes().values());
72 } catch (InterruptedException | ExecutionException ex) {
73 LOG.warn("Exception thrown while reading Logical Connection Point value: ", ex);
75 String message = String.join(PIPE,notification.getResource().getDevice().getNodeId(),
76 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
77 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
78 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
79 notification.getCircuitId() != null ? notification.getCircuitId() : "", buildType(notification));
81 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build();
82 if (allNodeList.contains(build)) {
83 LOG.info("onAlarmNotification: {}", message);
85 LOG.warn("onAlarmNotification: {}", message);
89 private String buildCause(ProbableCause probableCause) {
90 if (probableCause == null) {
93 return String.join(PIPE,
94 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
95 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
96 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
97 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
100 @SuppressWarnings("unchecked")
101 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
103 if (resource == null) {
104 LOG.error("Resource is null.");
105 return Optional.empty();
107 if (!resourceClass.isInstance(resource)) {
108 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
109 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
110 return Optional.empty();
112 return Optional.of((T) resource);
115 private static String buildType(AlarmNotification notification) {
116 String circuitPack = "";
117 String connection = "";
120 String internalLink = "";
121 String physicalLink = "";
124 String sharedRiskGroup = "";
126 String portCircuitPack = "";
128 Resource resource = notification.getResource().getResource().getResource();
129 ResourceType wantedResourceType = notification.getResource().getResourceType();
131 switch (wantedResourceType.getType()) {
133 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
134 if (circuitPackOptional.isPresent()) {
135 circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
140 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
141 if (connectionOptional.isPresent()) {
142 connection = connectionOptional.orElseThrow().getConnectionNumber();
147 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
148 if (degreeOptional.isPresent()) {
149 degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
154 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
155 if (interfaceOptional.isPresent()) {
156 iface = interfaceOptional.orElseThrow().getInterfaceName();
161 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
162 if (internalLinkOptional.isPresent()) {
163 internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
168 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
169 if (physicalLinkOptional.isPresent()) {
170 physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
175 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
176 if (serviceOptional.isPresent()) {
177 service = serviceOptional.orElseThrow().getServiceName();
182 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
183 if (shelfOptional.isPresent()) {
184 shelf = shelfOptional.orElseThrow().getShelfName();
188 case SharedRiskGroup:
189 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
190 if (sharedRiskGroupOptional.isPresent()) {
191 sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
196 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
197 if (portContainerOptional.isPresent()) {
198 port = portContainerOptional.orElseThrow().getPort().getPortName();
199 portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
204 LOG.warn("Unknown resource type {}", wantedResourceType);
206 return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
207 service, shelf, sharedRiskGroup, port, portCircuitPack);