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