2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt
4 * =================================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
22 import java.util.List;
23 import java.util.Optional;
24 import javax.annotation.Nonnull;
25 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
26 import org.eclipse.jdt.annotation.NonNull;
27 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
28 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
29 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.TransactionUtils;
30 import org.opendaylight.mdsal.binding.api.DataBroker;
31 import org.opendaylight.mdsal.binding.api.MountPoint;
32 import org.opendaylight.mdsal.binding.api.NotificationService;
33 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
50 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.binding.NotificationListener;
54 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
60 public class NetconfAccessorImpl implements NetconfAccessor {
62 private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
64 private static final @NonNull InstanceIdentifier<Topology> NETCONF_TOPO_IID =
65 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
66 new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
68 private final NodeId nodeId;
69 private final DataBroker dataBroker;
70 private final TransactionUtils transactionUtils;
71 private final MountPoint mountpoint;
72 private final NetconfNode netconfNode;
73 private final Capabilities capabilities;
76 * Contains all data to access and manage netconf device
78 * @param nodeId of managed netconf node
79 * @param netconfNode information
80 * @param dataBroker to access node
81 * @param mountpoint of netconfNode
82 * @param transactionUtils with read an write functions
84 public NetconfAccessorImpl(NodeId nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
85 TransactionUtils transactionUtils) {
88 this.netconfNode = netconfNode;
89 this.dataBroker = dataBroker;
90 this.mountpoint = mountpoint;
91 this.transactionUtils = transactionUtils;
93 ConnectionStatus csts = netconfNode != null ? netconfNode.getConnectionStatus() : null;
95 throw new IllegalStateException(String.format("connection status for %s is not connected", nodeId));
97 Capabilities tmp = Capabilities.getAvailableCapabilities(netconfNode);
98 if (tmp.getCapabilities().size() <= 0) {
99 throw new IllegalStateException(String.format("no capabilities found for %s", nodeId));
101 this.capabilities = tmp;
105 * @param nodeId with uuid of managed netconf node
106 * @param dataBroker to access node
108 public NetconfAccessorImpl(String nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
109 TransactionUtils transactionUtils) {
110 this(new NodeId(nodeId), netconfNode, dataBroker, mountpoint, transactionUtils);
114 public NodeId getNodeId() {
119 public DataBroker getDataBroker() {
124 public MountPoint getMountpoint() {
129 public TransactionUtils getTransactionUtils() {
130 return transactionUtils;
134 public NetconfNode getNetconfNode() {
139 public Capabilities getCapabilites() {
144 public @NonNull <T extends NotificationListener> ListenerRegistration<NotificationListener> doRegisterNotificationListener(
145 @NonNull T listener) {
146 log.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
147 final Optional<NotificationService> optionalNotificationService =
148 mountpoint.getService(NotificationService.class);
149 final NotificationService notificationService = optionalNotificationService.get();
150 final ListenerRegistration<NotificationListener> ranListenerRegistration =
151 notificationService.registerNotificationListener(listener);
152 log.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
153 mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
154 return ranListenerRegistration;
158 public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(String streamName) {
160 String failMessage = "";
161 final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
162 mountpoint.getService(RpcConsumerRegistry.class);
163 if (optionalRpcConsumerService.isPresent()) {
164 final RpcConsumerRegistry rpcConsumerRegitry = optionalRpcConsumerService.get();
166 final NotificationsService rpcService = rpcConsumerRegitry.getRpcService(NotificationsService.class);
168 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
169 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
170 log.info("Event listener triggering notification stream {} for node {}", streamName, nodeId);
172 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
173 if (createSubscriptionInput == null) {
174 failMessage = "createSubscriptionInput is null for mountpoint " + nodeId;
176 return rpcService.createSubscription(createSubscriptionInput);
178 } catch (NullPointerException e) {
179 failMessage = "createSubscription failed";
182 failMessage = "No RpcConsumerRegistry avaialble.";
184 log.warn(failMessage);
185 RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
186 result.withError(ErrorType.APPLICATION, failMessage);
187 SettableFuture<RpcResult<CreateSubscriptionOutput>> res = SettableFuture.create();
188 res.set(result.build());
193 public void registerNotificationsStream(List<Stream> streamList) {
194 for (Stream stream : streamList) {
195 log.info("Stream Name = {}, Stream Description = {}", stream.getName().getValue(), stream.getDescription());
196 if (!(stream.getName().getValue().equals(NetconfAccessor.DefaultNotificationsStream))) // Since this stream is already registered
197 registerNotificationsStream(stream.getName().getValue());
202 * check if nc-notifications.yang is supported by the device
205 public boolean isNCNotificationsSupported() {
206 Capabilities capabilities = getCapabilites();
207 if (capabilities.isSupportingNamespace(Netconf.QNAME)) {
214 public List<Stream> getNotificationStreams() {
215 final Class<Netconf> netconfClazz = Netconf.class;
216 InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
218 Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
219 Streams streams = res.getStreams();
220 return YangHelper.getList(streams.getStream());