ff659ca3dde6362e7b1854f0350492eede2d04fc
[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.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import java.io.File;
28 import java.io.FileNotFoundException;
29 import java.io.IOException;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.concurrent.ExecutionException;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeConnectListener;
40 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNodeStateListener;
41 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.VesNotificationListener;
42 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfNodeStateServiceImpl;
43 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.rpc.NetconfnodeStateServiceRpcApiImpl;
44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.ClusterSingletonServiceProviderMock;
45 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.DataBrokerNetconfMock;
46 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointMock;
47 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.MountPointServiceMock;
48 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.NotificationPublishServiceMock;
49 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.mock.RpcProviderRegistryMock;
50 import org.opendaylight.mdsal.binding.api.DataObjectModification;
51 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
52 import org.opendaylight.mdsal.binding.api.DataTreeModification;
53 import org.opendaylight.mdsal.binding.api.MountPointService;
54 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
55 import org.opendaylight.mdsal.binding.api.RpcProviderService;
56 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeConnectionStatus.ConnectionStatus;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.AttributeChangeNotification;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.AttributeChangeNotificationBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotification;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.FaultNotificationBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusInputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.GetStatusOutputBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationInputBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushAttributeChangeNotificationOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationInputBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.netconfnode.state.rev191011.PushFaultNotificationOutput;
71 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
72 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
73 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
74 import org.opendaylight.yangtools.concepts.ListenerRegistration;
75 import org.opendaylight.yangtools.yang.common.RpcResult;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78
79
80 public class TestNetconfNodeStateService {
81
82     private static Path KARAF_ETC = Paths.get("etc");
83     private static NetconfNodeStateServiceImpl netconfStateService;
84     private static MountPointMock mountPoint;
85     private static DataBrokerNetconfMock dataBrokerNetconf;
86
87     private static final Logger LOG = LoggerFactory.getLogger(TestNetconfNodeStateService.class);
88
89     @BeforeClass
90     public static void before() throws InterruptedException, IOException {
91
92         System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
93         // Call System property to get the classpath value
94         Path etc = KARAF_ETC;
95         delete(etc);
96
97         System.out.println("Create empty:" + etc.toString());
98         Files.createDirectories(etc);
99
100         // Create mocks
101         dataBrokerNetconf = new DataBrokerNetconfMock();
102         dataBrokerNetconf.newReadWriteTransaction();
103         mountPoint = new MountPointMock();
104         ClusterSingletonServiceProvider clusterSingletonService = new ClusterSingletonServiceProviderMock();
105         MountPointService mountPointService = new MountPointServiceMock(mountPoint);
106         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
107         RpcProviderService rpcProviderRegistry = new RpcProviderRegistryMock();
108
109         // start using blueprint interface
110         netconfStateService = new NetconfNodeStateServiceImpl();
111
112         netconfStateService.setDataBroker(dataBrokerNetconf);
113         netconfStateService.setMountPointService(mountPointService);
114         netconfStateService.setNotificationPublishService(notificationPublishService);
115         netconfStateService.setRpcProviderRegistry(rpcProviderRegistry);
116         netconfStateService.setClusterSingletonService(clusterSingletonService);
117         netconfStateService.init();
118         System.out.println("Initialization done");
119     }
120
121     @AfterClass
122     public static void after() throws InterruptedException, IOException {
123         System.out.println("Start shutdown");
124         // close using blueprint interface
125         netconfStateService.close();
126         delete(KARAF_ETC);
127
128     }
129
130     @Test
131     public void test1() {
132
133         System.out.println("Test1: Verify init state");
134         assertTrue("Devicemanager not initialized", netconfStateService.isInitializationSuccessful());
135     }
136
137
138     @Test
139     public void test2() {
140
141         System.out.println("Test2: Register state listener");
142
143         NetconfNodeStateListener nSL = mock(NetconfNodeStateListener.class);
144         ListenerRegistration<NetconfNodeStateListener> res =
145         netconfStateService.registerNetconfNodeStateListener(nSL);
146         assertNotNull("Result should be null", res);
147         res.getInstance();
148         res.close();
149     }
150
151     @Test
152     public void test3() {
153
154         System.out.println("Test3: Register connect listener");
155
156         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
157         ListenerRegistration<NetconfNodeConnectListener> res =
158         netconfStateService.registerNetconfNodeConnectListener(nCL);
159         assertNotNull("Result should be null", res);
160         res.getInstance();
161         res.close();
162     }
163
164     @Test
165     public void test4() {
166         System.out.println("Test4: Get status listener");
167         GetStatusInputBuilder inputBuilder = new GetStatusInputBuilder();
168         GetStatusOutputBuilder res = netconfStateService.getStatus(inputBuilder.build());
169         assertNotNull("Result should be null", res);
170     }
171
172     @SuppressWarnings("unchecked")
173     @Test
174     public void test5OnConnect() {
175         System.out.println("Test5: On Connect");
176         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
177         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
178         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
179
180         NodeId nodeId = new NodeId("Test");
181         NodeBuilder nodeBuilder = new NodeBuilder();
182         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
183         nodeBuilder.setNodeId(nodeId);
184         Node rootNode = nodeBuilder.build();
185
186         DataObjectModification<Node> dom = mock(DataObjectModification.class);
187         when(dom.getDataAfter()).thenReturn(rootNode);
188         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
189
190         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
191         when(ntn.getRootNode()).thenReturn(dom);
192
193         NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
194         netconfStateService.registerNetconfNodeConnectListener(nCL);
195         mountPoint.setDatabrokerAbsent(false);
196
197         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
198         dataBrokerNetconf.sendClusteredChanges(changes);
199         dataBrokerNetconf.sendChanges(changes);
200
201         //verify that it was called one time
202         verify(nCL,times(1)).onEnterConnected(nodeId, rootNodeNetconf, mountPoint.getDataBroker());
203
204     }
205
206     @SuppressWarnings("unchecked")
207     @Test
208     public void test6Update() {
209         System.out.println("Test6: OnChange");
210         NetconfNodeBuilder netconfNodeBuilder = new NetconfNodeBuilder();
211         netconfNodeBuilder.setConnectionStatus(ConnectionStatus.Connected);
212         NetconfNode rootNodeNetconf = netconfNodeBuilder.build();
213
214         NodeBuilder nodeBuilder = new NodeBuilder();
215         nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
216         nodeBuilder.setNodeId(new NodeId("Test"));
217         Node rootNodeAfter = nodeBuilder.build();
218
219         DataObjectModification<Node> dom = mock(DataObjectModification.class);
220         when(dom.getDataBefore()).thenReturn(rootNodeAfter);
221         when(dom.getDataAfter()).thenReturn(rootNodeAfter);
222         when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
223
224         DataTreeModification<Node> ntn = mock(DataTreeModification.class);
225         when(ntn.getRootNode()).thenReturn(dom);
226
227         Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
228         dataBrokerNetconf.sendClusteredChanges(changes);
229         dataBrokerNetconf.sendChanges(changes);
230     }
231
232     @Test
233     public void test7ApiStatus() throws InterruptedException, ExecutionException {
234
235         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
236
237         GetStatusInputBuilder statusInput = new GetStatusInputBuilder();
238         ListenableFuture<RpcResult<GetStatusOutput>> statusOutput = api.getStatus(statusInput.build());
239         RpcResult<GetStatusOutput> res = statusOutput.get();
240         GetStatusOutput output = res.getResult();
241         System.out.println("Output "+output);
242     }
243
244
245     @Test
246     public void test8ApiPushFault() throws InterruptedException, ExecutionException {
247
248         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
249
250         VesNotificationListener vNL = mock(VesNotificationListener.class);
251         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
252
253         FaultNotificationBuilder faultBuilder = new FaultNotificationBuilder();
254         faultBuilder.setProblem("problem1");
255         FaultNotification fault = faultBuilder.build();
256         PushFaultNotificationInputBuilder statusInput = new PushFaultNotificationInputBuilder();
257         statusInput.fieldsFrom(fault);
258         ListenableFuture<RpcResult<PushFaultNotificationOutput>> rpcOutput = api.pushFaultNotification(statusInput.build());
259         RpcResult<PushFaultNotificationOutput> res = rpcOutput.get();
260         PushFaultNotificationOutput output = res.getResult();
261
262         //verify that it was called one time
263         verify(vNL,times(1)).onNotification(fault);
264
265         registration.close();
266         System.out.println("Output "+output);
267     }
268
269     @Test
270     public void test9ApiPushNotifiction() throws InterruptedException, ExecutionException {
271
272         NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
273
274         VesNotificationListener vNL = mock(VesNotificationListener.class);
275         ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
276
277         AttributeChangeNotificationBuilder changeBuilder = new AttributeChangeNotificationBuilder();
278         changeBuilder.setAttributeName("attribute1");
279         AttributeChangeNotification change = changeBuilder.build();
280         PushAttributeChangeNotificationInputBuilder statusInput = new PushAttributeChangeNotificationInputBuilder();
281         statusInput.fieldsFrom(change);
282         ListenableFuture<RpcResult<PushAttributeChangeNotificationOutput>> rpcOutput = api.pushAttributeChangeNotification(statusInput.build());
283         RpcResult<PushAttributeChangeNotificationOutput> res = rpcOutput.get();
284         PushAttributeChangeNotificationOutput output = res.getResult();
285
286         //verify that it was called one time
287         verify(vNL,times(1)).onNotification(change);
288
289         registration.close();
290         System.out.println("Output "+output);
291     }
292
293
294     // ------- private section
295
296     private static void delete(Path etc) throws IOException {
297         if (Files.exists(etc)) {
298             System.out.println("Found and remove:" + etc.toString());
299             delete(etc.toFile());
300         }
301     }
302
303     private static void delete(File f) throws IOException {
304         if (f.isDirectory()) {
305             for (File c : f.listFiles()) {
306                 delete(c);
307             }
308         }
309         if (!f.delete()) {
310             throw new FileNotFoundException("Failed to delete file: " + f);
311         }
312     }
313
314
315
316 }