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;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.ReadTransaction;
17 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.ServiceNodelist;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.Nodelist;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.Nodes;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.alarmsuppression.rev171102.service.nodelist.nodelist.NodesBuilder;
22 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.AlarmNotification;
23 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev181019.OrgOpenroadmAlarmListener;
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 implements OrgOpenroadmAlarmListener {
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;
53 * Callback for alarm-notification.
55 * @param notification AlarmNotification object
58 public void onAlarmNotification(AlarmNotification notification) {
59 List<Nodes> allNodeList = new ArrayList<>();
60 InstanceIdentifier<ServiceNodelist> serviceNodeListIID = InstanceIdentifier.create(ServiceNodelist.class);
62 ReadTransaction rtx = dataBroker.newReadOnlyTransaction();
63 Optional<ServiceNodelist> serviceListObject =
64 rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
65 if (serviceListObject.isPresent()) {
66 for (Nodelist nodelist : serviceListObject.get().getNodelist()) {
67 allNodeList.addAll(nodelist.getNodes());
70 } catch (InterruptedException | ExecutionException ex) {
71 LOG.warn("Exception thrown while reading Logical Connection Point value", ex);
73 StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId().getValue())
75 sb.append(buildCause(notification.getProbableCause()));
76 sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
77 .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE)
78 .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE)
79 .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE);
81 sb.append(buildType(notification));
83 String message = sb.toString();
84 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId().getValue())
86 if (allNodeList.contains(build)) {
87 LOG.info("onAlarmNotification: {}", message);
89 LOG.warn("onAlarmNotification: {}", message);
93 private String buildCause(ProbableCause probableCause) {
94 StringBuilder sb = new StringBuilder();
95 if (probableCause == null) {
98 sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE)
99 .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "")
100 .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "")
101 .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "")
103 return sb.toString();
106 @SuppressWarnings("unchecked")
107 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
109 if (resource == null) {
110 LOG.error("Resource is null.");
111 } else if (!resourceClass.isInstance(resource)) {
112 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
113 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
115 return Optional.of((T) resource);
117 return Optional.empty();
120 private static String buildType(AlarmNotification notification) {
121 String circuitPack = "";
122 String connection = "";
125 String internalLink = "";
126 String physicalLink = "";
129 String sharedRiskGroup = "";
131 String portCircuitPack = "";
133 Resource resource = notification.getResource().getResource().getResource();
134 ResourceType wantedResourceType = notification.getResource().getResourceType();
136 switch (wantedResourceType.getType()) {
138 Optional<CircuitPack> circuitPackOptional = tryCastToParticularResource(CircuitPack.class, resource);
139 if (circuitPackOptional.isPresent()) {
140 circuitPack = circuitPackOptional.get().getCircuitPackName();
145 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
146 if (connectionOptional.isPresent()) {
147 connection = connectionOptional.get().getConnectionName();
152 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
153 if (degreeOptional.isPresent()) {
154 degree = degreeOptional.get().getDegreeNumber().toString();
159 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
160 if (interfaceOptional.isPresent()) {
161 iface = interfaceOptional.get().getInterfaceName();
166 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
167 if (internalLinkOptional.isPresent()) {
168 internalLink = internalLinkOptional.get().getInternalLinkName();
173 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
174 if (physicalLinkOptional.isPresent()) {
175 physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
180 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
181 if (serviceOptional.isPresent()) {
182 service = serviceOptional.get().getServiceName();
187 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
188 if (shelfOptional.isPresent()) {
189 shelf = shelfOptional.get().getShelfName();
193 case SharedRiskGroup:
194 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
195 if (sharedRiskGroupOptional.isPresent()) {
196 sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
201 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
202 if (portContainerOptional.isPresent()) {
203 port = portContainerOptional.get().getPort().getPortName();
204 portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
209 LOG.warn("Unknown resource type {}", wantedResourceType);
211 StringBuilder sb = new StringBuilder(circuitPack);
212 sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface);
213 sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service);
214 sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port);
215 sb.append(PIPE).append(portCircuitPack);
216 return sb.toString();