Fixup Augmentable and Identifiable methods changing
[netconf.git] / netconf / messagebus-netconf / src / main / java / org / opendaylight / netconf / messagebus / eventsources / netconf / NetconfEventSourceRegistration.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.netconf.messagebus.eventsources.netconf;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.List;
13 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
14 import org.opendaylight.controller.messagebus.spi.EventSourceRegistration;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.status.available.capabilities.AvailableCapability;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Helper class to keep connection status of netconf node  and event source registration object.
31  */
32 public final class NetconfEventSourceRegistration implements AutoCloseable {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfEventSourceRegistration.class);
35     private static final YangInstanceIdentifier NETCONF_DEVICE_DOM_PATH = YangInstanceIdentifier.builder()
36             .node(NetworkTopology.QNAME).node(Topology.QNAME)
37             .nodeWithKey(Topology.QNAME, QName.create(Topology.QNAME, "topology-id"), TopologyNetconf.QNAME
38                     .getLocalName())
39             .node(Node.QNAME).build();
40     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "node-id");
41     private static final String NOTIFICATION_CAPABILITY_PREFIX = "(urn:ietf:params:xml:ns:netconf:notification";
42
43     private final Node node;
44     private final NetconfEventSourceManager netconfEventSourceManager;
45     private ConnectionStatus currentNetconfConnStatus;
46     private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
47
48     public static NetconfEventSourceRegistration create(final InstanceIdentifier<?> instanceIdent, final Node node,
49                                                         final NetconfEventSourceManager netconfEventSourceManager) {
50         Preconditions.checkNotNull(instanceIdent);
51         Preconditions.checkNotNull(node);
52         Preconditions.checkNotNull(netconfEventSourceManager);
53         if (!isEventSource(node)) {
54             return null;
55         }
56         NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(node, netconfEventSourceManager);
57         nesr.updateStatus();
58         LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
59         return nesr;
60     }
61
62     private static boolean isEventSource(final Node node) {
63         final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
64         if (netconfNode == null) {
65             return false;
66         }
67         if (netconfNode.getAvailableCapabilities() == null) {
68             return false;
69         }
70         final List<AvailableCapability> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
71         if (capabilities == null || capabilities.isEmpty()) {
72             return false;
73         }
74         for (final AvailableCapability capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
75             if (capability.getCapability().startsWith(NOTIFICATION_CAPABILITY_PREFIX)) {
76                 return true;
77             }
78         }
79
80         return false;
81     }
82
83     private NetconfEventSourceRegistration(final Node node, final NetconfEventSourceManager netconfEventSourceManager) {
84         this.node = node;
85         this.netconfEventSourceManager = netconfEventSourceManager;
86         this.eventSourceRegistration = null;
87         this.currentNetconfConnStatus = ConnectionStatus.Connecting;
88     }
89
90     Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
91         return Optional.fromNullable(eventSourceRegistration);
92     }
93
94     NetconfNode getNetconfNode() {
95         return node.augmentation(NetconfNode.class);
96     }
97
98     void updateStatus() {
99         ConnectionStatus netconfConnStatus = getNetconfNode().getConnectionStatus();
100         LOG.info("Change status on node {}, new status is {}", this.node.getNodeId().getValue(), netconfConnStatus);
101         if (netconfConnStatus.equals(currentNetconfConnStatus)) {
102             return;
103         }
104         changeStatus(netconfConnStatus);
105     }
106
107     private static boolean checkConnectionStatusType(final ConnectionStatus status) {
108         return status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
109                 || status == ConnectionStatus.UnableToConnect;
110     }
111
112     private void changeStatus(final ConnectionStatus newStatus) {
113         Preconditions.checkNotNull(newStatus);
114         Preconditions.checkState(this.currentNetconfConnStatus != null);
115         if (!checkConnectionStatusType(newStatus)) {
116             throw new IllegalStateException("Unknown new Netconf Connection Status");
117         }
118         switch (this.currentNetconfConnStatus) {
119             case Connecting:
120             case UnableToConnect:
121                 if (newStatus == ConnectionStatus.Connected) {
122                     if (this.eventSourceRegistration == null) {
123                         registrationEventSource();
124                     } else {
125                         // reactivate stream on registered event source (invoke publish notification about connection)
126                         this.eventSourceRegistration.getInstance().reActivateStreams();
127                     }
128                 }
129                 break;
130             case Connected:
131                 if (newStatus == ConnectionStatus.Connecting || newStatus == ConnectionStatus.UnableToConnect) {
132                     // deactivate streams on registered event source (invoke publish notification about connection)
133                     this.eventSourceRegistration.getInstance().deActivateStreams();
134                 }
135                 break;
136             default:
137                 throw new IllegalStateException("Unknown current Netconf Connection Status");
138         }
139         this.currentNetconfConnStatus = newStatus;
140     }
141
142     private void registrationEventSource() {
143         final Optional<DOMMountPoint> domMountPoint = netconfEventSourceManager.getDomMounts()
144                 .getMountPoint(domMountPath(node.getNodeId()));
145         EventSourceRegistration<NetconfEventSource> registration = null;
146         if (domMountPoint.isPresent()/* && mountPoint.isPresent()*/) {
147             NetconfEventSourceMount mount = new NetconfEventSourceMount(node, domMountPoint.get());
148             final NetconfEventSource netconfEventSource = new NetconfEventSource(
149                     netconfEventSourceManager.getStreamMap(),
150                     mount,
151                     netconfEventSourceManager.getPublishService());
152             registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
153             LOG.info("Event source {} has been registered", node.getNodeId().getValue());
154         }
155         this.eventSourceRegistration = registration;
156     }
157
158     private static YangInstanceIdentifier domMountPath(final NodeId nodeId) {
159         return YangInstanceIdentifier.builder(NETCONF_DEVICE_DOM_PATH)
160                 .nodeWithKey(Node.QNAME, NODE_ID_QNAME, nodeId.getValue()).build();
161     }
162
163     private void closeEventSourceRegistration() {
164         if (getEventSourceRegistration().isPresent()) {
165             getEventSourceRegistration().get().close();
166         }
167     }
168
169     @Override
170     public void close() {
171         closeEventSourceRegistration();
172     }
173
174 }