1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk feature sdnr wt
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
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;
57 public class TestDeviceManagerWithDatabase {
59 private static int DATABASETIMEOUTSECONDS = 30;
61 private static Path KARAF_ETC = Paths.get("etc");
62 private static DeviceManagerImpl deviceManager;
63 private static MountPointMock mountPoint;
64 private static DataBrokerNetconfMock dataBrokerNetconf;
66 private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
71 public static void before() throws InterruptedException, IOException {
73 System.out.println("Logger: "+LOG.getClass().getName() + " " + LOG.getName());
74 // Call System property to get the classpath value
78 System.out.println("Create empty:"+etc.toString());
79 Files.createDirectories(etc);
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();
91 //start using blueprint interface
92 deviceManager = new DeviceManagerImpl();
94 deviceManager.setDataBroker(dataBrokerNetconf);
95 deviceManager.setMountPointService(mountPointService);
96 deviceManager.setNotificationPublishService(notificationPublishService);
97 deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
100 deviceManager.init();
101 } catch (Exception e) {
105 readOnlyTransaction.close();
106 System.out.println("Initialization status: "+deviceManager.isDevicemanagerInitializationOk());
107 assertTrue("Devicemanager not initialized", deviceManager.isDevicemanagerInitializationOk());
108 System.out.println("Initialization done");
113 public static void after() throws InterruptedException, IOException {
115 System.out.println("Start shutdown");
116 //close using blueprint interface
118 deviceManager.close();
119 } catch (Exception e) {
120 System.out.println(e);
127 public void test0() throws InterruptedException {
128 HtDatabaseWebAPIClient client = new HtDatabaseWebAPIClient();
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) {
139 public void test1() throws InterruptedException {
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
147 System.out.println("Test1: database initialized");
151 public void test2() {
152 System.out.println("Test2: slave mountpoint");
154 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
155 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
156 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
157 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
159 mountPoint.setDatabrokerAbsent(true);
160 NodeId nodeId = new NodeId("mountpointTest2");
162 deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
163 } catch (Exception e) {
165 fail("Exception received.");
168 readOnlyTransaction.close();
169 System.out.println("Test2: Done");
174 public void test3() {
175 System.out.println("Test3: master mountpoint ONF Model 12");
177 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
178 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
179 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
180 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
182 mountPoint.setDatabrokerAbsent(false);
183 NodeId nodeId = new NodeId("mountpointTest3");
185 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
186 System.out.println("Node capabilites: "+capabilities);
189 deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
190 } catch (Exception e) {
192 fail("Exception received.");
195 readOnlyTransaction.sendProblemNotification();
198 } catch (InterruptedException e) {
201 readOnlyTransaction.close();
202 System.out.println("Test3: Done");
207 public void test4() {
208 System.out.println("Test4: master mountpoint ONF Model 1211");
210 ReadOnlyTransactionMountpoint1211Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211Mock();
211 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
212 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
214 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
215 mountPoint.setDatabrokerAbsent(false);
216 NodeId nodeId = new NodeId("mountpointTest4");
218 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
219 System.out.println("Node capabilites: "+capabilities);
222 deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
223 } catch (Exception e) {
225 fail("Exception received.");
228 readOnlyTransaction.sendProblemNotification();
231 } catch (InterruptedException e) {
232 Thread.interrupted();
235 readOnlyTransaction.close();
236 System.out.println("Test4: Done");
241 public void test5() {
242 System.out.println("Test5: master mountpoint ONF Model 1211p");
244 ReadOnlyTransactionMountpoint1211pMock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211pMock();
245 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
246 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
248 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
249 mountPoint.setDatabrokerAbsent(false);
250 NodeId nodeId = new NodeId("mountpointTest5");
252 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
253 System.out.println("Node capabilites: "+capabilities);
256 deviceManager.startListenerOnNodeForConnectedState(Action.ADD, nodeId, nNode);
257 } catch (Exception e) {
259 fail("Exception received.");
261 readOnlyTransaction.sendProblemNotification();
264 } catch (InterruptedException e) {
265 Thread.interrupted();
268 readOnlyTransaction.close();
269 System.out.println("Test5: Done");
274 public void test6() {
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);
280 while ( testFile.exists() && wait-- > 0) {
281 System.out.println("Waiting "+wait);
284 } catch (InterruptedException e) {
285 Thread.interrupted();
290 System.out.println("Test6: Done");
294 //********************* Private
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);