358a17c397c2a7bf6469a90941e5d3909f748bb1
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  ******************************************************************************/
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.IOException;
28 import java.nio.file.Files;
29 import java.nio.file.Path;
30 import java.nio.file.Paths;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerService.Action;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.DataBrokerNetconfMock;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointMock;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointServiceMock;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.NotificationPublishServiceMock;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.RpcProviderRegistryMock;
41 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
42 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
43 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
44 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47
48 public class Test1dm {
49
50     private static int DATABASETIMEOUTSECONDS = 30;
51
52     private static Path KARAF_ETC = Paths.get("etc");
53     private static DeviceManagerImpl deviceManager;
54     private static MountPointMock  mountPoint;
55
56
57     @BeforeClass
58     public static void before() throws InterruptedException, IOException {
59
60         // Call System property to get the classpath value
61         Path etc = KARAF_ETC;
62         delete(etc);
63         System.out.println("Create empty:"+etc.toString());
64         Files.createDirectories(etc);
65
66         //Create mocks
67         DataBroker dataBroker = new DataBrokerNetconfMock();
68         MountPointService mountPointService = new MountPointServiceMock(mountPoint = new MountPointMock());
69         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
70         RpcProviderRegistry rpcProviderRegistry = new RpcProviderRegistryMock();
71
72         //start using blueprint interface
73         deviceManager = new DeviceManagerImpl();
74
75         deviceManager.setDataBroker(dataBroker);
76         deviceManager.setMountPointService(mountPointService);
77         deviceManager.setNotificationPublishService(notificationPublishService);
78         deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
79
80         try {
81             deviceManager.init();
82         } catch (Exception e) {
83             e.printStackTrace();
84         }
85         System.out.println("Initialization status: "+deviceManager.isDevicemanagerInitializationOk());
86         assertTrue("Devicemanager not initialized", deviceManager.isDevicemanagerInitializationOk());
87         System.out.println("Initialization done");
88     }
89
90     @AfterClass
91     public static void after() throws InterruptedException, IOException {
92
93         System.out.println("Start shutdown");
94         //close using blueprint interface
95         try {
96             deviceManager.close();
97         } catch (Exception e) {
98             System.out.println(e);
99         }
100         delete(KARAF_ETC);
101
102     }
103
104     @Test
105     public void test1() throws InterruptedException  {
106
107         System.out.println("Test1: Wait for database");
108         int timeout = DATABASETIMEOUTSECONDS;
109         while ( !deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) {
110             System.out.println("Test1: "+timeout);
111             Thread.sleep(1000); //On second
112         }
113         System.out.println("Test1: database initialized");
114     }
115
116     @Test
117     public void test2() {
118         System.out.println("Test2: slave mountpoint");
119
120         mountPoint.setDatabrokerAbsent(true);
121         NodeId nodeId = new NodeId("mountpointTest1");
122         NetconfNodeBuilder nNodeBuilder = new NetconfNodeBuilder();
123
124         System.out.println("Call devicemanager");
125         try {
126             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNodeBuilder.build());
127         } catch (Exception e) {
128             e.printStackTrace();
129             fail("Exception received.");
130         }
131         System.out.println("Test2: Done");
132
133     }
134
135     @Test
136     public void test3() {
137         System.out.println("Test3: master mountpoint");
138
139         mountPoint.setDatabrokerAbsent(false);
140         NodeId nodeId = new NodeId("mountpointTest2");
141         NetconfNodeBuilder nNodeBuilder = new NetconfNodeBuilder();
142
143         System.out.println("Call devicemanager");
144
145         try {
146             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNodeBuilder.build());
147         } catch (Exception e) {
148             e.printStackTrace();
149             fail("Exception received.");
150         }
151         System.out.println("Test3: Done");
152
153     }
154
155     //********************* Private
156
157     private static void delete(Path etc) throws IOException {
158         if (Files.exists(etc)) {
159             System.out.println("Found and remove:"+etc.toString());
160             delete(etc.toFile());
161         }
162     }
163
164     private static void delete(File f) throws IOException {
165         if (f.isDirectory()) {
166             for (File c : f.listFiles()) {
167                 delete(c);
168             }
169         }
170         if (!f.delete()) {
171             throw new FileNotFoundException("Failed to delete file: " + f);
172         }
173     }
174
175 }