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.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.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);
61 ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction();
62 com.google.common.base.Optional<ServiceNodelist> serviceListObject =
63 rtx.read(LogicalDatastoreType.OPERATIONAL, serviceNodeListIID).get();
64 if (serviceListObject.isPresent()) {
65 for (Nodelist nodelist : serviceListObject.get().getNodelist()) {
66 allNodeList.addAll(nodelist.getNodes());
69 } catch (InterruptedException | ExecutionException ex) {
70 LOG.warn("Exception thrown while reading Logical Connection Point value from {} {}", ex);
72 StringBuilder sb = new StringBuilder(notification.getResource().getDevice().getNodeId()).append(PIPE);
73 sb.append(buildCause(notification.getProbableCause()));
74 sb.append(notification.getId() != null ? notification.getId() : "").append(PIPE)
75 .append(notification.getType() != null ? notification.getType().toString() : "").append(PIPE)
76 .append(notification.getRaiseTime() != null ? notification.getRaiseTime().toString() : "").append(PIPE)
77 .append(notification.getSeverity() != null ? notification.getSeverity().getName() : "").append(PIPE)
78 .append(notification.getCircuitId() != null ? notification.getCircuitId() : "").append(PIPE);
80 sb.append(buildType(notification));
82 String message = sb.toString();
83 Nodes build = new NodesBuilder().setNodeId(notification.getResource().getDevice().getNodeId()).build();
84 if (allNodeList.contains(build)) {
91 private String buildCause(ProbableCause probableCause) {
92 StringBuilder sb = new StringBuilder();
93 if (probableCause == null) {
96 sb.append((probableCause.getCause() != null) ? probableCause.getCause().getName() : "").append(PIPE)
97 .append((probableCause.getDirection() != null) ? probableCause.getDirection().getName() : "")
98 .append(PIPE).append((probableCause.getExtension() != null) ? probableCause.getExtension() : "")
99 .append(PIPE).append((probableCause.getLocation() != null) ? probableCause.getLocation().getName() : "")
101 return sb.toString();
104 @SuppressWarnings("unchecked")
105 private static <T extends Resource> Optional<T> tryCastToParticularResource(Class<T> resourceClass,
107 if (resource == null) {
108 LOG.error("Resource is null.");
109 } else if (!resourceClass.isInstance(resource)) {
110 LOG.error("Resource implement different type than expected. Expected {}, actual {}.",
111 resourceClass.getSimpleName(), resource.getClass().getSimpleName());
113 return Optional.of((T) resource);
115 return Optional.empty();
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.get().getCircuitPackName();
143 Optional<Connection> connectionOptional = tryCastToParticularResource(Connection.class, resource);
144 if (connectionOptional.isPresent()) {
145 connection = connectionOptional.get().getConnectionNumber();
150 Optional<Degree> degreeOptional = tryCastToParticularResource(Degree.class, resource);
151 if (degreeOptional.isPresent()) {
152 degree = degreeOptional.get().getDegreeNumber().toString();
157 Optional<Interface> interfaceOptional = tryCastToParticularResource(Interface.class, resource);
158 if (interfaceOptional.isPresent()) {
159 iface = interfaceOptional.get().getInterfaceName();
164 Optional<InternalLink> internalLinkOptional = tryCastToParticularResource(InternalLink.class, resource);
165 if (internalLinkOptional.isPresent()) {
166 internalLink = internalLinkOptional.get().getInternalLinkName();
171 Optional<PhysicalLink> physicalLinkOptional = tryCastToParticularResource(PhysicalLink.class, resource);
172 if (physicalLinkOptional.isPresent()) {
173 physicalLink = physicalLinkOptional.get().getPhysicalLinkName();
178 Optional<Service> serviceOptional = tryCastToParticularResource(Service.class, resource);
179 if (serviceOptional.isPresent()) {
180 service = serviceOptional.get().getServiceName();
185 Optional<Shelf> shelfOptional = tryCastToParticularResource(Shelf.class, resource);
186 if (shelfOptional.isPresent()) {
187 shelf = shelfOptional.get().getShelfName();
191 case SharedRiskGroup:
192 Optional<Srg> sharedRiskGroupOptional = tryCastToParticularResource(Srg.class, resource);
193 if (sharedRiskGroupOptional.isPresent()) {
194 sharedRiskGroup = sharedRiskGroupOptional.get().getSrgNumber().toString();
199 Optional<Port> portContainerOptional = tryCastToParticularResource(Port.class, resource);
200 if (portContainerOptional.isPresent()) {
201 port = portContainerOptional.get().getPort().getPortName();
202 portCircuitPack = portContainerOptional.get().getPort().getCircuitPackName();
207 LOG.warn("Unknown resource type {}", wantedResourceType);
209 StringBuilder sb = new StringBuilder(circuitPack);
210 sb.append(PIPE).append(connection).append(PIPE).append(degree).append(PIPE).append(iface);
211 sb.append(PIPE).append(internalLink).append(PIPE).append(physicalLink).append(PIPE).append(service);
212 sb.append(PIPE).append(shelf).append(PIPE).append(sharedRiskGroup).append(PIPE).append(port);
213 sb.append(PIPE).append(portCircuitPack);
214 return sb.toString();