Merge "Remove password in properties"
[ccsdk/features.git] / sdnr / wt / netconfnode-state-service / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / netconfnodestateservice / impl / NetconfAccessorImpl.java
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.Optional;
23 import javax.annotation.Nonnull;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
26 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
27 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.TransactionUtils;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.MountPoint;
30 import org.opendaylight.mdsal.binding.api.NotificationService;
31 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
33 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.network.topology.topology.topology.types.TopologyNetconf;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.NotificationListener;
48 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
49 import org.opendaylight.yangtools.yang.common.RpcResult;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class NetconfAccessorImpl implements NetconfAccessor {
55
56     private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
57
58     private static final @NonNull InstanceIdentifier<Topology> NETCONF_TOPO_IID =
59             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
60                     new TopologyKey(new TopologyId(TopologyNetconf.QNAME.getLocalName())));
61
62     private final NodeId nodeId;
63     private final DataBroker dataBroker;
64     private final TransactionUtils transactionUtils;
65     private final MountPoint mountpoint;
66     private final NetconfNode netconfNode;
67     private final Capabilities capabilities;
68
69     /**
70      * Contains all data to access and manage netconf device
71      *
72      * @param nodeId of managed netconf node
73      * @param netconfNode information
74      * @param dataBroker to access node
75      * @param mountpoint of netconfNode
76      * @param transactionUtils with read an write functions
77      */
78     public NetconfAccessorImpl(NodeId nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
79             TransactionUtils transactionUtils) {
80         super();
81         this.nodeId = nodeId;
82         this.netconfNode = netconfNode;
83         this.dataBroker = dataBroker;
84         this.mountpoint = mountpoint;
85         this.transactionUtils = transactionUtils;
86
87         ConnectionStatus csts = netconfNode != null ? netconfNode.getConnectionStatus() : null;
88         if (csts == null) {
89             throw new IllegalStateException(String.format("connection status for %s is not connected", nodeId));
90         }
91         Capabilities tmp = Capabilities.getAvailableCapabilities(netconfNode);
92         if (tmp.getCapabilities().size() <= 0) {
93             throw new IllegalStateException(String.format("no capabilities found for %s", nodeId));
94         }
95         this.capabilities = tmp;
96     }
97
98     /**
99      * @param nodeId with uuid of managed netconf node
100      * @param dataBroker to access node
101      */
102     public NetconfAccessorImpl(String nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
103             TransactionUtils transactionUtils) {
104         this(new NodeId(nodeId), netconfNode, dataBroker, mountpoint, transactionUtils);
105     }
106
107     @Override
108     public NodeId getNodeId() {
109         return nodeId;
110     }
111
112     @Override
113     public DataBroker getDataBroker() {
114         return dataBroker;
115     }
116
117     @Override
118     public MountPoint getMountpoint() {
119         return mountpoint;
120     }
121
122     @Override
123     public TransactionUtils getTransactionUtils() {
124         return transactionUtils;
125     }
126
127     @Override
128     public NetconfNode getNetconfNode() {
129         return netconfNode;
130     }
131
132     @Override
133     public Capabilities getCapabilites() {
134         return capabilities;
135     }
136
137     @Override
138     public @NonNull <T extends NotificationListener> ListenerRegistration<NotificationListener> doRegisterNotificationListener(
139             @NonNull T listener) {
140         log.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
141         final Optional<NotificationService> optionalNotificationService =
142                 mountpoint.getService(NotificationService.class);
143         final NotificationService notificationService = optionalNotificationService.get();
144         final ListenerRegistration<NotificationListener> ranListenerRegistration =
145                 notificationService.registerNotificationListener(listener);
146         log.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
147                 mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
148         return ranListenerRegistration;
149     }
150
151     @Override
152     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(String streamName) {
153
154         String failMessage = "";
155         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
156                 mountpoint.getService(RpcConsumerRegistry.class);
157         if (optionalRpcConsumerService.isPresent()) {
158             final RpcConsumerRegistry rpcConsumerRegitry = optionalRpcConsumerService.get();
159             @Nonnull
160             final NotificationsService rpcService = rpcConsumerRegitry.getRpcService(NotificationsService.class);
161
162             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
163             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
164             log.info("Event listener triggering notification stream {} for node {}", streamName, nodeId);
165             try {
166                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
167                 if (createSubscriptionInput == null) {
168                     failMessage = "createSubscriptionInput is null for mountpoint " + nodeId;
169                 } else {
170                     return rpcService.createSubscription(createSubscriptionInput);
171                 }
172             } catch (NullPointerException e) {
173                 failMessage = "createSubscription failed";
174             }
175         } else {
176             failMessage = "No RpcConsumerRegistry avaialble.";
177         }
178         log.warn(failMessage);
179         RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
180         result.withError(ErrorType.APPLICATION, failMessage);
181         SettableFuture<RpcResult<CreateSubscriptionOutput>> res = SettableFuture.create();
182         res.set(result.build());
183         return res;
184     }
185
186
187 }