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.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;
59 @SuppressWarnings("deprecation")
60 public class TestDeviceManagerWithDatabase {
62 private static int DATABASETIMEOUTSECONDS = 30;
64 private static Path KARAF_ETC = Paths.get("etc");
65 private static DeviceManagerImpl deviceManager;
66 private static MountPointMock mountPoint;
67 private static DataBrokerNetconfMock dataBrokerNetconf;
69 private static final Logger LOG = LoggerFactory.getLogger(TestDeviceManagerWithDatabase.class);
74 public static void before() throws InterruptedException, IOException {
76 System.out.println("Logger: "+LOG.getClass().getName() + " " + LOG.getName());
77 // Call System property to get the classpath value
81 System.out.println("Create empty:"+etc.toString());
82 Files.createDirectories(etc);
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();
94 //start using blueprint interface
97 deviceManager = new DeviceManagerImpl();
99 deviceManager.setDataBroker(dataBrokerNetconf);
100 deviceManager.setMountPointService(mountPointService);
101 deviceManager.setNotificationPublishService(notificationPublishService);
102 deviceManager.setRpcProviderRegistry(rpcProviderRegistry);
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
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");
120 public static void after() throws InterruptedException, IOException {
122 System.out.println("Start shutdown");
123 //close using blueprint interface
125 deviceManager.close();
126 } catch (Exception e) {
127 System.out.println(e);
134 public void test0() throws InterruptedException {
135 HtDatabaseWebAPIClient client = new HtDatabaseWebAPIClient();
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) {
146 public void test1() throws InterruptedException {
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
154 System.out.println("Test1: database initialized");
158 public void test2() {
159 System.out.println("Test2: slave mountpoint");
161 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
162 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
163 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
164 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
166 mountPoint.setDatabrokerAbsent(true);
167 NodeId nodeId = new NodeId("mountpointTest2");
169 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
170 } catch (Exception e) {
172 fail("Exception received.");
175 readOnlyTransaction.close();
176 System.out.println("Test2: Done");
181 public void test3() {
182 System.out.println("Test3: master mountpoint ONF Model 12");
184 ReadOnlyTransactionMountpoint12Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint12Mock();
185 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
186 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
187 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
189 mountPoint.setDatabrokerAbsent(false);
190 NodeId nodeId = new NodeId("mountpointTest3");
192 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
193 System.out.println("Node capabilites: "+capabilities);
196 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
197 } catch (Exception e) {
199 fail("Exception received.");
202 readOnlyTransaction.sendProblemNotification();
205 } catch (InterruptedException e) {
208 readOnlyTransaction.close();
209 System.out.println("Test3: Done");
214 public void test4() {
215 System.out.println("Test4: master mountpoint ONF Model 1211");
217 ReadOnlyTransactionMountpoint1211Mock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211Mock();
218 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
219 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
221 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
222 mountPoint.setDatabrokerAbsent(false);
223 NodeId nodeId = new NodeId("mountpointTest4");
225 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
226 System.out.println("Node capabilites: "+capabilities);
229 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
230 } catch (Exception e) {
232 fail("Exception received.");
235 readOnlyTransaction.sendProblemNotification();
238 } catch (InterruptedException e) {
239 Thread.interrupted();
242 readOnlyTransaction.close();
243 System.out.println("Test4: Done");
248 public void test5() {
249 System.out.println("Test5: master mountpoint ONF Model 1211p");
251 ReadOnlyTransactionMountpoint1211pMock readOnlyTransaction = new ReadOnlyTransactionMountpoint1211pMock();
252 dataBrokerNetconf.setReadOnlyTransaction(readOnlyTransaction);
253 mountPoint.setReadOnlyTransaction(readOnlyTransaction);
255 NetconfNode nNode = readOnlyTransaction.getMock().getNetconfNode();
256 mountPoint.setDatabrokerAbsent(false);
257 NodeId nodeId = new NodeId("mountpointTest5");
259 Capabilities capabilities = Capabilities.getAvailableCapabilities(nNode);
260 System.out.println("Node capabilites: "+capabilities);
263 deviceManager.startListenerOnNodeForConnectedState(Action.CREATE, nodeId, nNode);
264 } catch (Exception e) {
266 fail("Exception received.");
268 readOnlyTransaction.sendProblemNotification();
271 } catch (InterruptedException e) {
272 Thread.interrupted();
275 readOnlyTransaction.close();
276 System.out.println("Test5: Done");
281 public void test6() {
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);
287 while ( testFile.exists() && wait-- > 0) {
288 System.out.println("Waiting "+wait);
291 } catch (InterruptedException e) {
292 Thread.interrupted();
297 System.out.println("Test6: Done");
301 //********************* Private
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());
310 private static void delete(File f) throws IOException {
311 if (f.isDirectory()) {
312 for (File c : f.listFiles()) {
317 throw new FileNotFoundException("Failed to delete file: " + f);