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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
28 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
29 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.TransactionUtils;
30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
31 import org.opendaylight.mdsal.binding.api.DataBroker;
32 import org.opendaylight.mdsal.binding.api.MountPoint;
33 import org.opendaylight.mdsal.binding.api.NotificationService;
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.CreateSubscriptionInputBuilder;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.NotificationListener;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class NetconfBindingAccessorImpl extends NetconfAccessorImpl implements NetconfBindingAccessor {
52 private static final Logger log = LoggerFactory.getLogger(NetconfBindingAccessorImpl.class);
54 private final static GenericTransactionUtils GENERICTRANSACTIONUTILS = new GenericTransactionUtils();
56 private final DataBroker dataBroker;
57 private final MountPoint mountpoint;
58 final NotificationsService mountpointNotificationService;
61 * Contains all data to access and manage NETCONF device
63 * @param accessor with basic mountpoint information
64 * @param dataBroker to access node
65 * @param mountpoint of netconfNode
66 * @throws IllegalArgumentException
68 public NetconfBindingAccessorImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint)
69 throws IllegalArgumentException {
71 this.dataBroker = Objects.requireNonNull(dataBroker);
72 this.mountpoint = Objects.requireNonNull(mountpoint);
74 final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
75 mountpoint.getService(RpcConsumerRegistry.class);
76 if (optionalRpcConsumerService.isPresent()) {
77 mountpointNotificationService = optionalRpcConsumerService.get().getRpcService(NotificationsService.class);
79 throw new IllegalArgumentException("Can not process without rpcConsumerService service");
84 public DataBroker getDataBroker() {
89 public MountPoint getMountpoint() {
94 public TransactionUtils getTransactionUtils() {
95 return GENERICTRANSACTIONUTILS;
99 public @NonNull <T extends NotificationListener> ListenerRegistration<NotificationListener> doRegisterNotificationListener(
100 @NonNull T listener) {
101 log.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
102 final Optional<NotificationService> optionalNotificationService =
103 mountpoint.getService(NotificationService.class);
104 final NotificationService notificationService = optionalNotificationService.get();
105 final ListenerRegistration<NotificationListener> ranListenerRegistration =
106 notificationService.registerNotificationListener(listener);
107 log.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
108 mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
109 return ranListenerRegistration;
113 public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(
114 @NonNull String streamName) {
115 final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
116 if (streamName != null) {
117 createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
119 log.info("Event listener triggering notification stream '{}' for node {}", streamName, getNodeId());
120 return mountpointNotificationService.createSubscription(createSubscriptionInputBuilder.build());
124 public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream() {
125 return registerNotificationsStream((String)null);
129 public void registerNotificationsStream(List<Stream> streamList) {
130 for (Stream stream : streamList) {
132 StreamNameType streamName = stream.getName();
133 if (streamName != null) {
134 String streamNameValue = stream.getName().getValue();
135 log.info("Stream Name = {}, Stream Description = {}", streamNameValue, stream.getDescription());
136 if (!(streamNameValue.equals(DefaultNotificationsStream)))
137 // Register any not default stream. Default stream is already registered
138 registerNotificationsStream(streamNameValue);
140 log.warn("Ignore a stream without name");
146 public List<Stream> getNotificationStreams() {
147 final Class<Netconf> netconfClazz = Netconf.class;
148 InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
150 Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
152 Streams streams = res.getStreams();
153 if (streams != null) {
154 return YangHelper.getList(streams.nonnullStream());
157 return Collections.emptyList();