6f3a592c87c9aba34be3aa291ae119c12f6728a5
[ccsdk/features.git] /
1 /*
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
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.access.binding;
19
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;
49
50 public class NetconfBindingAccessorImpl extends NetconfAccessorImpl implements NetconfBindingAccessor {
51
52     private static final Logger log = LoggerFactory.getLogger(NetconfBindingAccessorImpl.class);
53
54     private final static GenericTransactionUtils GENERICTRANSACTIONUTILS = new GenericTransactionUtils();
55
56     private final DataBroker dataBroker;
57     private final MountPoint mountpoint;
58     final NotificationsService mountpointNotificationService;
59
60     /**
61      * Contains all data to access and manage NETCONF device
62      *
63      * @param accessor with basic mountpoint information
64      * @param dataBroker to access node
65      * @param mountpoint of netconfNode
66      * @throws IllegalArgumentException
67      */
68     public NetconfBindingAccessorImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint)
69             throws IllegalArgumentException {
70         super(accessor);
71         this.dataBroker = Objects.requireNonNull(dataBroker);
72         this.mountpoint = Objects.requireNonNull(mountpoint);
73
74         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
75                 mountpoint.getService(RpcConsumerRegistry.class);
76         if (optionalRpcConsumerService.isPresent()) {
77             mountpointNotificationService = optionalRpcConsumerService.get().getRpcService(NotificationsService.class);
78         } else {
79             throw new IllegalArgumentException("Can not process without rpcConsumerService service");
80         }
81     }
82
83     @Override
84     public DataBroker getDataBroker() {
85         return dataBroker;
86     }
87
88     @Override
89     public MountPoint getMountpoint() {
90         return mountpoint;
91     }
92
93     @Override
94     public TransactionUtils getTransactionUtils() {
95         return GENERICTRANSACTIONUTILS;
96     }
97
98     @Override
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;
110     }
111
112     @Override
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));
118         }
119         log.info("Event listener triggering notification stream '{}' for node {}", streamName, getNodeId());
120         return mountpointNotificationService.createSubscription(createSubscriptionInputBuilder.build());
121     }
122
123     @Override
124     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream() {
125         return registerNotificationsStream((String)null);
126     }
127
128     @Override
129     public void registerNotificationsStream(List<Stream> streamList) {
130         for (Stream stream : streamList) {
131             @Nullable
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);
139             } else {
140                 log.warn("Ignore a stream without name");
141             }
142         }
143     }
144
145     @Override
146     public List<Stream> getNotificationStreams() {
147         final Class<Netconf> netconfClazz = Netconf.class;
148         InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
149
150         Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
151         if (res != null) {
152             Streams streams = res.getStreams();
153             if (streams != null) {
154                 return YangHelper.getList(streams.nonnullStream());
155             }
156         }
157         return Collections.emptyList();
158     }
159
160 }