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
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=========================================================
15 ******************************************************************************/
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.assertNotNull;
26 import static org.junit.Assert.assertTrue;
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;
80 public class TestNetconfNodeStateService {
82 private static Path KARAF_ETC = Paths.get("etc");
83 private static NetconfNodeStateServiceImpl netconfStateService;
84 private static MountPointMock mountPoint;
85 private static DataBrokerNetconfMock dataBrokerNetconf;
87 private static final Logger LOG = LoggerFactory.getLogger(TestNetconfNodeStateService.class);
90 public static void before() throws InterruptedException, IOException {
92 System.out.println("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
93 // Call System property to get the classpath value
97 System.out.println("Create empty:" + etc.toString());
98 Files.createDirectories(etc);
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();
109 // start using blueprint interface
110 netconfStateService = new NetconfNodeStateServiceImpl();
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");
122 public static void after() throws InterruptedException, IOException {
123 System.out.println("Start shutdown");
124 // close using blueprint interface
125 netconfStateService.close();
131 public void test1() {
133 System.out.println("Test1: Verify init state");
134 assertTrue("Devicemanager not initialized", netconfStateService.isInitializationSuccessful());
139 public void test2() {
141 System.out.println("Test2: Register state listener");
143 NetconfNodeStateListener nSL = mock(NetconfNodeStateListener.class);
144 ListenerRegistration<NetconfNodeStateListener> res =
145 netconfStateService.registerNetconfNodeStateListener(nSL);
146 assertNotNull("Result should be null", res);
152 public void test3() {
154 System.out.println("Test3: Register connect listener");
156 NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
157 ListenerRegistration<NetconfNodeConnectListener> res =
158 netconfStateService.registerNetconfNodeConnectListener(nCL);
159 assertNotNull("Result should be null", res);
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);
172 @SuppressWarnings("unchecked")
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();
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();
186 DataObjectModification<Node> dom = mock(DataObjectModification.class);
187 when(dom.getDataAfter()).thenReturn(rootNode);
188 when(dom.getModificationType()).thenReturn(ModificationType.WRITE);
190 DataTreeModification<Node> ntn = mock(DataTreeModification.class);
191 when(ntn.getRootNode()).thenReturn(dom);
193 NetconfNodeConnectListener nCL = mock(NetconfNodeConnectListener.class);
194 netconfStateService.registerNetconfNodeConnectListener(nCL);
195 mountPoint.setDatabrokerAbsent(false);
197 Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
198 dataBrokerNetconf.sendClusteredChanges(changes);
199 dataBrokerNetconf.sendChanges(changes);
201 //verify that it was called one time
202 verify(nCL,times(1)).onEnterConnected(nodeId, rootNodeNetconf, mountPoint.getDataBroker());
206 @SuppressWarnings("unchecked")
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();
214 NodeBuilder nodeBuilder = new NodeBuilder();
215 nodeBuilder.addAugmentation(NetconfNode.class, rootNodeNetconf);
216 nodeBuilder.setNodeId(new NodeId("Test"));
217 Node rootNodeAfter = nodeBuilder.build();
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);
224 DataTreeModification<Node> ntn = mock(DataTreeModification.class);
225 when(ntn.getRootNode()).thenReturn(dom);
227 Collection<DataTreeModification<Node>> changes = Arrays.asList(ntn);
228 dataBrokerNetconf.sendClusteredChanges(changes);
229 dataBrokerNetconf.sendChanges(changes);
233 public void test7ApiStatus() throws InterruptedException, ExecutionException {
235 NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
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);
246 public void test8ApiPushFault() throws InterruptedException, ExecutionException {
248 NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
250 VesNotificationListener vNL = mock(VesNotificationListener.class);
251 ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
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();
262 //verify that it was called one time
263 verify(vNL,times(1)).onNotification(fault);
265 registration.close();
266 System.out.println("Output "+output);
270 public void test9ApiPushNotifiction() throws InterruptedException, ExecutionException {
272 NetconfnodeStateServiceRpcApiImpl api = netconfStateService.getNetconfnodeStateServiceRpcApiImpl();
274 VesNotificationListener vNL = mock(VesNotificationListener.class);
275 ListenerRegistration<VesNotificationListener> registration = netconfStateService.registerVesNotifications(vNL);
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();
286 //verify that it was called one time
287 verify(vNL,times(1)).onNotification(change);
289 registration.close();
290 System.out.println("Output "+output);
294 // ------- private section
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());
303 private static void delete(File f) throws IOException {
304 if (f.isDirectory()) {
305 for (File c : f.listFiles()) {
310 throw new FileNotFoundException("Failed to delete file: " + f);