Merge "Reformat sdnr devicemanager-onf to ONAP code style"
[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) 2019 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.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.yang.binding.NotificationListener;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class NetconfAccessorImpl implements NetconfAccessor {
49
50     private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
51
52     private final NodeId nodeId;
53     private final DataBroker dataBroker;
54     private final TransactionUtils transactionUtils;
55     private final MountPoint mountpoint;
56     private final NetconfNode netconfNode;
57     private final Capabilities capabilities;
58
59     /**
60      * Contains all data to access and manage netconf device
61      * 
62      * @param nodeId of managed netconf node
63      * @param netconfNode information
64      * @param dataBroker to access node
65      * @param mountpoint of netconfNode
66      * @param transactionUtils with read an write functions
67      */
68     public NetconfAccessorImpl(NodeId nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
69             TransactionUtils transactionUtils) {
70         super();
71         this.nodeId = nodeId;
72         this.netconfNode = netconfNode;
73         this.dataBroker = dataBroker;
74         this.mountpoint = mountpoint;
75         this.transactionUtils = transactionUtils;
76
77         ConnectionStatus csts = netconfNode != null ? netconfNode.getConnectionStatus() : null;
78         this.capabilities = Capabilities.getAvailableCapabilities(csts != null ? netconfNode : null);
79     }
80
81     /**
82      * @param nodeId with uuid of managed netconf node
83      * @param dataBroker to access node
84      */
85     public NetconfAccessorImpl(String nodeId, NetconfNode netconfNode, DataBroker dataBroker, MountPoint mountpoint,
86             TransactionUtils transactionUtils) {
87         this(new NodeId(nodeId), netconfNode, dataBroker, mountpoint, transactionUtils);
88     }
89
90     @Override
91     public NodeId getNodeId() {
92         return nodeId;
93     }
94
95     @Override
96     public DataBroker getDataBroker() {
97         return dataBroker;
98     }
99
100     @Override
101     public MountPoint getMountpoint() {
102         return mountpoint;
103     }
104
105     @Override
106     public TransactionUtils getTransactionUtils() {
107         return transactionUtils;
108     }
109
110     @Override
111     public NetconfNode getNetconfNode() {
112         return netconfNode;
113     }
114
115     @Override
116     public Capabilities getCapabilites() {
117         return capabilities;
118     }
119
120     @Override
121     public @NonNull <T extends NotificationListener> ListenerRegistration<NotificationListener> doRegisterNotificationListener(
122             @NonNull T listener) {
123         log.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
124         final Optional<NotificationService> optionalNotificationService =
125                 mountpoint.getService(NotificationService.class);
126         final NotificationService notificationService = optionalNotificationService.get();
127         final ListenerRegistration<NotificationListener> ranListenerRegistration =
128                 notificationService.registerNotificationListener(listener);
129         log.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
130                 mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
131         return ranListenerRegistration;
132     }
133
134     @Override
135     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(String streamName) {
136
137         String failMessage = "";
138         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
139                 mountpoint.getService(RpcConsumerRegistry.class);
140         if (optionalRpcConsumerService.isPresent()) {
141             final RpcConsumerRegistry rpcConsumerRegitry = optionalRpcConsumerService.get();
142             @Nonnull
143             final NotificationsService rpcService = rpcConsumerRegitry.getRpcService(NotificationsService.class);
144
145             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
146             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
147             log.info("Event listener triggering notification stream {} for node {}", streamName, nodeId);
148             try {
149                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
150                 if (createSubscriptionInput == null) {
151                     failMessage = "createSubscriptionInput is null for mountpoint " + nodeId;
152                 } else {
153                     return rpcService.createSubscription(createSubscriptionInput);
154                 }
155             } catch (NullPointerException e) {
156                 failMessage = "createSubscription failed";
157             }
158         } else {
159             failMessage = "No RpcConsumerRegistry avaialble.";
160         }
161         log.warn(failMessage);
162         RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
163         result.withError(ErrorType.APPLICATION, failMessage);
164         SettableFuture<RpcResult<CreateSubscriptionOutput>> res = SettableFuture.create();
165         res.set(result.build());
166         return res;
167     }
168
169
170 }