6716581a266eee263e659ca5145352825e7bd895
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding;
23
24 import com.google.common.util.concurrent.ListenableFuture;
25 import com.google.common.util.concurrent.SettableFuture;
26 import java.util.List;
27 import java.util.Optional;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
32 import org.opendaylight.mdsal.binding.api.DataBroker;
33 import org.opendaylight.mdsal.binding.api.MountPoint;
34 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class NetconfBindingNotificationsImpl extends NetconfBindingAccessorImpl implements NetconfNotifications {
52
53     private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
54
55     public NetconfBindingNotificationsImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint) {
56         super(accessor, dataBroker, mountpoint);
57     }
58
59     @Override
60     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(@NonNull String streamName) {
61         String failMessage = "";
62         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
63                 getMountpoint().getService(RpcConsumerRegistry.class);
64         if (optionalRpcConsumerService.isPresent()) {
65             final NotificationsService rpcService = optionalRpcConsumerService.get().getRpcService(NotificationsService.class);
66
67             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
68             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
69             log.info("Event listener triggering notification stream {} for node {}", streamName, getNodeId());
70             try {
71                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
72                 if (createSubscriptionInput == null) {
73                     failMessage = "createSubscriptionInput is null for mountpoint " + getNodeId();
74                 } else {
75                     return rpcService.createSubscription(createSubscriptionInput);
76                 }
77             } catch (NullPointerException e) {
78                 failMessage = "createSubscription failed";
79             }
80         } else {
81             failMessage = "No RpcConsumerRegistry avaialble.";
82         }
83         log.warn(failMessage);
84         RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
85         result.withError(ErrorType.APPLICATION, failMessage);
86         SettableFuture<RpcResult<CreateSubscriptionOutput>> future = SettableFuture.create();
87         future.set(result.build());
88         return future;
89     }
90
91     @Override
92     public void registerNotificationsStream(List<Stream> streamList) {
93         for (Stream stream : streamList) {
94             log.info("Stream Name = {}, Stream Description = {}", stream.getName().getValue(), stream.getDescription());
95             if (!(stream.getName().getValue().equals(NetconfNotifications.DefaultNotificationsStream))) // Since this stream is already registered
96                 registerNotificationsStream(stream.getName().getValue());
97         }
98     }
99
100     @Override
101     public boolean isNotificationsSupported() {
102         return false;
103     }
104
105
106     /**
107      * check if nc-notifications.yang is supported by the device
108      */
109     @Override
110     public boolean isNCNotificationsSupported() {
111         return getCapabilites().isSupportingNamespace(Netconf.QNAME);
112     }
113
114     @Override
115     public List<Stream> getNotificationStreams() {
116         final Class<Netconf> netconfClazz = Netconf.class;
117         InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
118
119         Netconf res = getTransactionUtils().readData(getDataBroker(),
120                 LogicalDatastoreType.OPERATIONAL, streamsIID);
121         Streams streams = res.getStreams();
122         return YangHelper.getList(streams.getStream());
123     }
124
125     @Override
126     public Optional<NetconfNotifications> getNotificationAccessor() {
127         return Optional.of(this);
128     }
129
130 }