2 * Copyright © 2017 AT&T and others. All rights reserved.
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
8 package org.opendaylight.transportpce.networkmodel.listeners;
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;
40 public class AlarmNotificationListener implements OrgOpenroadmAlarmListener {
42 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener.class);
43 private static final String PIPE = "|";
44 private final DataBroker dataBroker;
46 public AlarmNotificationListener(DataBroker dataBroker) {
47 this.dataBroker = dataBroker;
52 * Callback for alarm-notification.
54 * @param notification AlarmNotification object
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());
68 } catch (InterruptedException | ExecutionException ex) {
69 LOG.warn("Exception thrown while reading Logical Connection Point value: ", ex);
71 String message = String.join(PIPE,notification.getResource().getDevice().getNodeId(),
72 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
73 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
74 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
75 notification.getCircuitId() != null ? notification.getCircuitId() : "", buildType(notification));
77 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build();
78 if (allNodeList.contains(build)) {
79 LOG.info("onAlarmNotification: {}", message);
81 LOG.warn("onAlarmNotification: {}", message);
85 private String buildCause(ProbableCause probableCause) {
86 if (probableCause == null) {
89 return String.join(PIPE,
90 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
91 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
92 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
93 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
96 @SuppressWarnings("unchecked")
97 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
99 if (resource == null) {
100 LOG.error("Resource is null.");
101 return Optional.empty();
103 if (!resourceClass.isInstance(resource)) {
104 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
105 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
106 return Optional.empty();
108 return Optional.of((T) resource);
111 private static String buildType(AlarmNotification notification) {
112 String circuitPack = "";
113 String connection = "";
116 String internalLink = "";
117 String physicalLink = "";
120 String sharedRiskGroup = "";
122 String portCircuitPack = "";
124 Resource resource = notification.getResource().getResource().getResource();
125 ResourceType wantedResourceType = notification.getResource().getResourceType();
127 switch (wantedResourceType.getType()) {
129 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
130 if (circuitPackOptional.isPresent()) {
131 circuitPack = circuitPackOptional.get().getCircuitPackName();
136 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
137 if (connectionOptional.isPresent()) {
138 connection = connectionOptional.get().getConnectionNumber();
143 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
144 if (degreeOptional.isPresent()) {
145 degree = degreeOptional.get().getDegreeNumber().toString();
150 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
151 if (interfaceOptional.isPresent()) {
152 iface = interfaceOptional.get().getInterfaceName();
157 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
158 if (internalLinkOptional.isPresent()) {
159 internalLink = internalLinkOptional.get().getInternalLinkName();
164 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
165 if (physicalLinkOptional.isPresent()) {
166 physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
171 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
172 if (serviceOptional.isPresent()) {
173 service = serviceOptional.get().getServiceName();
178 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
179 if (shelfOptional.isPresent()) {
180 shelf = shelfOptional.get().getShelfName();
184 case SharedRiskGroup:
185 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
186 if (sharedRiskGroupOptional.isPresent()) {
187 sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
192 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
193 if (portContainerOptional.isPresent()) {
194 port = portContainerOptional.get().getPort().getPortName();
195 portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
200 LOG.warn("Unknown resource type {}", wantedResourceType);
202 return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
203 service, shelf, sharedRiskGroup, port, portCircuitPack);