cc49ea9bfbb6bea1c2c2cefd10651f91d15e7370
[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.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import com.google.common.io.Files;
28 import com.sun.net.httpserver.HttpExchange;
29 import com.sun.net.httpserver.HttpHandler;
30 import com.sun.net.httpserver.HttpServer;
31 import java.io.File;
32 import java.io.IOException;
33 import java.io.OutputStream;
34 import java.net.InetSocketAddress;
35 import java.nio.charset.StandardCharsets;
36 import java.util.ArrayList;
37 import java.util.List;
38 import java.util.concurrent.ExecutorService;
39 import java.util.concurrent.Executors;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
46 import org.onap.ccsdk.features.sdnr.wt.common.util.ResourceFileLoader;
47 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
48 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
49 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
50
51 public class TestAai {
52
53     private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
54     private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
55     private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
56     private static final int AAI_SERVER_PORT = 45454;
57     private static final String TESTCONFIG_CONTENT = "[dcae]\n" + "dcaeUserCredentials=admin:admin\n" + "dcaeUrl=off\n"
58             + "dcaeHeartbeatPeriodSeconds=120\n" + "dcaeTestCollector=no\n" + "\n" + "[aots]\n"
59             + "userPassword=passwd\n" + "soapurladd=off\n" + "soapaddtimeout=10\n" + "soapinqtimeout=20\n"
60             + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n" + "assignedto=userid\n"
61             + "addtemplate=addreq.tmpl.xml\n" + "severitypassthrough=critical,major,minor,warning\n"
62             + "systemuser=user\n" + "prt-offset=1200\n" + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n"
63             + "#smtpUsername=\n" + "#smtpPassword=\n" + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n"
64             + "esCluster=sendateodl5\n" + "\n" + "[aai]\n" + "#keep comment\n"
65             + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=http://localhost:" + AAI_SERVER_PORT + "\n"
66             + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
67             + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" + "aaiApplicationId=SDNR\n"
68             + "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n"
69             + "aaiPcks12ClientCertPassphrase=adminadmin\n" + "aaiClientConnectionTimeout=30000\n" + "\n" + "[pm]\n"
70             + "pmCluster=sendateodl5\n" + "pmEnabled=true\n" + "\n" + "";
71
72     private static final String EXT_TEST_URL = "https://testaai.onap.org:8443";
73     private static final String EXT_TEST_KEY = "test.key";
74     private static final String EXT_TEST_PASSWD = "test123";
75     private static final String EXT_TEST_APPLICATIONID = "SDNC";
76     private static final long EXT_TEST_CONN_TIMEOUT = 6000;
77
78
79     private static final CharSequence TESTCONFIG_CONTENT_EXT = "[aai]\n" + "#keep comment\n"
80             + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=http://localhost:" + AAI_SERVER_PORT + "\n"
81             + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
82             + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" +
83             //            "aaiApplicationId=SDNR\n" +
84             //            "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
85             //            "aaiPcks12ClientCertPassphrase=adminadmin\n" +
86             //            "aaiClientConnectionTimeout=30000\n" +
87             "\n";
88     private static final CharSequence TESTCONFIG_CONTENT_EXT2 = "org.onap.ccsdk.sli.adaptors.aai.ssl.key="
89             + EXT_TEST_KEY + "\n" + "org.onap.ccsdk.sli.adaptors.aai.ssl.key.psswd=" + EXT_TEST_PASSWD + "\n"
90             + "org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore=false\n"
91             + "org.onap.ccsdk.sli.adaptors.aai.application=" + EXT_TEST_APPLICATIONID + "\n"
92             + "org.onap.ccsdk.sli.adaptors.aai.uri=" + EXT_TEST_URL + "\n" + "connection.timeout="
93             + EXT_TEST_CONN_TIMEOUT + "\n";
94     private static HttpServer server;
95     private static ExecutorService httpThreadPool;
96     private static ConfigurationFileRepresentation globalCfg;
97
98     @Test
99     public void test() {
100
101         String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
102         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
103
104         AaiProviderClient provider = new AaiProviderClient(cfg, null);
105
106         String mountPointName = "testDevice 01";
107         String type = "Unit";
108         String model = "Horizon Compact+";
109         String vendor = "DragonWave-X";
110         String ipv4 = "127.0.0.1";
111         String ipv6 = "::1";
112         List<String> ifInfos = new ArrayList<>();
113         ifInfos.add("LP-MWPS-RADIO");
114         InventoryInformationDcae ii = new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
115         System.out.println("registering device");
116         provider.onDeviceRegistered(mountPointName, ii);
117         try {
118             Thread.sleep(5000);
119         } catch (InterruptedException e) {
120             e.printStackTrace();
121         }
122         System.out.println("unregistering device");
123         provider.onDeviceUnregistered(mountPointName);
124         System.out.println("finished");
125         try {
126             provider.close();
127         } catch (Exception e) {
128             e.printStackTrace();
129         }
130     }
131
132     @Test
133     public void test2() {
134         try {
135             Thread.sleep(3000);
136         } catch (InterruptedException e1) {
137             e1.printStackTrace();
138         }
139         AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
140
141         String mountPointName = "testDevice 01";
142         String type = "Unit";
143         String model = "Horizon Compact+";
144         String vendor = "DragonWave-X";
145         String ipv4 = "127.0.0.1";
146         String ipv6 = "::1";
147         List<String> ifInfos = new ArrayList<>();
148         ifInfos.add("LP-MWPS-RADIO");
149         InventoryInformationDcae ii = new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
150         System.out.println("registering device");
151         provider.onDeviceRegistered(mountPointName);
152         provider.onDeviceRegistered(mountPointName, ii);
153         try {
154             Thread.sleep(5000);
155         } catch (InterruptedException e) {
156             e.printStackTrace();
157         }
158         System.out.println("unregistering device");
159         provider.onDeviceUnregistered(mountPointName);
160         System.out.println("finished");
161         try {
162             provider.close();
163         } catch (Exception e) {
164             e.printStackTrace();
165         }
166     }
167
168     @Test
169     public void testExtendedProperties() {
170         File testConfigurationFileName = new File("abbsads.properties");
171         File extfile = new File("aaiclient.properties");
172         if (testConfigurationFileName.exists()) {
173             testConfigurationFileName.delete();
174         }
175         if (extfile.exists()) {
176             extfile.delete();
177         }
178         try {
179             Files.asCharSink(testConfigurationFileName, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT);
180         } catch (IOException e) {
181             e.printStackTrace();
182             fail("problem writing a test config file: " + e.getMessage());
183         }
184         try {
185             Files.asCharSink(extfile, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT2);
186         } catch (IOException e) {
187             e.printStackTrace();
188             fail("problem writing a second test config file: " + e.getMessage());
189         }
190
191         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
192         AaiConfig config = new AaiConfig(cfg);
193         System.out.println(config.toString());
194
195         assertTrue(config.getBaseUrl().startsWith(EXT_TEST_URL));
196         assertEquals(EXT_TEST_KEY, config.getPcks12CertificateFilename());
197         assertEquals(EXT_TEST_PASSWD, config.getPcks12CertificatePassphrase());
198         assertEquals(EXT_TEST_APPLICATIONID, config.getHeaders().get("X-FromAppId"));
199         assertEquals(EXT_TEST_CONN_TIMEOUT, config.getConnectionTimeout());
200
201         if (testConfigurationFileName.exists()) {
202             testConfigurationFileName.delete();
203         }
204         if (extfile.exists()) {
205             extfile.delete();
206         }
207
208     }
209
210     @BeforeClass
211     public static void initAaiTestWebserver() throws IOException {
212         try {
213             Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
214         } catch (IOException e1) {
215             fail(e1.getMessage());
216         }
217         //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
218         globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
219         server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
220         httpThreadPool = Executors.newFixedThreadPool(5);
221         server.setExecutor(httpThreadPool);
222         AaiConfig config = new AaiConfig(globalCfg);
223         server.createContext(config.getBaseUri(), new MyHandler());
224         //server.createContext("/", new MyRootHandler());
225         server.setExecutor(null); // creates a default executor
226         server.start();
227         System.out.println("http server started");
228     }
229
230     @AfterClass
231     public static void stopTestWebserver() {
232         if (server != null) {
233             server.stop(0);
234             httpThreadPool.shutdownNow();
235             System.out.println("http server stopped");
236         }
237         if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
238             ENABLEDAAI_TESTCONFIG_FILE.delete();
239         }
240
241     }
242
243     static class MyHandler implements HttpHandler {
244         @Override
245         public void handle(HttpExchange t) throws IOException {
246             String method = t.getRequestMethod();
247             System.out.println("req method: " + method);
248             OutputStream os = null;
249             try {
250                 String res = "";
251                 if (method.equals("GET")) {
252                     t.sendResponseHeaders(404, res.length());
253                     os = t.getResponseBody();
254                     os.write(res.getBytes());
255                 } else if (method.equals("DELETE")) {
256                     t.sendResponseHeaders(200, res.length());
257                     os = t.getResponseBody();
258                     os.write(res.getBytes());
259                 } else if (method.equals("PUT")) {
260                     t.sendResponseHeaders(200, res.length());
261                     os = t.getResponseBody();
262                     os.write(res.getBytes());
263                 } else {
264                     t.sendResponseHeaders(404, 0);
265                 }
266                 System.out.println("req handled successful");
267
268             } catch (Exception e) {
269                 System.out.println(e.getMessage());
270             } finally {
271                 if (os != null) {
272                     os.close();
273                 }
274             }
275         }
276     }
277 }