5427c03399a3ef15b42d7925074b78d84763c186
[ccsdk/features.git] /
1 /*******************************************************************************
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
15  ******************************************************************************/
16 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test;
17
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.when;
20
21 import com.google.common.util.concurrent.ListenableFuture;
22
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;
28 import java.io.File;
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;
84
85
86 public class TestNetconfNodeStateService {
87
88     private static Path KARAF_ETC = Paths.get("etc");
89     private static NetconfNodeStateServiceImpl netconfStateService;
90     private static MountPointMock mountPoint;
91     private static DataBrokerNetconfMock dataBrokerNetconf;
92
93     private static final Logger LOG = LoggerFactory.getLogger(TestNetconfNodeStateService.class);
94
95     @BeforeClass
96     public static void before() throws InterruptedException, IOException {
97
98         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
99         // Call System property to get the classpath value
100         Path etc = KARAF_ETC;
101         delete(etc);
102
103         System.out.println("Create empty:" + etc.toString());
104         Files.createDirectories(etc);
105
106         // Create mocks
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);
115
116         // start using blueprint interface
117         netconfStateService = new NetconfNodeStateServiceImpl();
118
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");
127     }
128
129     @AfterClass
130     public static void after() throws InterruptedException, IOException {
131         System.out.println("Start shutdown");
132         // close using blueprint interface
133         netconfStateService.close();
134         delete(KARAF_ETC);
135
136     }
137
138     @Test
139     public void test1() {
140
141         System.out.println("Test1: Verify init state");
142         assertTrue("Devicemanager not initialized", netconfStateService.isInitializationSuccessful());
143     }
144
145
146     @Test
147     public void test2() {
148
149         System.out.println("Test2: Register state listener");
150
151         NetconfNodeStateListener nSL = mock(NetconfNodeStateListener.class);
152         ListenerRegistration<NetconfNodeStateListener> res =
153         netconfStateService.registerNetconfNodeStateListener(nSL);
154         assertNotNull("Result should be null", res);
155         res.getInstance();
156         res.close();
157     }
158
159     @Test
160     public void test3() {
161
162         System.out.println("Test3: Register connect listener");
163
164         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
165         ListenerRegistration<NetconfNodeConnectListener> res =
166         netconfStateService.registerNetconfNodeConnectListener(nCL);
167         assertNotNull("Result should be null", res);
168         res.getInstance();
169         res.close();
170     }
171
172     @Test
173     public void test4() {
174         System.out.println("Test4: Get status listener");
175         GetStatusInputBuilder inputBuilder = new GetStatusInputBuilder();
176         GetStatusOutputBuilder res = netconfStateService.getStatus(inputBuilder.build());
177         assertNotNull("Result should be null", res);
178     }
179
180     @SuppressWarnings("unchecked")
181     @Test
182     public void test5OnConnect() throws InterruptedException {
183         System.out.println("Test5: On Connect");
184         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
185         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
186         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
187
188         String nodeIdString = "Test";
189         NodeId nodeId = new NodeId(nodeIdString);
190         NodeBuilder nodeBuilder = new NodeBuilder();
191         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
192         nodeBuilder.setNodeId(nodeId);
193         Node rootNode = nodeBuilder.build();
194         NetconfAccessor acessor = new NetconfAccessorImpl(nodeId, rootNodeNetconf, mountPoint.getDataBroker(),
195                 mountPoint, new GenericTransactionUtils());
196
197         DataObjectModification<Node> dom = mock(DataObjectModification.class);
198         when(dom.getDataAfter()).thenReturn(rootNode);
199         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
200
201         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
202         when(ntn.getRootNode()).thenReturn(dom);
203
204         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
205         netconfStateService.registerNetconfNodeConnectListener(nCL);
206         mountPoint.setDatabrokerAbsent(false);
207
208         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
209         dataBrokerNetconf.sendClusteredChanges(changes);
210         dataBrokerNetconf.sendChanges(changes);
211         Thread.sleep(300);
212         //verify that it was called one time and nodeId is the expected
213         ArgumentCaptor<NetconfAccessor> varArgs = ArgumentCaptor.forClass(NetconfAccessor.class);
214         verify(nCL).onEnterConnected(varArgs.capture());
215         System.out.println("Accessor "+varArgs.getValue().getNodeId());
216         assertEquals(nodeIdString, varArgs.getValue().getNodeId().getValue());
217
218     }
219
220     @SuppressWarnings("unchecked")
221     @Test
222     public void test6Update() {
223         System.out.println("Test6: OnChange");
224         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
225         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
226         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
227
228         NodeBuilder nodeBuilder = new NodeBuilder();
229         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
230         nodeBuilder.setNodeId(new NodeId("Test"));
231         Node rootNodeAfter = nodeBuilder.build();
232
233         DataObjectModification<Node> dom = mock(DataObjectModification.class);
234         when(dom.getDataBefore()).thenReturn(rootNodeAfter);
235         when(dom.getDataAfter()).thenReturn(rootNodeAfter);
236         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
237
238         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
239         when(ntn.getRootNode()).thenReturn(dom);
240
241         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
242         dataBrokerNetconf.sendClusteredChanges(changes);
243         dataBrokerNetconf.sendChanges(changes);
244     }
245
246     @Test
247     public void test7ApiStatus() throws InterruptedException, ExecutionException {
248
249         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
250
251         GetStatusInputBuilder statusInput = new GetStatusInputBuilder();
252         ListenableFuture<RpcResult<GetStatusOutput>> statusOutput = api.getStatus(statusInput.build());
253         RpcResult<GetStatusOutput> res = statusOutput.get();
254         GetStatusOutput output = res.getResult();
255         System.out.println("Output "+output);
256     }
257
258
259     @Test
260     public void test8ApiPushFault() throws InterruptedException, ExecutionException {
261
262         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
263
264         VesNotificationListener vNL = mock(VesNotificationListener.class);
265         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
266
267         FaultNotificationBuilder faultBuilder = new FaultNotificationBuilder();
268         faultBuilder.setProblem("problem1");
269         FaultNotification fault = faultBuilder.build();
270         PushFaultNotificationInputBuilder statusInput = new PushFaultNotificationInputBuilder();
271         statusInput.fieldsFrom(fault);
272         ListenableFuture<RpcResult<PushFaultNotificationOutput>> rpcOutput = api.pushFaultNotification(statusInput.build());
273         RpcResult<PushFaultNotificationOutput> res = rpcOutput.get();
274         PushFaultNotificationOutput output = res.getResult();
275
276         //verify that it was called one time
277         verify(vNL,times(1)).onNotification(fault);
278
279         registration.close();
280         System.out.println("Output "+output);
281     }
282
283     @Test
284     public void test9ApiPushNotifiction() throws InterruptedException, ExecutionException {
285
286         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
287
288         VesNotificationListener vNL = mock(VesNotificationListener.class);
289         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
290
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 = api.pushAttributeChangeNotification(statusInput.build());
297         RpcResult<PushAttributeChangeNotificationOutput> res = rpcOutput.get();
298         PushAttributeChangeNotificationOutput output = res.getResult();
299
300         //verify that it was called one time
301         verify(vNL,times(1)).onNotification(change);
302
303         registration.close();
304         System.out.println("Output "+output);
305     }
306
307
308     // ------- private section
309
310     private static void delete(Path etc) throws IOException {
311         if (Files.exists(etc)) {
312             System.out.println("Found and remove:" + etc.toString());
313             delete(etc.toFile());
314         }
315     }
316
317     private static void delete(File f) throws IOException {
318         if (f.isDirectory()) {
319             for (File c : f.listFiles()) {
320                 delete(c);
321             }
322         }
323         if (!f.delete()) {
324             throw new FileNotFoundException("Failed to delete file: " + f);
325         }
326     }
327
328
329
330 }