46ff07b2ca546547428cda961540346120ca2444
[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.Collections;
27 import java.util.List;
28 import java.util.Optional;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
34 import org.opendaylight.mdsal.binding.api.DataBroker;
35 import org.opendaylight.mdsal.binding.api.MountPoint;
36 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 public class NetconfBindingNotificationsImpl extends NetconfBindingAccessorImpl implements NetconfNotifications {
54
55     private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
56
57     public NetconfBindingNotificationsImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint) {
58         super(accessor, dataBroker, mountpoint);
59     }
60
61     @Override
62     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(
63             @NonNull String streamName) {
64         String failMessage = "";
65         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
66                 getMountpoint().getService(RpcConsumerRegistry.class);
67         if (optionalRpcConsumerService.isPresent()) {
68             final NotificationsService rpcService =
69                     optionalRpcConsumerService.get().getRpcService(NotificationsService.class);
70
71             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
72             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
73             log.info("Event listener triggering notification stream {} for node {}", streamName, getNodeId());
74             try {
75                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
76                 if (createSubscriptionInput == null) {
77                     failMessage = "createSubscriptionInput is null for mountpoint " + getNodeId();
78                 } else {
79                     // Regular case, return value
80                     return rpcService.createSubscription(createSubscriptionInput);
81                 }
82             } catch (NullPointerException e) {
83                 failMessage = "createSubscription failed";
84             }
85         } else {
86             failMessage = "No RpcConsumerRegistry avaialble.";
87         }
88         //Be here only in case of problem and return failed indication
89         log.warn(failMessage);
90         RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
91         result.withError(ErrorType.APPLICATION, failMessage);
92         SettableFuture<RpcResult<CreateSubscriptionOutput>> future = SettableFuture.create();
93         future.set(result.build());
94         return future;
95     }
96
97     @Override
98     public void registerNotificationsStream(List<Stream> streamList) {
99         for (Stream stream : streamList) {
100             @Nullable
101             StreamNameType streamName = stream.getName();
102             if (streamName != null) {
103                 String streamNameValue = stream.getName().getValue();
104                 log.info("Stream Name = {}, Stream Description = {}", streamNameValue, stream.getDescription());
105                 if (!(streamNameValue.equals(NetconfNotifications.DefaultNotificationsStream)))
106                     // Register any not default stream. Default stream is already registered
107                     registerNotificationsStream(streamNameValue);
108             } else {
109                 log.warn("Ignore a stream without name");
110             }
111         }
112     }
113
114     @Override
115     public boolean isNotificationsSupported() {
116         return false;
117     }
118
119
120     /**
121      * check if nc-notifications.yang is supported by the device
122      */
123     @Override
124     public boolean isNCNotificationsSupported() {
125         return getCapabilites().isSupportingNamespace(Netconf.QNAME);
126     }
127
128     @Override
129     public List<Stream> getNotificationStreams() {
130         final Class<Netconf> netconfClazz = Netconf.class;
131         InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
132
133         Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
134         if (res != null) {
135             Streams streams = res.getStreams();
136             if (streams != null) {
137                 return YangHelper.getList(streams.nonnullStream());
138             }
139         }
140         return Collections.emptyList();
141     }
142
143     @Override
144     public Optional<NetconfNotifications> getNotificationAccessor() {
145         return Optional.of(this);
146     }
147
148 }