Don't use NotificationListener (NodeRegistration)
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / AlarmNotificationListener221.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.networkmodel.listeners;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Optional;
13 import java.util.Set;
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.rev181019.AlarmNotification;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.alarm.ProbableCause;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.ResourceType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.Resource;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.CircuitPack;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Connection;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Degree;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Interface;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.InternalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.PhysicalLink;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Port;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Service;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Shelf;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Srg;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class AlarmNotificationListener221 {
42
43     private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener221.class);
44     private static final String PIPE = "|";
45     private final DataBroker dataBroker;
46
47     public AlarmNotificationListener221(DataBroker dataBroker) {
48         this.dataBroker = dataBroker;
49     }
50
51     public CompositeListener getCompositeListener() {
52         return new CompositeListener(Set.of(
53             new CompositeListener.Component<>(AlarmNotification.class, this::onAlarmNotification)));
54     }
55
56     /**
57      * Callback for alarm-notification.
58      *
59      * @param notification AlarmNotification object
60      */
61     private void onAlarmNotification(AlarmNotification notification) {
62         List<Nodes> allNodeList = new ArrayList<>();
63         InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
64         try {
65             ReadTransaction rtx = dataBroker.newReadOnlyTransaction();
66             Optional<ServiceNodelist> serviceListObject =
67                     rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
68             if (serviceListObject.isPresent()) {
69                 for (Nodelist nodelist : serviceListObject.orElseThrow().nonnullNodelist().values()) {
70                     allNodeList.addAll(nodelist.nonnullNodes().values());
71                 }
72             }
73         } catch (InterruptedException | ExecutionException ex) {
74             LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
75         }
76         String message = String.join(PIPE,notification.getResource().getDevice().getNodeId().getValue(),
77                 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
78                 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
79                 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
80                 notification.getCircuitId() != null ? notification.getCircuitId() : "", buildType(notification));
81
82         Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
83             .build();
84         if (allNodeList.contains(build)) {
85             LOG.info("onAlarmNotification: {}", message);
86         } else {
87             LOG.warn("onAlarmNotification: {}", message);
88         }
89     }
90
91     private String buildCause(ProbableCause probableCause) {
92         if (probableCause == null) {
93             return "||||";
94         }
95         return String.join(PIPE,
96                 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
97                 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
98                 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
99                 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
100     }
101
102     @SuppressWarnings("unchecked")
103     private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
104             Resource resource) {
105         if (resource == null) {
106             LOG.error("Resource is null.");
107             return Optional.empty();
108         }
109         if (!resourceClass.isInstance(resource)) {
110             LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
111                     resourceClass.getSimpleName(), resource.getClass().getSimpleName());
112             return Optional.empty();
113         }
114         return Optional.of((T) resource);
115     }
116
117     private static String buildType(AlarmNotification notification) {
118         String circuitPack = "";
119         String connection = "";
120         String degree = "";
121         String iface = "";
122         String internalLink = "";
123         String physicalLink = "";
124         String service = "";
125         String shelf = "";
126         String sharedRiskGroup = "";
127         String port = "";
128         String portCircuitPack = "";
129
130         Resource resource = notification.getResource().getResource().getResource();
131         ResourceType wantedResourceType = notification.getResource().getResourceType();
132
133         switch (wantedResourceType.getType()) {
134             case CircuitPack:
135                 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
136                 if (circuitPackOptional.isPresent()) {
137                     circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
138                 }
139                 break;
140
141             case Connection:
142                 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
143                 if (connectionOptional.isPresent()) {
144                     connection = connectionOptional.orElseThrow().getConnectionName();
145                 }
146                 break;
147
148             case Degree:
149                 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
150                 if (degreeOptional.isPresent()) {
151                     degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
152                 }
153                 break;
154
155             case Interface:
156                 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
157                 if (interfaceOptional.isPresent()) {
158                     iface = interfaceOptional.orElseThrow().getInterfaceName();
159                 }
160                 break;
161
162             case InternalLink:
163                 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
164                 if (internalLinkOptional.isPresent()) {
165                     internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
166                 }
167                 break;
168
169             case PhysicalLink:
170                 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
171                 if (physicalLinkOptional.isPresent()) {
172                     physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
173                 }
174                 break;
175
176             case Service:
177                 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
178                 if (serviceOptional.isPresent()) {
179                     service = serviceOptional.orElseThrow().getServiceName();
180                 }
181                 break;
182
183             case Shelf:
184                 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
185                 if (shelfOptional.isPresent()) {
186                     shelf = shelfOptional.orElseThrow().getShelfName();
187                 }
188                 break;
189
190             case SharedRiskGroup:
191                 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
192                 if (sharedRiskGroupOptional.isPresent()) {
193                     sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
194                 }
195                 break;
196
197             case Port:
198                 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
199                 if (portContainerOptional.isPresent()) {
200                     port = portContainerOptional.orElseThrow().getPort().getPortName();
201                     portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
202                 }
203                 break;
204
205             default:
206                 LOG.warn("Unknown resource type {}", wantedResourceType);
207         }
208         return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
209                 service, shelf, sharedRiskGroup, port, portCircuitPack);
210     }
211 }