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.test;
 
  24 import java.util.Arrays;
 
  25 import java.util.Collection;
 
  26 import java.util.Optional;
 
  27 import org.eclipse.jdt.annotation.NonNull;
 
  28 import org.junit.Assert;
 
  29 import static org.junit.Assert.assertEquals;
 
  30 import org.junit.Test;
 
  31 import org.mockito.ArgumentCaptor;
 
  32 import org.mockito.ArgumentMatchers;
 
  33 import org.mockito.Mockito;
 
  34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
 
  35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfNodeStateServiceImpl;
 
  36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
 
  37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfCommunicatorManager;
 
  38 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding.NetconfBindingAccessorImpl;
 
  39 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomContext;
 
  40 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.NetconfDomAccessorImpl;
 
  41 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.example.TestNetconfHelper;
 
  42 import org.opendaylight.mdsal.binding.api.DataBroker;
 
  43 import org.opendaylight.mdsal.binding.api.MountPoint;
 
  44 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
 
  45 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 
  46 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 
  47 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
 
  48 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
 
  49 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 
  50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
 
  51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
 
  52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
 
  53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
 
  54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
 
  55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
 
  56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
 
  57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 
  58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
  59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
  60 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
  62 public class TestNetconfAccessorImpl extends Mockito {
 
  65     public void testConstruct() {
 
  66         NetconfCommunicatorManager netconfCommunicatorManager = mock(NetconfCommunicatorManager.class);
 
  67         DomContext domContext = mock(DomContext.class);
 
  68         String nodeIdString = "Test";
 
  69         String capabilityStringForNetworkElement = "network-element";
 
  70         NodeId nodeId = new NodeId(nodeIdString);
 
  71         NetconfNode testNode = TestNetconfHelper.getTestNode(nodeId, capabilityStringForNetworkElement)
 
  72                 .augmentation(NetconfNode.class);
 
  74         NetconfNodeStateServiceImpl netconfNodeStateService = mock(NetconfNodeStateServiceImpl.class);
 
  75         NetconfAccessorImpl netconfAccessor = new NetconfAccessorImpl(nodeId, testNode, netconfCommunicatorManager,
 
  76                 domContext, netconfNodeStateService);
 
  78         Assert.assertNotNull(netconfAccessor);
 
  80         NetconfAccessorImpl netconfAcessor2 = new NetconfAccessorImpl(netconfAccessor);
 
  82         Assert.assertNotNull(netconfAcessor2);
 
  86     public void testBindingNotifications() {
 
  87         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
 
  89         DataBroker dataBroker = mock(DataBroker.class);
 
  91         NotificationsService notificationService = mock(NotificationsService.class);
 
  93         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
 
  94         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
 
  96         MountPoint mountPoint = mock(MountPoint.class);
 
  97         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
 
 100         NetconfBindingAccessorImpl test =
 
 101                 new NetconfBindingAccessorImpl(netconfAccessor, dataBroker, mountPoint);
 
 103         String streamName = "NETCONF";
 
 104         test.registerNotificationsStream(streamName);
 
 106         //Capture parameters and assert them
 
 107         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
 
 108         verify(notificationService).createSubscription(captor.capture());
 
 110         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
 
 114     public void testNotificationList() {
 
 116         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
 
 118         DataBroker dataBroker = mock(DataBroker.class);
 
 120         NotificationsService notificationService = mock(NotificationsService.class);
 
 122         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
 
 123         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
 
 125         MountPoint mountPoint = mock(MountPoint.class);
 
 126         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
 
 130         NetconfBindingAccessorImpl test =
 
 131                 new NetconfBindingAccessorImpl(netconfAccessor, dataBroker, mountPoint);
 
 133         String streamName = NetconfAccessor.DefaultNotificationsStream + "ChangeIt";
 
 134         StreamNameType streamNameType = new StreamNameType(streamName);
 
 135         Stream stream = new StreamBuilder().setName(streamNameType).build();
 
 136         test.registerNotificationsStream(Arrays.asList(stream));
 
 138         //Capture parameters and assert them
 
 139         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
 
 140         verify(notificationService).createSubscription(captor.capture());
 
 142         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
 
 147     public void testNetconfDomNotification() {
 
 149         DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
 
 150         DomContext domContext = mock(DomContext.class);
 
 151         DOMRpcService domRpcService = mock(DOMRpcService.class);
 
 152         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
 
 153         DOMNotificationService domNotificationService = mock(DOMNotificationService.class);
 
 154         DOMMountPoint domMountPoint = mock(DOMMountPoint.class);
 
 156         when(domNotificationService.registerNotificationListener(any(DOMNotificationListener.class),
 
 157                 ArgumentMatchers.<Collection<Absolute>>any()))
 
 158                         .thenReturn(new ListenerRegistration<DOMNotificationListener>() {
 
 160                             public @NonNull DOMNotificationListener getInstance() {
 
 165                             public void close() {}
 
 168         YangInstanceIdentifier mountpointPath = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME).build();
 
 169         when(domMountPoint.getIdentifier()).thenReturn(mountpointPath);
 
 170         when(domMountPoint.getService(DOMNotificationService.class)).thenReturn(Optional.of(domNotificationService));
 
 171         when(domMountPoint.getService(DOMRpcService.class)).thenReturn(Optional.of(domRpcService));
 
 173         /* Remark: Throws WARN java.lang.UnsupportedOperationException
 
 174          *  "[main] WARN org.opendaylight.netconf.util.NetconfUtil -
 
 175          *  Unable to set namespace context, falling back to setPrefix()
 
 176          *  during initialization."
 
 178         NetconfDomAccessorImpl netconfDomAccessor =
 
 179                 new NetconfDomAccessorImpl(netconfAccessor, domDataBroker, domMountPoint, domContext);
 
 181         Collection<Absolute> types = Arrays.asList(Absolute.of(NetworkTopology.QNAME));
 
 182         DOMNotificationListener listener = (notification) -> System.out.println("Notification: " + notification);
 
 183         ListenerRegistration<DOMNotificationListener> res =
 
 184                 netconfDomAccessor.doRegisterNotificationListener(listener, types);
 
 186         //Capture parameters and assert them
 
 187         ArgumentCaptor<DOMNotificationListener> captor1 = ArgumentCaptor.forClass(DOMNotificationListener.class);
 
 188         @SuppressWarnings("unchecked")
 
 189         ArgumentCaptor<Collection<Absolute>> captor2 = ArgumentCaptor.forClass(Collection.class);
 
 190         verify(domNotificationService).registerNotificationListener(captor1.capture(), captor2.capture());
 
 192         assertEquals("Listener", listener, captor1.getValue());
 
 193         assertEquals("SchemaPath", types, captor2.getValue());