65b8e6f569319c438935046d443570d0e9e325a6
[ccsdk/features.git] /
1 /*
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
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
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
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl;
19
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;
59
60 public class NetconfAccessorImpl implements NetconfAccessor {
61
62     private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
63
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())));
67
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;
74
75     /**
76      * Contains all data to access and manage netconf device
77      *
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
83      */
84     public NetconfAccessorImpl(NodeId nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
85             TransactionUtils transactionUtils) {
86         super();
87         this.nodeId = nodeId;
88         this.netconfNode = netconfNode;
89         this.dataBroker = dataBroker;
90         this.mountpoint = mountpoint;
91         this.transactionUtils = transactionUtils;
92
93         ConnectionStatus csts = netconfNode != null ? netconfNode.getConnectionStatus() : null;
94         if (csts == null) {
95             throw new IllegalStateException(String.format("connection status for %s is not connected", nodeId));
96         }
97         Capabilities tmp = Capabilities.getAvailableCapabilities(netconfNode);
98         if (tmp.getCapabilities().size() <= 0) {
99             throw new IllegalStateException(String.format("no capabilities found for %s", nodeId));
100         }
101         this.capabilities = tmp;
102     }
103
104     /**
105      * @param nodeId with uuid of managed netconf node
106      * @param dataBroker to access node
107      */
108     public NetconfAccessorImpl(String nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
109             TransactionUtils transactionUtils) {
110         this(new NodeId(nodeId), netconfNode, dataBroker, mountpoint, transactionUtils);
111     }
112
113     @Override
114     public NodeId getNodeId() {
115         return nodeId;
116     }
117
118     @Override
119     public DataBroker getDataBroker() {
120         return dataBroker;
121     }
122
123     @Override
124     public MountPoint getMountpoint() {
125         return mountpoint;
126     }
127
128     @Override
129     public TransactionUtils getTransactionUtils() {
130         return transactionUtils;
131     }
132
133     @Override
134     public NetconfNode getNetconfNode() {
135         return netconfNode;
136     }
137
138     @Override
139     public Capabilities getCapabilites() {
140         return capabilities;
141     }
142
143     @Override
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;
155     }
156
157     @Override
158     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(String streamName) {
159
160         String failMessage = "";
161         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
162                 mountpoint.getService(RpcConsumerRegistry.class);
163         if (optionalRpcConsumerService.isPresent()) {
164             final RpcConsumerRegistry rpcConsumerRegitry = optionalRpcConsumerService.get();
165             @Nonnull
166             final NotificationsService rpcService = rpcConsumerRegitry.getRpcService(NotificationsService.class);
167
168             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
169             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
170             log.info("Event listener triggering notification stream {} for node {}", streamName, nodeId);
171             try {
172                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
173                 if (createSubscriptionInput == null) {
174                     failMessage = "createSubscriptionInput is null for mountpoint " + nodeId;
175                 } else {
176                     return rpcService.createSubscription(createSubscriptionInput);
177                 }
178             } catch (NullPointerException e) {
179                 failMessage = "createSubscription failed";
180             }
181         } else {
182             failMessage = "No RpcConsumerRegistry avaialble.";
183         }
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());
189         return res;
190     }
191
192     @Override
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());
198         }
199     }
200
201     /**
202      * check if nc-notifications.yang is supported by the device
203      */
204     @Override
205     public boolean isNCNotificationsSupported() {
206         Capabilities capabilities = getCapabilites();
207         if (capabilities.isSupportingNamespace(Netconf.QNAME)) {
208             return true;
209         }
210         return false;
211     }
212
213     @Override
214     public List<Stream> getNotificationStreams() {
215         final Class<Netconf> netconfClazz = Netconf.class;
216         InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
217
218         Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
219         Streams streams = res.getStreams();
220         return YangHelper.getList(streams.getStream());
221     }
222
223
224 }