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