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 com.google.common.util.concurrent.SettableFuture;
 
  22 import java.util.Collections;
 
  23 import java.util.List;
 
  24 import java.util.Objects;
 
  25 import java.util.Optional;
 
  26 import org.eclipse.jdt.annotation.NonNull;
 
  27 import org.eclipse.jdt.annotation.Nullable;
 
  28 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
 
  29 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfBindingAccessor;
 
  30 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.TransactionUtils;
 
  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.NotificationService;
 
  35 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
 
  36 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 
  37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
 
  38 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInputBuilder;
 
  39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionOutput;
 
  40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
 
  41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
 
  42 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.Netconf;
 
  43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.Streams;
 
  44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
 
  45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
  46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
  47 import org.opendaylight.yangtools.yang.binding.NotificationListener;
 
  48 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
 
  49 import org.opendaylight.yangtools.yang.common.RpcResult;
 
  50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
  51 import org.slf4j.Logger;
 
  52 import org.slf4j.LoggerFactory;
 
  54 public class NetconfBindingAccessorImpl extends NetconfAccessorImpl implements NetconfBindingAccessor {
 
  56     private static final Logger log = LoggerFactory.getLogger(NetconfBindingAccessorImpl.class);
 
  58     private final static GenericTransactionUtils GENERICTRANSACTIONUTILS = new GenericTransactionUtils();
 
  60     private final DataBroker dataBroker;
 
  61     private final MountPoint mountpoint;
 
  64      * Contains all data to access and manage netconf device
 
  66      * @param nodeId of managed netconf node
 
  67      * @param netconfNode information
 
  68      * @param dataBroker to access node
 
  69      * @param mountpoint of netconfNode
 
  71     public NetconfBindingAccessorImpl(NetconfAccessorImpl accessor, DataBroker dataBroker, MountPoint mountpoint) {
 
  73         this.dataBroker = Objects.requireNonNull(dataBroker);
 
  74         this.mountpoint = Objects.requireNonNull(mountpoint);
 
  78     public DataBroker getDataBroker() {
 
  83     public MountPoint getMountpoint() {
 
  88     public TransactionUtils getTransactionUtils() {
 
  89         return GENERICTRANSACTIONUTILS;
 
  93     public @NonNull <T extends NotificationListener> ListenerRegistration<NotificationListener> doRegisterNotificationListener(
 
  94             @NonNull T listener) {
 
  95         log.info("Begin register listener for Mountpoint {}", mountpoint.getIdentifier().toString());
 
  96         final Optional<NotificationService> optionalNotificationService =
 
  97                 mountpoint.getService(NotificationService.class);
 
  98         final NotificationService notificationService = optionalNotificationService.get();
 
  99         final ListenerRegistration<NotificationListener> ranListenerRegistration =
 
 100                 notificationService.registerNotificationListener(listener);
 
 101         log.info("End registration listener for Mountpoint {} Listener: {} Result: {}",
 
 102                 mountpoint.getIdentifier().toString(), optionalNotificationService, ranListenerRegistration);
 
 103         return ranListenerRegistration;
 
 108     public ListenableFuture<RpcResult<CreateSubscriptionOutput>> registerNotificationsStream(
 
 109             @NonNull String streamName) {
 
 110         String failMessage = "";
 
 111         final Optional<RpcConsumerRegistry> optionalRpcConsumerService =
 
 112                 getMountpoint().getService(RpcConsumerRegistry.class);
 
 113         if (optionalRpcConsumerService.isPresent()) {
 
 114             final NotificationsService rpcService =
 
 115                     optionalRpcConsumerService.get().getRpcService(NotificationsService.class);
 
 117             final CreateSubscriptionInputBuilder createSubscriptionInputBuilder = new CreateSubscriptionInputBuilder();
 
 118             createSubscriptionInputBuilder.setStream(new StreamNameType(streamName));
 
 119             log.info("Event listener triggering notification stream {} for node {}", streamName, getNodeId());
 
 121                 CreateSubscriptionInput createSubscriptionInput = createSubscriptionInputBuilder.build();
 
 122                 if (createSubscriptionInput == null) {
 
 123                     failMessage = "createSubscriptionInput is null for mountpoint " + getNodeId();
 
 125                     // Regular case, return value
 
 126                     return rpcService.createSubscription(createSubscriptionInput);
 
 128             } catch (NullPointerException e) {
 
 129                 failMessage = "createSubscription failed";
 
 132             failMessage = "No RpcConsumerRegistry avaialble.";
 
 134         //Be here only in case of problem and return failed indication
 
 135         log.warn(failMessage);
 
 136         RpcResultBuilder<CreateSubscriptionOutput> result = RpcResultBuilder.failed();
 
 137         result.withError(ErrorType.APPLICATION, failMessage);
 
 138         SettableFuture<RpcResult<CreateSubscriptionOutput>> future = SettableFuture.create();
 
 139         future.set(result.build());
 
 144     public void registerNotificationsStream(List<Stream> streamList) {
 
 145         for (Stream stream : streamList) {
 
 147             StreamNameType streamName = stream.getName();
 
 148             if (streamName != null) {
 
 149                 String streamNameValue = stream.getName().getValue();
 
 150                 log.info("Stream Name = {}, Stream Description = {}", streamNameValue, stream.getDescription());
 
 151                 if (!(streamNameValue.equals(DefaultNotificationsStream)))
 
 152                     // Register any not default stream. Default stream is already registered
 
 153                     registerNotificationsStream(streamNameValue);
 
 155                 log.warn("Ignore a stream without name");
 
 161     public List<Stream> getNotificationStreams() {
 
 162         final Class<Netconf> netconfClazz = Netconf.class;
 
 163         InstanceIdentifier<Netconf> streamsIID = InstanceIdentifier.builder(netconfClazz).build();
 
 165         Netconf res = getTransactionUtils().readData(getDataBroker(), LogicalDatastoreType.OPERATIONAL, streamsIID);
 
 167             Streams streams = res.getStreams();
 
 168             if (streams != null) {
 
 169                 return YangHelper.getList(streams.nonnullStream());
 
 172         return Collections.emptyList();