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