2 * Copyright © 2021 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.rev200529.AlarmNotification;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev200529.alarm.ProbableCause;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.ResourceType;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.Resource;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.CircuitPack;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Connection;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Degree;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Interface;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.InternalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.PhysicalLink;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Port;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Service;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Shelf;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.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 AlarmNotificationListener710 {
43 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener710.class);
44 private static final String PIPE = "|";
45 private final DataBroker dataBroker;
47 public AlarmNotificationListener710(DataBroker dataBroker) {
48 this.dataBroker = dataBroker;
51 public CompositeListener getCompositeListener() {
52 return new CompositeListener(Set.of(
53 new CompositeListener.Component<>(AlarmNotification.class, this::onAlarmNotification)));
58 * Callback for alarm-notification.
60 * @param notification AlarmNotification object
62 private void onAlarmNotification(AlarmNotification notification) {
63 List<Nodes> allNodeList = new ArrayList<>();
64 InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
66 ReadTransaction rtx = dataBroker.newReadOnlyTransaction();
67 Optional<ServiceNodelist> serviceListObject =
68 rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
69 if (serviceListObject.isPresent()) {
70 for (Nodelist nodelist : serviceListObject.orElseThrow().nonnullNodelist().values()) {
71 allNodeList.addAll(nodelist.nonnullNodes().values());
74 } catch (InterruptedException | ExecutionException ex) {
75 LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
77 String message = String.join(PIPE,notification.getResource().getDevice().getNodeId().getValue(),
78 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
79 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
80 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
81 notification.getCircuitId() != null ? notification.getCircuitId() : "",buildType(notification));
83 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
85 if (allNodeList.contains(build)) {
86 LOG.info("onAlarmNotification: {}", message);
88 LOG.warn("onAlarmNotification: {}", message);
92 private String buildCause(ProbableCause probableCause) {
93 if (probableCause == null) {
96 return String.join(PIPE,
97 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
98 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
99 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
100 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
103 @SuppressWarnings("unchecked")
104 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
106 if (resource == null) {
107 LOG.error("Resource is null.");
108 return Optional.empty();
110 if (!resourceClass.isInstance(resource)) {
111 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
112 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
113 return Optional.empty();
115 return Optional.of((T) resource);
118 private static String buildType(AlarmNotification notification) {
119 String circuitPack = "";
120 String connection = "";
123 String internalLink = "";
124 String physicalLink = "";
127 String sharedRiskGroup = "";
129 String portCircuitPack = "";
131 Resource resource = notification.getResource().getResource().getResource();
132 ResourceType wantedResourceType = notification.getResource().getResourceType();
134 switch (wantedResourceType.getType()) {
136 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
137 if (circuitPackOptional.isPresent()) {
138 circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
143 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
144 if (connectionOptional.isPresent()) {
145 connection = connectionOptional.orElseThrow().getConnectionName();
150 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
151 if (degreeOptional.isPresent()) {
152 degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
157 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
158 if (interfaceOptional.isPresent()) {
159 iface = interfaceOptional.orElseThrow().getInterfaceName();
164 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
165 if (internalLinkOptional.isPresent()) {
166 internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
171 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
172 if (physicalLinkOptional.isPresent()) {
173 physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
178 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
179 if (serviceOptional.isPresent()) {
180 service = serviceOptional.orElseThrow().getServiceName();
185 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
186 if (shelfOptional.isPresent()) {
187 shelf = shelfOptional.orElseThrow().getShelfName();
191 case SharedRiskGroup:
192 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
193 if (sharedRiskGroupOptional.isPresent()) {
194 sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
199 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
200 if (portContainerOptional.isPresent()) {
201 port = portContainerOptional.orElseThrow().getPort().getPortName();
202 portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
207 LOG.warn("Unknown resource type {}", wantedResourceType);
209 return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
210 service, shelf, sharedRiskGroup, port, portCircuitPack);