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