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