eb07a48f1e33baf94f7b6ea65490ae581a5f1408
[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 = netconfStateService.registerNetconfNodeStateListener(nSL);
153         assertNotNull("Result should be null", res);
154         res.getInstance();
155         res.close();
156     }
157
158     @Test
159     public void test3() {
160
161         System.out.println("Test3: Register connect listener");
162
163         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
164         ListenerRegistration<NetconfNodeConnectListener> res =
165                 netconfStateService.registerNetconfNodeConnectListener(nCL);
166         assertNotNull("Result should be null", res);
167         res.getInstance();
168         res.close();
169     }
170
171     @Test
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);
177     }
178
179     @SuppressWarnings("unchecked")
180     @Test
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();
186
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());
195
196         DataObjectModification<Node> dom = mock(DataObjectModification.class);
197         when(dom.getDataAfter()).thenReturn(rootNode);
198         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
199
200         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
201         when(ntn.getRootNode()).thenReturn(dom);
202
203         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
204         netconfStateService.registerNetconfNodeConnectListener(nCL);
205         mountPoint.setDatabrokerAbsent(false);
206
207         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
208         dataBrokerNetconf.sendClusteredChanges(changes);
209         dataBrokerNetconf.sendChanges(changes);
210         Thread.sleep(300);
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());
216
217     }
218
219     @SuppressWarnings("unchecked")
220     @Test
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();
226
227         NodeBuilder nodeBuilder = new NodeBuilder();
228         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
229         nodeBuilder.setNodeId(new NodeId("Test"));
230         Node rootNodeAfter = nodeBuilder.build();
231
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);
236
237         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
238         when(ntn.getRootNode()).thenReturn(dom);
239
240         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
241         dataBrokerNetconf.sendClusteredChanges(changes);
242         dataBrokerNetconf.sendChanges(changes);
243     }
244
245     @Test
246     public void test7ApiStatus() throws InterruptedException, ExecutionException {
247
248         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
249
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);
255     }
256
257
258     @Test
259     public void test8ApiPushFault() throws InterruptedException, ExecutionException {
260
261         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
262
263         VesNotificationListener vNL = mock(VesNotificationListener.class);
264         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
265
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();
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 =
297                 api.pushAttributeChangeNotification(statusInput.build());
298         RpcResult<PushAttributeChangeNotificationOutput> res = rpcOutput.get();
299         PushAttributeChangeNotificationOutput output = res.getResult();
300
301         //verify that it was called one time
302         verify(vNL, times(1)).onNotification(change);
303
304         registration.close();
305         System.out.println("Output " + output);
306     }
307
308
309     // ------- private section
310
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());
315         }
316     }
317
318     private static void delete(File f) throws IOException {
319         if (f.isDirectory()) {
320             for (File c : f.listFiles()) {
321                 delete(c);
322             }
323         }
324         if (!f.delete()) {
325             throw new FileNotFoundException("Failed to delete file: " + f);
326         }
327     }
328
329
330
331 }