2 * Copyright (c) 2015 Cisco Systems, Inc. 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.netconf.messagebus.eventsources.netconf;
10 import static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
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;
33 * Helper class to keep connection status of netconf node and event source registration object.
35 @Deprecated(forRemoval = true)
36 final class NetconfEventSourceRegistration implements AutoCloseable {
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
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";
47 private final Node node;
48 private final NetconfEventSourceManager netconfEventSourceManager;
49 private final BindingNormalizedNodeSerializer serializer;
51 private ConnectionStatus currentNetconfConnStatus;
52 private EventSourceRegistration<NetconfEventSource> eventSourceRegistration;
54 private NetconfEventSourceRegistration(final BindingNormalizedNodeSerializer serializer, final Node node,
55 final NetconfEventSourceManager netconfEventSourceManager) {
56 this.serializer = requireNonNull(serializer);
58 this.netconfEventSourceManager = netconfEventSourceManager;
59 this.eventSourceRegistration = null;
60 this.currentNetconfConnStatus = ConnectionStatus.Connecting;
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)) {
71 NetconfEventSourceRegistration nesr = new NetconfEventSourceRegistration(serializer, node,
72 netconfEventSourceManager);
74 LOG.debug("NetconfEventSourceRegistration for node {} has been initialized...", node.getNodeId().getValue());
78 private static boolean isEventSource(final Node node) {
79 final NetconfNode netconfNode = node.augmentation(NetconfNode.class);
80 if (netconfNode == null) {
83 if (netconfNode.getAvailableCapabilities() == null) {
86 final List<AvailableCapability> capabilities = netconfNode.getAvailableCapabilities().getAvailableCapability();
87 if (capabilities == null || capabilities.isEmpty()) {
90 for (final AvailableCapability capability : netconfNode.getAvailableCapabilities().getAvailableCapability()) {
91 if (capability.getCapability().startsWith(NOTIFICATION_CAPABILITY_PREFIX)) {
99 Optional<EventSourceRegistration<NetconfEventSource>> getEventSourceRegistration() {
100 return Optional.ofNullable(eventSourceRegistration);
103 NetconfNode getNetconfNode() {
104 return node.augmentation(NetconfNode.class);
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)) {
113 changeStatus(netconfConnStatus);
116 private static boolean checkConnectionStatusType(final ConnectionStatus status) {
117 return status == ConnectionStatus.Connected || status == ConnectionStatus.Connecting
118 || status == ConnectionStatus.UnableToConnect;
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");
127 switch (this.currentNetconfConnStatus) {
129 case UnableToConnect:
130 if (newStatus == ConnectionStatus.Connected) {
131 if (this.eventSourceRegistration == null) {
132 registrationEventSource();
134 // reactivate stream on registered event source (invoke publish notification about connection)
135 this.eventSourceRegistration.getInstance().reActivateStreams();
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();
146 throw new IllegalStateException("Unknown current Netconf Connection Status");
148 this.currentNetconfConnStatus = newStatus;
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(),
160 netconfEventSourceManager.getPublishService());
161 registration = netconfEventSourceManager.getEventSourceRegistry().registerEventSource(netconfEventSource);
162 LOG.info("Event source {} has been registered", node.getNodeId().getValue());
164 this.eventSourceRegistration = registration;
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();
172 private void closeEventSourceRegistration() {
173 if (getEventSourceRegistration().isPresent()) {
174 getEventSourceRegistration().get().close();
179 public void close() {
180 closeEventSourceRegistration();