2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding;
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;
51 public class NetconfBindingNotificationsImpl extends NetconfBindingAccessorImpl implements NetconfNotifications {
53 private static final Logger log = LoggerFactory.getLogger(NetconfAccessorImpl.class);
55 public NetconfBindingNotificationsImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint) {
56 super(accessor, dataBroker, mountpoint);
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);
67 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
68 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
69 log.info("Event listener triggering notification stream {} for node {}", streamName, getNodeId());
71 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
72 if (createSubscriptionInput == null) {
73 failMessage = "createSubscriptionInput is null for mountpoint " + getNodeId();
75 return rpcService.createSubscription(createSubscriptionInput);
77 } catch (NullPointerException e) {
78 failMessage = "createSubscription failed";
81 failMessage = "No RpcConsumerRegistry avaialble.";
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());
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());
101 public boolean isNotificationsSupported() {
107 * check if nc-notifications.yang is supported by the device
110 public boolean isNCNotificationsSupported() {
111 return getCapabilites().isSupportingNamespace(Netconf.QNAME);
115 public List<Stream> getNotificationStreams() {
116 final Class<Netconf> netconfClazz = Netconf.class;
117 InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
119 Netconf res = getTransactionUtils().readData(getDataBroker(),
120 LogicalDatastoreType.OPERATIONAL, streamsIID);
121 Streams streams = res.getStreams();
122 return YangHelper.getList(streams.getStream());
126 public Optional<NetconfNotifications> getNotificationAccessor() {
127 return Optional.of(this);