Networkmodel class renaming
[transportpce.git] / networkmodel / src / main / java / org / opendaylight / transportpce / networkmodel / listeners / AlarmNotificationListener.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.rev161014.AlarmNotification;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.alarm.ProbableCause;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.ResourceType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.Resource;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.CircuitPack;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Connection;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Degree;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Interface;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.InternalLink;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.PhysicalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Port;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Service;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.resource.resource.resource.Shelf;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev161014.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 AlarmNotificationListener implements OrgOpenroadmAlarmListener {
41
42     private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener.class);
43     private static final String PIPE = "|";
44     private final DataBroker dataBroker;
45
46     public AlarmNotificationListener(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 (ReadTransaction rtx = dataBroker.newReadOnlyTransaction()) {
61             Optional<ServiceNodelist> serviceListObject =
62                     rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
63             if (serviceListObject.isPresent()) {
64                 for (Nodelist nodelist : serviceListObject.get().nonnullNodelist().values()) {
65                     allNodeList.addAll(nodelist.nonnullNodes().values());
66                 }
67             }
68         } catch (InterruptedException | ExecutionException ex) {
69             LOG.warn("Exception thrown while reading Logical Connection Point value: ", ex);
70         }
71         StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId()).append(PIPE);
72         sb.append(buildCause(notification.getProbableCause()));
73         sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
74                 .append(notification.getType() != null ? notification.getType().toString() : "").append(PIPE)
75                 .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE)
76                 .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE)
77                 .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE);
78
79         sb.append(buildType(notification));
80
81         String message = sb.toString();
82         Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build();
83         if (allNodeList.contains(build)) {
84             LOG.info("onAlarmNotification: {}", message);
85         } else {
86             LOG.warn("onAlarmNotification: {}", message);
87         }
88     }
89
90     private String buildCause(ProbableCause probableCause) {
91         StringBuilder sb = new StringBuilder();
92         if (probableCause == null) {
93             return "||||";
94         }
95         sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE)
96                 .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "")
97                 .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "")
98                 .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "")
99                 .append(PIPE);
100         return sb.toString();
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         } else if (!resourceClass.isInstance(resource)) {
109             LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
110                     resourceClass.getSimpleName(), resource.getClass().getSimpleName());
111         } else {
112             return Optional.of((T) resource);
113         }
114         return Optional.empty();
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.get().getCircuitPackName();
138                 }
139                 break;
140
141             case Connection:
142                 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
143                 if (connectionOptional.isPresent()) {
144                     connection = connectionOptional.get().getConnectionNumber();
145                 }
146                 break;
147
148             case Degree:
149                 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
150                 if (degreeOptional.isPresent()) {
151                     degree = degreeOptional.get().getDegreeNumber().toString();
152                 }
153                 break;
154
155             case Interface:
156                 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
157                 if (interfaceOptional.isPresent()) {
158                     iface = interfaceOptional.get().getInterfaceName();
159                 }
160                 break;
161
162             case InternalLink:
163                 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
164                 if (internalLinkOptional.isPresent()) {
165                     internalLink = internalLinkOptional.get().getInternalLinkName();
166                 }
167                 break;
168
169             case PhysicalLink:
170                 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
171                 if (physicalLinkOptional.isPresent()) {
172                     physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
173                 }
174                 break;
175
176             case Service:
177                 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
178                 if (serviceOptional.isPresent()) {
179                     service = serviceOptional.get().getServiceName();
180                 }
181                 break;
182
183             case Shelf:
184                 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
185                 if (shelfOptional.isPresent()) {
186                     shelf = shelfOptional.get().getShelfName();
187                 }
188                 break;
189
190             case SharedRiskGroup:
191                 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
192                 if (sharedRiskGroupOptional.isPresent()) {
193                     sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
194                 }
195                 break;
196
197             case Port:
198                 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
199                 if (portContainerOptional.isPresent()) {
200                     port = portContainerOptional.get().getPort().getPortName();
201                     portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
202                 }
203                 break;
204
205             default:
206                 LOG.warn("Unknown resource type {}", wantedResourceType);
207         }
208         StringBuilder sb = new StringBuilder(circuitPack);
209         sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface);
210         sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service);
211         sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port);
212         sb.append(PIPE).append(portCircuitPack);
213         return sb.toString();
214     }
215 }