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;
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.rev200529.AlarmNotification;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev200529.OrgOpenroadmAlarmListener;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev200529.alarm.ProbableCause;
24 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.ResourceType;
25 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.Resource;
26 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.CircuitPack;
27 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Connection;
28 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Degree;
29 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Interface;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.InternalLink;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.PhysicalLink;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Port;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Service;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.resource.resource.resource.Shelf;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.rev200529.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 AlarmNotificationListener710 implements OrgOpenroadmAlarmListener {
42 private static final Logger LOG = LoggerFactory.getLogger(AlarmNotificationListener710.class);
43 private static final String PIPE = "|";
44 private final DataBroker dataBroker;
46 public AlarmNotificationListener710(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);
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.orElseThrow().nonnullNodelist().values()) {
66 allNodeList.addAll(nodelist.nonnullNodes().values());
69 } catch (InterruptedException | ExecutionException ex) {
70 LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
72 String message = String.join(PIPE,notification.getResource().getDevice().getNodeId().getValue(),
73 buildCause(notification.getProbableCause()),notification.getId() != null ? notification.getId() : "",
74 notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "",
75 notification.getSeverity() != null ? notification.getSeverity().getName() : "",
76 notification.getCircuitId() != null ? notification.getCircuitId() : "",buildType(notification));
78 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
80 if (allNodeList.contains(build)) {
81 LOG.info("onAlarmNotification: {}", message);
83 LOG.warn("onAlarmNotification: {}", message);
87 private String buildCause(ProbableCause probableCause) {
88 if (probableCause == null) {
91 return String.join(PIPE,
92 (probableCause.getCause() != null) ? probableCause.getCause().getName() : "",
93 (probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "",
94 (probableCause.getExtension() != null) ? probableCause.getExtension() : "",
95 (probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "");
98 @SuppressWarnings("unchecked")
99 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
101 if (resource == null) {
102 LOG.error("Resource is null.");
103 return Optional.empty();
105 if (!resourceClass.isInstance(resource)) {
106 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
107 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
108 return Optional.empty();
110 return Optional.of((T) resource);
113 private static String buildType(AlarmNotification notification) {
114 String circuitPack = "";
115 String connection = "";
118 String internalLink = "";
119 String physicalLink = "";
122 String sharedRiskGroup = "";
124 String portCircuitPack = "";
126 Resource resource = notification.getResource().getResource().getResource();
127 ResourceType wantedResourceType = notification.getResource().getResourceType();
129 switch (wantedResourceType.getType()) {
131 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
132 if (circuitPackOptional.isPresent()) {
133 circuitPack = circuitPackOptional.orElseThrow().getCircuitPackName();
138 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
139 if (connectionOptional.isPresent()) {
140 connection = connectionOptional.orElseThrow().getConnectionName();
145 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
146 if (degreeOptional.isPresent()) {
147 degree = degreeOptional.orElseThrow().getDegreeNumber().toString();
152 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
153 if (interfaceOptional.isPresent()) {
154 iface = interfaceOptional.orElseThrow().getInterfaceName();
159 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
160 if (internalLinkOptional.isPresent()) {
161 internalLink = internalLinkOptional.orElseThrow().getInternalLinkName();
166 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
167 if (physicalLinkOptional.isPresent()) {
168 physicalLink = physicalLinkOptional.orElseThrow().getPhysicalLinkName();
173 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
174 if (serviceOptional.isPresent()) {
175 service = serviceOptional.orElseThrow().getServiceName();
180 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
181 if (shelfOptional.isPresent()) {
182 shelf = shelfOptional.orElseThrow().getShelfName();
186 case SharedRiskGroup:
187 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
188 if (sharedRiskGroupOptional.isPresent()) {
189 sharedRiskGroup = sharedRiskGroupOptional.orElseThrow().getSrgNumber().toString();
194 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
195 if (portContainerOptional.isPresent()) {
196 port = portContainerOptional.orElseThrow().getPort().getPortName();
197 portCircuitPack = portContainerOptional.orElseThrow().getPort().getCircuitPackName();
202 LOG.warn("Unknown resource type {}", wantedResourceType);
204 return String.join(PIPE, circuitPack, connection, degree, iface, internalLink, physicalLink,
205 service, shelf, sharedRiskGroup, port, portCircuitPack);