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;
14 import java.util.concurrent.ExecutionException;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
17 import org.opendaylight.mdsal.binding.api.ReadTransaction;
18 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.ServiceNodelist;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.Nodelist;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.Nodes;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.NodesBuilder;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.AlarmNotification;
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;
41 public class AlarmNotificationListener221 {
43 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener221.class);
44 private static final String PIPE = "|";
45 private final DataBroker dataBroker;
47 public AlarmNotificationListener221(DataBroker dataBroker) {
48 this.dataBroker = dataBroker;
51 public CompositeListener getCompositeListener() {
52 return new CompositeListener(Set.of(
53 new CompositeListener.Component<>(AlarmNotification.class, this::onAlarmNotification)));
57 * Callback for alarm-notification.
59 * @param notification AlarmNotification object
61 private void onAlarmNotification(AlarmNotification notification) {
62 List<Nodes> allNodeList = new ArrayList<>();
63 InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
65 ReadTransaction rtx = dataBroker.newReadOnlyTransaction();
66 Optional<ServiceNodelist> serviceListObject =
67 rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
68 if (serviceListObject.isPresent()) {
69 for (Nodelist nodelist : serviceListObject.orElseThrow().nonnullNodelist().values()) {
70 allNodeList.addAll(nodelist.nonnullNodes().values());
73 } catch (InterruptedException | ExecutionException ex) {
74 LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
76 String message = String.join(PIPE,notification.getResource().getDevice().getNodeId().getValue(),
77 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
78 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
79 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
80 notification.getCircuitId() != null ? notification.getCircuitId() : "", buildType(notification));
82 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
84 if (allNodeList.contains(build)) {
85 LOG.info("onAlarmNotification: {}", message);
87 LOG.warn("onAlarmNotification: {}", message);
91 private String buildCause(ProbableCause probableCause) {
92 if (probableCause == null) {
95 return String.join(PIPE,
96 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
97 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
98 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
99 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
102 @SuppressWarnings("unchecked")
103 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
105 if (resource == null) {
106 LOG.error("Resource is null.");
107 return Optional.empty();
109 if (!resourceClass.isInstance(resource)) {
110 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
111 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
112 return Optional.empty();
114 return Optional.of((T) resource);
117 private static String buildType(AlarmNotification notification) {
118 String circuitPack = "";
119 String connection = "";
122 String internalLink = "";
123 String physicalLink = "";
126 String sharedRiskGroup = "";
128 String portCircuitPack = "";
130 Resource resource = notification.getResource().getResource().getResource();
131 ResourceType wantedResourceType = notification.getResource().getResourceType();
133 switch (wantedResourceType.getType()) {
135 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
136 if (circuitPackOptional.isPresent()) {
137 circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
142 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
143 if (connectionOptional.isPresent()) {
144 connection = connectionOptional.orElseThrow().getConnectionName();
149 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
150 if (degreeOptional.isPresent()) {
151 degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
156 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
157 if (interfaceOptional.isPresent()) {
158 iface = interfaceOptional.orElseThrow().getInterfaceName();
163 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
164 if (internalLinkOptional.isPresent()) {
165 internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
170 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
171 if (physicalLinkOptional.isPresent()) {
172 physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
177 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
178 if (serviceOptional.isPresent()) {
179 service = serviceOptional.orElseThrow().getServiceName();
184 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
185 if (shelfOptional.isPresent()) {
186 shelf = shelfOptional.orElseThrow().getShelfName();
190 case SharedRiskGroup:
191 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
192 if (sharedRiskGroupOptional.isPresent()) {
193 sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
198 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
199 if (portContainerOptional.isPresent()) {
200 port = portContainerOptional.orElseThrow().getPort().getPortName();
201 portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
206 LOG.warn("Unknown resource type {}", wantedResourceType);
208 return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
209 service, shelf, sharedRiskGroup, port, portCircuitPack);