713c98c01c99147fb2ef9efd14bf9f5cdde13731
[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 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.rev181019.AlarmNotification;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.alarm.ProbableCause;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.ResourceType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.Resource;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.CircuitPack;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Connection;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Degree;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Interface;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.InternalLink;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.PhysicalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Port;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Service;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.resource.resource.resource.Shelf;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev181019.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 AlarmNotificationListener221 implements OrgOpenroadmAlarmListener {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener221.class);
43     private static final String PIPE = "|";
44     private final DataBroker dataBroker;
45
46     public AlarmNotificationListener221(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         StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId().getValue())
73             .append(PIPE);
74         sb.append(buildCause(notification.getProbableCause()));
75         sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
76                 .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE)
77                 .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE)
78                 .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE);
79
80         sb.append(buildType(notification));
81
82         String message = sb.toString();
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         StringBuilder sb = new StringBuilder();
94         if (probableCause == null) {
95             return "||||";
96         }
97         sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE)
98                 .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "")
99                 .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "")
100                 .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "")
101                 .append(PIPE);
102         return sb.toString();
103     }
104
105     @SuppressWarnings("unchecked")
106     private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
107             Resource resource) {
108         if (resource == null) {
109             LOG.error("Resource is null.");
110         } else if (!resourceClass.isInstance(resource)) {
111             LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
112                     resourceClass.getSimpleName(), resource.getClass().getSimpleName());
113         } else {
114             return Optional.of((T) resource);
115         }
116         return Optional.empty();
117     }
118
119     private static String buildType(AlarmNotification notification) {
120         String circuitPack = "";
121         String connection = "";
122         String degree = "";
123         String iface = "";
124         String internalLink = "";
125         String physicalLink = "";
126         String service = "";
127         String shelf = "";
128         String sharedRiskGroup = "";
129         String port = "";
130         String portCircuitPack = "";
131
132         Resource resource = notification.getResource().getResource().getResource();
133         ResourceType wantedResourceType = notification.getResource().getResourceType();
134
135         switch (wantedResourceType.getType()) {
136             case CircuitPack:
137                 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
138                 if (circuitPackOptional.isPresent()) {
139                     circuitPack = circuitPackOptional.get().getCircuitPackName();
140                 }
141                 break;
142
143             case Connection:
144                 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
145                 if (connectionOptional.isPresent()) {
146                     connection = connectionOptional.get().getConnectionName();
147                 }
148                 break;
149
150             case Degree:
151                 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
152                 if (degreeOptional.isPresent()) {
153                     degree = degreeOptional.get().getDegreeNumber().toString();
154                 }
155                 break;
156
157             case Interface:
158                 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
159                 if (interfaceOptional.isPresent()) {
160                     iface = interfaceOptional.get().getInterfaceName();
161                 }
162                 break;
163
164             case InternalLink:
165                 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
166                 if (internalLinkOptional.isPresent()) {
167                     internalLink = internalLinkOptional.get().getInternalLinkName();
168                 }
169                 break;
170
171             case PhysicalLink:
172                 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
173                 if (physicalLinkOptional.isPresent()) {
174                     physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
175                 }
176                 break;
177
178             case Service:
179                 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
180                 if (serviceOptional.isPresent()) {
181                     service = serviceOptional.get().getServiceName();
182                 }
183                 break;
184
185             case Shelf:
186                 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
187                 if (shelfOptional.isPresent()) {
188                     shelf = shelfOptional.get().getShelfName();
189                 }
190                 break;
191
192             case SharedRiskGroup:
193                 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
194                 if (sharedRiskGroupOptional.isPresent()) {
195                     sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
196                 }
197                 break;
198
199             case Port:
200                 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
201                 if (portContainerOptional.isPresent()) {
202                     port = portContainerOptional.get().getPort().getPortName();
203                     portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
204                 }
205                 break;
206
207             default:
208                 LOG.warn("Unknown resource type {}", wantedResourceType);
209         }
210         StringBuilder sb = new StringBuilder(circuitPack);
211         sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface);
212         sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service);
213         sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port);
214         sb.append(PIPE).append(portCircuitPack);
215         return sb.toString();
216     }
217 }