76684e50fbfc080dd98a4f4c0360b735594d5d62
[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.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.database.HtDatabaseWebAPIClient;
37 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.Resources;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.Capabilities;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerImpl;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.DeviceManagerService.Action;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.DataBrokerNetconfMock;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointMock;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.MountPointServiceMock;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.NotificationPublishServiceMock;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.mock.RpcProviderRegistryMock;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211Mock;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint1211pMock;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ReadOnlyTransactionMountpoint12Mock;
49 import org.opendaylight.controller.md.sal.binding.api.MountPointService;
50 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
51 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class TestDeviceManagerWithDatabase {
58
59     private static int DATABASETIMEOUTSECONDS = 30;
60
61     private static Path KARAF_ETC = Paths.get("etc");
62     private static DeviceManagerImpl deviceManager;
63     private static MountPointMock  mountPoint;
64     private static DataBrokerNetconfMock dataBrokerNetconf;
65
66     private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
67
68
69
70     @BeforeClass
71     public static void before() throws InterruptedException, IOException {
72
73          System.out.println("Logger: "+LOG.getClass().getName() + " " + LOG.getName());
74         // Call System property to get the classpath value
75         Path etc = KARAF_ETC;
76         delete(etc);
77
78         System.out.println("Create empty:"+etc.toString());
79         Files.createDirectories(etc);
80
81         //Create mocks
82         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
83         dataBrokerNetconf = new DataBrokerNetconfMock();
84         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
85         mountPoint = new MountPointMock();
86         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
87         MountPointService mountPointService = new MountPointServiceMock(mountPoint);
88         NotificationPublishService notificationPublishService = new NotificationPublishServiceMock();
89         RpcProviderRegistry rpcProviderRegistry = new RpcProviderRegistryMock();
90
91         //start using blueprint interface
92         deviceManager = new DeviceManagerImpl();
93
94         deviceManager.setDataBroker(dataBrokerNetconf);
95         deviceManager.setMountPointService(mountPointService);
96         deviceManager.setNotificationPublishService(notificationPublishService);
97         deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
98
99         try {
100             deviceManager.init();
101         } catch (Exception e) {
102             e.printStackTrace();
103         }
104
105         readOnlyTransaction.close();
106         System.out.println("Initialization status: "+deviceManager.isDevicemanagerInitializationOk());
107         assertTrue("Devicemanager not initialized", deviceManager.isDevicemanagerInitializationOk());
108         System.out.println("Initialization done");
109
110     }
111
112     @AfterClass
113     public static void after() throws InterruptedException, IOException {
114
115         System.out.println("Start shutdown");
116         //close using blueprint interface
117         try {
118             deviceManager.close();
119         } catch (Exception e) {
120             System.out.println(e);
121         }
122         delete(KARAF_ETC);
123
124     }
125
126     @Test
127     public void test0() throws InterruptedException {
128         HtDatabaseWebAPIClient client = new HtDatabaseWebAPIClient();
129         try {
130             String response = client.sendRequest("/mwtn/mediator-server/_search", "GET",
131                     new JSONObject("{\"match\":{\"id\":id}}"));
132             System.out.println(response);
133         } catch (JSONException | IOException e) {
134             e.printStackTrace();
135         }
136     }
137
138     @Test
139     public void test1() throws InterruptedException  {
140
141         System.out.println("Test1: Wait for database");
142         int timeout = DATABASETIMEOUTSECONDS;
143         while ( !deviceManager.isDatabaseInitializationFinished() && timeout-- > 0) {
144             System.out.println("Test1: "+timeout);
145             Thread.sleep(1000); //On second
146         }
147         System.out.println("Test1: database initialized");
148     }
149
150     @Test
151     public void test2() {
152         System.out.println("Test2: slave mountpoint");
153
154         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
155         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
156         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
157         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
158
159         mountPoint.setDatabrokerAbsent(true);
160         NodeId nodeId = new NodeId("mountpointTest2");
161         try {
162             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
163         } catch (Exception e) {
164             e.printStackTrace();
165             fail("Exception received.");
166         }
167
168         readOnlyTransaction.close();
169         System.out.println("Test2: Done");
170
171     }
172
173     @Test
174     public void test3() {
175         System.out.println("Test3: master mountpoint ONF Model 12");
176
177         ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
178         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
179         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
180         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
181
182         mountPoint.setDatabrokerAbsent(false);
183         NodeId nodeId = new NodeId("mountpointTest3");
184
185         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
186         System.out.println("Node capabilites: "+capabilities);
187
188         try {
189             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
190         } catch (Exception e) {
191             e.printStackTrace();
192             fail("Exception received.");
193         }
194
195         readOnlyTransaction.sendProblemNotification();
196         try {
197             Thread.sleep(500);
198         } catch (InterruptedException e) {
199         }
200
201         readOnlyTransaction.close();
202         System.out.println("Test3: Done");
203
204     }
205
206     @Test
207     public void test4() {
208         System.out.println("Test4: master mountpoint ONF Model 1211");
209
210         ReadOnlyTransactionMountpoint1211Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211Mock();
211         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
212         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
213
214         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
215         mountPoint.setDatabrokerAbsent(false);
216         NodeId nodeId = new NodeId("mountpointTest4");
217
218         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
219         System.out.println("Node capabilites: "+capabilities);
220
221         try {
222             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
223         } catch (Exception e) {
224             e.printStackTrace();
225             fail("Exception received.");
226         }
227
228         readOnlyTransaction.sendProblemNotification();
229         try {
230             Thread.sleep(500);
231         } catch (InterruptedException e) {
232             Thread.interrupted();
233         }
234
235         readOnlyTransaction.close();
236         System.out.println("Test4: Done");
237
238     }
239
240     @Test
241     public void test5() {
242         System.out.println("Test5: master mountpoint ONF Model 1211p");
243
244         ReadOnlyTransactionMountpoint1211pMock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211pMock();
245         dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
246         mountPoint.setReadOnlyTransaction(readOnlyTransaction);
247
248         NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
249         mountPoint.setDatabrokerAbsent(false);
250         NodeId nodeId = new NodeId("mountpointTest5");
251
252         Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
253         System.out.println("Node capabilites: "+capabilities);
254
255         try {
256             deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
257         } catch (Exception e) {
258             e.printStackTrace();
259             fail("Exception received.");
260         }
261         readOnlyTransaction.sendProblemNotification();
262         try {
263             Thread.sleep(500);
264         } catch (InterruptedException e) {
265             Thread.interrupted();
266         }
267
268         readOnlyTransaction.close();
269         System.out.println("Test5: Done");
270
271     }
272
273     @Test
274     public void test6() {
275
276         System.out.println("Test6: Write zip data file file");
277         File testFile = new File("etc/elasticsearch_update.zip");
278         Resources.extractFileTo("elasticsearch_update.zip", testFile);
279         int wait=130;
280         while ( testFile.exists() && wait-- > 0) {
281             System.out.println("Waiting "+wait);
282             try {
283                 Thread.sleep(1000);
284             } catch (InterruptedException e) {
285                 Thread.interrupted();
286             }
287         }
288
289
290         System.out.println("Test6: Done");
291
292     }
293
294     //********************* Private
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 }