Update PortMapping & imports to v1.2.1
[transportpce.git] / renderer / src / main / java / org / opendaylight / transportpce / renderer / RendererNotificationsImpl.java
1 /*
2  * Copyright © 2017 AT&T and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.transportpce.renderer;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import java.util.stream.Collectors;
22
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
25 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
27 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
29 import org.opendaylight.controller.md.sal.binding.api.MountPoint;
30 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
31 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
34 import org.opendaylight.transportpce.renderer.listeners.AlarmNotificationListener;
35 import org.opendaylight.transportpce.renderer.listeners.DeOperationsListener;
36 import org.opendaylight.transportpce.renderer.listeners.DeviceListener;
37 import org.opendaylight.transportpce.renderer.listeners.LldpListener;
38 import org.opendaylight.transportpce.renderer.listeners.TcaListener;
39 import org.opendaylight.transportpce.renderer.mapping.PortMapping;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.AlarmNotification;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.alarm.rev161014.OrgOpenroadmAlarmListener;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.de.operations.rev161014.OrgOpenroadmDeOperationsListener;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.OrgOpenroadmDeviceListener;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.lldp.rev161014.OrgOpenroadmLldpListener;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.tca.rev161014.OrgOpenroadmTcaListener;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
58 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
61 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
66
67 public class RendererNotificationsImpl implements DataTreeChangeListener<Node> {
68
69     private final DataBroker dataBroker;
70     private final MountPointService mountService;
71     private static final Logger LOG = LoggerFactory.getLogger(RendererNotificationsImpl.class);
72     private ListenerRegistration<RendererNotificationsImpl> dataTreeChangeListenerRegistration;
73
74     private final Set<String> currentMountedDevice;
75     public static final InstanceIdentifier<Topology> NETCONF_TOPO_IID = InstanceIdentifier.create(NetworkTopology.class)
76         .child(Topology.class, new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
77
78     LoadingCache<String, KeyedInstanceIdentifier<Node, NodeKey>> mountIds = CacheBuilder.newBuilder().maximumSize(20)
79         .build(new CacheLoader<String, KeyedInstanceIdentifier<Node, NodeKey>>() {
80             @Override
81             public KeyedInstanceIdentifier<Node, NodeKey> load(final String key) {
82                 return NETCONF_TOPO_IID.child(Node.class, new NodeKey(new NodeId(key)));
83             }
84         });
85
86     public RendererNotificationsImpl(final DataBroker dataBroker, final MountPointService mountService,
87         Set<String> currentMountedDevice) {
88         this.dataBroker = dataBroker;
89         this.mountService = mountService;
90         this.currentMountedDevice = currentMountedDevice;
91         if (mountService == null) {
92             LOG.error("Mount service is null");
93
94         }
95         if (dataBroker != null) {
96             this.dataTreeChangeListenerRegistration = dataBroker.registerDataTreeChangeListener(
97                 new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NETCONF_TOPO_IID.child(Node.class)), this);
98         }
99     }
100
101     private void registerNotificationListener(final NodeId nodeId) {
102         final Optional<MountPoint> mountPoint;
103         try {
104             // Get mount point for specified device
105             mountPoint = mountService.getMountPoint(mountIds.get(nodeId.getValue()));
106             if (!mountPoint.isPresent()) {
107                 LOG.error("Mount point for node {} doesn't exist", nodeId.getValue());
108             }
109         } catch (ExecutionException e) {
110             throw new IllegalArgumentException(e);
111         }
112
113         // Register notification service
114         final Optional<NotificationService> notificationService = mountPoint.get().getService(
115             NotificationService.class);
116         if (!notificationService.isPresent()) {
117             LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
118         }
119
120         final OrgOpenroadmAlarmListener alarmListener;
121         alarmListener = new AlarmNotificationListener();
122         LOG.info("Registering notification listener on {} for node: {}", AlarmNotification.QNAME, nodeId);
123         // Register notification listener
124         final ListenerRegistration<OrgOpenroadmAlarmListener>
125             accessAlarmNotificationListenerRegistration =
126                 notificationService.get().registerNotificationListener(alarmListener);
127
128         final OrgOpenroadmDeOperationsListener deOperationsListener;
129         deOperationsListener = new DeOperationsListener();
130         LOG.info("Registering notification listener on OrgOpenroadmDeOperationsListener for node: {}", nodeId);
131         // Register notification listener
132         final ListenerRegistration<OrgOpenroadmDeOperationsListener>
133             accessDeOperationasNotificationListenerRegistration =
134                 notificationService.get().registerNotificationListener(deOperationsListener);
135
136         final OrgOpenroadmDeviceListener deviceListener;
137         deviceListener = new DeviceListener();
138         LOG.info("Registering notification listener on OrgOpenroadmDeviceListener for node: {}", nodeId);
139         // Register notification listener
140         final ListenerRegistration<OrgOpenroadmDeviceListener>
141             accessDeviceNotificationListenerRegistration = notificationService.get()
142                 .registerNotificationListener(deviceListener);
143
144         final OrgOpenroadmLldpListener lldpListener;
145         lldpListener = new LldpListener();
146         LOG.info("Registering notification listener on OrgOpenroadmLldpListener for node: {}", nodeId);
147         // Register notification listener
148         final ListenerRegistration<OrgOpenroadmLldpListener> accessLldpNotificationListenerRegistration =
149             notificationService.get().registerNotificationListener(lldpListener);
150
151         final OrgOpenroadmTcaListener tcaListener;
152         tcaListener = new TcaListener();
153         LOG.info("Registering notification listener on OrgOpenroadmTcaListener for node: {}", nodeId);
154         // Register notification listener
155         final ListenerRegistration<OrgOpenroadmTcaListener> accessTcaNotificationListenerRegistration =
156             notificationService.get().registerNotificationListener(tcaListener);
157
158         // Listening to NETCONF datastream
159         final String streamName = "NETCONF";
160         final Optional<RpcConsumerRegistry> service = mountPoint.get().getService(RpcConsumerRegistry.class);
161         if (!service.isPresent()) {
162             LOG.error("Failed to get RpcService for node {}", nodeId.getValue());
163         }
164         final NotificationsService rpcService = service.get().getRpcService(NotificationsService.class);
165         final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
166         createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
167         LOG.info("Triggering notification stream {} for node {}", streamName, nodeId);
168         final Future<RpcResult<Void>> subscription = rpcService.createSubscription(createSubscriptionInputBuilder
169             .build());
170     }
171
172     public void close() {
173         LOG.info("RenderernotificationsImpl Closed");
174         // Clean up the Data Change Listener registration
175         if (dataTreeChangeListenerRegistration != null) {
176             dataTreeChangeListenerRegistration.close();
177         }
178     }
179
180     @Override
181     public void onDataTreeChanged(Collection<DataTreeModification<Node>> changes) {
182
183         for (DataTreeModification<Node> change : changes) {
184
185             DataObjectModification<Node> rootNode = change.getRootNode();
186             String nodeId = rootNode.getDataAfter().getKey().getNodeId().getValue();
187             NetconfNode nnode = Preconditions.checkNotNull(rootNode.getDataAfter().getAugmentation(NetconfNode.class),
188                 "Node not connected via Netconf protocol");
189             if (nnode != null) {
190
191                 if (rootNode.getModificationType() == ModificationType.WRITE) {
192                     LOG.info("Node added " + nodeId);
193
194                 } else if (rootNode.getModificationType() == ModificationType.SUBTREE_MODIFIED) {
195
196                     LOG.info("Node modified " + nodeId);
197                     ConnectionStatus csts = nnode.getConnectionStatus();
198
199                     switch (csts) {
200                         case Connected: {
201                             LOG.info("NETCONF Node: {} is fully connected", nodeId);
202                             List<String> capabilities = nnode.getAvailableCapabilities().getAvailableCapability()
203                                 .stream().map(cp -> cp.getCapability()).collect(Collectors.toList());
204                             LOG.info("Capabilities: {}", capabilities);
205                             /*
206                              * TODO: check for required
207                              * capabilities to listen for notifications
208                              * registerNotificationListener(rootNode.
209                              * getDataAfter(). getNodeId());
210                              */
211                             currentMountedDevice.add(nodeId);
212                             new PortMapping(dataBroker, mountService, nodeId).createMappingData();
213                             break;
214                         }
215                         case Connecting: {
216                             LOG.info("NETCONF Node: {} was disconnected", nodeId);
217                             break;
218                         }
219                         case UnableToConnect: {
220                             LOG.info("NETCONF Node: {} connection failed", nodeId);
221                             break;
222                         }
223                         default:
224                             LOG.warn("Unexpected connection status " + csts.getName());
225                     }
226                 } else if (rootNode.getModificationType() ==  ModificationType.DELETE) {
227                     LOG.info("Node removed " + nodeId);
228                     currentMountedDevice.remove(nodeId);
229                 }
230             }
231         }
232     }
233 }