2  * ============LICENSE_START======================================================= ONAP : ccsdk
 
   3  * feature sdnr wt ================================================================================
 
   4  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
 
   5  * ================================================================================ Licensed under
 
   6  * the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
 
   7  * with the License. You may obtain a copy of the License at
 
   9  * http://www.apache.org/licenses/LICENSE-2.0
 
  11  * Unless required by applicable law or agreed to in writing, software distributed under the License
 
  12  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 
  13  * or implied. See the License for the specific language governing permissions and limitations under
 
  14  * the License. ============LICENSE_END=========================================================
 
  16 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test;
 
  18 import static org.mockito.Mockito.mock;
 
  19 import static org.mockito.Mockito.when;
 
  21 import com.google.common.util.concurrent.ListenableFuture;
 
  23 import static org.mockito.Mockito.verify;
 
  24 import static org.mockito.Mockito.times;
 
  25 import static org.junit.Assert.assertEquals;
 
  26 import static org.junit.Assert.assertNotNull;
 
  27 import static org.junit.Assert.assertTrue;
 
  29 import java.io.FileNotFoundException;
 
  30 import java.io.IOException;
 
  31 import java.nio.file.Files;
 
  32 import java.nio.file.Path;
 
  33 import java.nio.file.Paths;
 
  34 import java.util.Arrays;
 
  35 import java.util.Collection;
 
  36 import java.util.concurrent.ExecutionException;
 
  37 import org.junit.AfterClass;
 
  38 import org.junit.BeforeClass;
 
  39 import org.junit.Test;
 
  40 import org.mockito.ArgumentCaptor;
 
  41 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.IEntityDataProvider;
 
  42 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
 
  43 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener;
 
  44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
 
  45 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.VesNotificationListener;
 
  46 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.GenericTransactionUtils;
 
  47 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfAccessorImpl;
 
  48 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfNodeStateServiceImpl;
 
  49 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.rpc.NetconfnodeStateServiceRpcApiImpl;
 
  50 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.ClusterSingletonServiceProviderMock;
 
  51 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.DataBrokerNetconfMock;
 
  52 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointMock;
 
  53 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointServiceMock;
 
  54 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.NotificationPublishServiceMock;
 
  55 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.RpcProviderRegistryMock;
 
  56 import org.opendaylight.mdsal.binding.api.DataObjectModification;
 
  57 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
 
  58 import org.opendaylight.mdsal.binding.api.DataTreeModification;
 
  59 import org.opendaylight.mdsal.binding.api.MountPointService;
 
  60 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
 
  61 import org.opendaylight.mdsal.binding.api.RpcProviderService;
 
  62 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 
  63 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
 
  64 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
 
  65 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
 
  66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.AttributeChangeNotification;
 
  67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.AttributeChangeNotificationBuilder;
 
  68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotification;
 
  69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotificationBuilder;
 
  70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusInputBuilder;
 
  71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutput;
 
  72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutputBuilder;
 
  73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationInputBuilder;
 
  74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationOutput;
 
  75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationInputBuilder;
 
  76 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationOutput;
 
  77 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 
  78 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 
  79 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
 
  80 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 
  81 import org.opendaylight.yangtools.yang.common.RpcResult;
 
  82 import org.slf4j.Logger;
 
  83 import org.slf4j.LoggerFactory;
 
  86 public class TestNetconfNodeStateService {
 
  88     private static Path KARAF_ETC = Paths.get("etc");
 
  89     private static NetconfNodeStateServiceImpl netconfStateService;
 
  90     private static MountPointMock mountPoint;
 
  91     private static DataBrokerNetconfMock dataBrokerNetconf;
 
  93     private static final Logger LOG = LoggerFactory.getLogger(TestNetconfNodeStateService.class);
 
  96     public static void before() throws InterruptedException, IOException {
 
  98         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
 
  99         // Call System property to get the classpath value
 
 100         Path etc = KARAF_ETC;
 
 103         System.out.println("Create empty:" + etc.toString());
 
 104         Files.createDirectories(etc);
 
 107         dataBrokerNetconf = new DataBrokerNetconfMock();
 
 108         dataBrokerNetconf.newReadWriteTransaction();
 
 109         mountPoint = new MountPointMock();
 
 110         ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock();
 
 111         MountPointService mountPointService = new MountPointServiceMock(mountPoint);
 
 112         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
 
 113         RpcProviderService rpcProviderRegistry = new RpcProviderRegistryMock();
 
 114         IEntityDataProvider entityProviderMock = mock(IEntityDataProvider.class);
 
 116         // start using blueprint interface
 
 117         netconfStateService = new NetconfNodeStateServiceImpl();
 
 119         netconfStateService.setDataBroker(dataBrokerNetconf);
 
 120         netconfStateService.setMountPointService(mountPointService);
 
 121         netconfStateService.setNotificationPublishService(notificationPublishService);
 
 122         netconfStateService.setRpcProviderRegistry(rpcProviderRegistry);
 
 123         netconfStateService.setClusterSingletonService(clusterSingletonService);
 
 124         netconfStateService.setEntityDataProvider(entityProviderMock);
 
 125         netconfStateService.init();
 
 126         System.out.println("Initialization done");
 
 130     public static void after() throws InterruptedException, IOException {
 
 131         System.out.println("Start shutdown");
 
 132         // close using blueprint interface
 
 133         netconfStateService.close();
 
 139     public void test1() {
 
 141         System.out.println("Test1: Verify init state");
 
 142         assertTrue("Devicemanager not initialized", netconfStateService.isInitializationSuccessful());
 
 147     public void test2() {
 
 149         System.out.println("Test2: Register state listener");
 
 151         NetconfNodeStateListener nSL = mock(NetconfNodeStateListener.class);
 
 152         ListenerRegistration<NetconfNodeStateListener> res = netconfStateService.registerNetconfNodeStateListener(nSL);
 
 153         assertNotNull("Result should be null", res);
 
 159     public void test3() {
 
 161         System.out.println("Test3: Register connect listener");
 
 163         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
 
 164         ListenerRegistration<NetconfNodeConnectListener> res =
 
 165                 netconfStateService.registerNetconfNodeConnectListener(nCL);
 
 166         assertNotNull("Result should be null", res);
 
 172     public void test4() {
 
 173         System.out.println("Test4: Get status listener");
 
 174         GetStatusInputBuilder inputBuilder = new GetStatusInputBuilder();
 
 175         GetStatusOutputBuilder res = netconfStateService.getStatus(inputBuilder.build());
 
 176         assertNotNull("Result should be null", res);
 
 179     @SuppressWarnings("unchecked")
 
 181     public void test5OnConnect() throws InterruptedException {
 
 182         System.out.println("Test5: On Connect");
 
 183         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
 
 184         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
 
 185         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
 
 187         String nodeIdString = "Test";
 
 188         NodeId nodeId = new NodeId(nodeIdString);
 
 189         NodeBuilder nodeBuilder = new NodeBuilder();
 
 190         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
 
 191         nodeBuilder.setNodeId(nodeId);
 
 192         Node rootNode = nodeBuilder.build();
 
 193         NetconfAccessor acessor = new NetconfAccessorImpl(nodeId, rootNodeNetconf, mountPoint.getDataBroker(),
 
 194                 mountPoint, new GenericTransactionUtils());
 
 196         DataObjectModification<Node> dom = mock(DataObjectModification.class);
 
 197         when(dom.getDataAfter()).thenReturn(rootNode);
 
 198         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
 
 200         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
 
 201         when(ntn.getRootNode()).thenReturn(dom);
 
 203         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
 
 204         netconfStateService.registerNetconfNodeConnectListener(nCL);
 
 205         mountPoint.setDatabrokerAbsent(false);
 
 207         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
 
 208         dataBrokerNetconf.sendClusteredChanges(changes);
 
 209         dataBrokerNetconf.sendChanges(changes);
 
 211         //verify that it was called one time and nodeId is the expected
 
 212         ArgumentCaptor<NetconfAccessor> varArgs = ArgumentCaptor.forClass(NetconfAccessor.class);
 
 213         verify(nCL).onEnterConnected(varArgs.capture());
 
 214         System.out.println("Accessor " + varArgs.getValue().getNodeId());
 
 215         assertEquals(nodeIdString, varArgs.getValue().getNodeId().getValue());
 
 219     @SuppressWarnings("unchecked")
 
 221     public void test6Update() {
 
 222         System.out.println("Test6: OnChange");
 
 223         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
 
 224         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
 
 225         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
 
 227         NodeBuilder nodeBuilder = new NodeBuilder();
 
 228         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
 
 229         nodeBuilder.setNodeId(new NodeId("Test"));
 
 230         Node rootNodeAfter = nodeBuilder.build();
 
 232         DataObjectModification<Node> dom = mock(DataObjectModification.class);
 
 233         when(dom.getDataBefore()).thenReturn(rootNodeAfter);
 
 234         when(dom.getDataAfter()).thenReturn(rootNodeAfter);
 
 235         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
 
 237         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
 
 238         when(ntn.getRootNode()).thenReturn(dom);
 
 240         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
 
 241         dataBrokerNetconf.sendClusteredChanges(changes);
 
 242         dataBrokerNetconf.sendChanges(changes);
 
 246     public void test7ApiStatus() throws InterruptedException, ExecutionException {
 
 248         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
 
 250         GetStatusInputBuilder statusInput = new GetStatusInputBuilder();
 
 251         ListenableFuture<RpcResult<GetStatusOutput>> statusOutput = api.getStatus(statusInput.build());
 
 252         RpcResult<GetStatusOutput> res = statusOutput.get();
 
 253         GetStatusOutput output = res.getResult();
 
 254         System.out.println("Output " + output);
 
 259     public void test8ApiPushFault() throws InterruptedException, ExecutionException {
 
 261         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
 
 263         VesNotificationListener vNL = mock(VesNotificationListener.class);
 
 264         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
 
 266         FaultNotificationBuilder faultBuilder = new FaultNotificationBuilder();
 
 267         faultBuilder.setProblem("problem1");
 
 268         FaultNotification fault = faultBuilder.build();
 
 269         PushFaultNotificationInputBuilder statusInput = new PushFaultNotificationInputBuilder();
 
 270         statusInput.fieldsFrom(fault);
 
 271         ListenableFuture<RpcResult<PushFaultNotificationOutput>> rpcOutput =
 
 272                 api.pushFaultNotification(statusInput.build());
 
 273         RpcResult<PushFaultNotificationOutput> res = rpcOutput.get();
 
 274         PushFaultNotificationOutput output = res.getResult();
 
 276         //verify that it was called one time
 
 277         verify(vNL, times(1)).onNotification(fault);
 
 279         registration.close();
 
 280         System.out.println("Output " + output);
 
 284     public void test9ApiPushNotifiction() throws InterruptedException, ExecutionException {
 
 286         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
 
 288         VesNotificationListener vNL = mock(VesNotificationListener.class);
 
 289         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
 
 291         AttributeChangeNotificationBuilder changeBuilder = new AttributeChangeNotificationBuilder();
 
 292         changeBuilder.setAttributeName("attribute1");
 
 293         AttributeChangeNotification change = changeBuilder.build();
 
 294         PushAttributeChangeNotificationInputBuilder statusInput = new PushAttributeChangeNotificationInputBuilder();
 
 295         statusInput.fieldsFrom(change);
 
 296         ListenableFuture<RpcResult<PushAttributeChangeNotificationOutput>> rpcOutput =
 
 297                 api.pushAttributeChangeNotification(statusInput.build());
 
 298         RpcResult<PushAttributeChangeNotificationOutput> res = rpcOutput.get();
 
 299         PushAttributeChangeNotificationOutput output = res.getResult();
 
 301         //verify that it was called one time
 
 302         verify(vNL, times(1)).onNotification(change);
 
 304         registration.close();
 
 305         System.out.println("Output " + output);
 
 309     // ------- private section
 
 311     private static void delete(Path etc) throws IOException {
 
 312         if (Files.exists(etc)) {
 
 313             System.out.println("Found and remove:" + etc.toString());
 
 314             delete(etc.toFile());
 
 318     private static void delete(File f) throws IOException {
 
 319         if (f.isDirectory()) {
 
 320             for (File c : f.listFiles()) {
 
 325             throw new FileNotFoundException("Failed to delete file: " + f);