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=========================================================
21 package org.onap.ccsdk.features.sdnr.wt.devicemanager.test;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
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;
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;
51 public class TestAai {
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" + "";
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;
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" +
88 private static final CharSequence TESTCONFIG_CONTENT_EXT2 = "org.onap.ccsdk.sli.adaptors.aai.ssl.key="+EXT_TEST_KEY+"\n"
89 + "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"
93 + "connection.timeout="+EXT_TEST_CONN_TIMEOUT+"\n";
94 private static HttpServer server;
95 private static ExecutorService httpThreadPool;
96 private static ConfigurationFileRepresentation globalCfg;
101 String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
102 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
104 AaiProviderClient provider = new AaiProviderClient(cfg, null);
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";
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);
119 } catch (InterruptedException e) {
122 System.out.println("unregistering device");
123 provider.onDeviceUnregistered(mountPointName);
124 System.out.println("finished");
127 } catch (Exception e) {
133 public void test2() {
136 } catch (InterruptedException e1) {
137 e1.printStackTrace();
139 AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
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";
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);
155 } catch (InterruptedException e) {
158 System.out.println("unregistering device");
159 provider.onDeviceUnregistered(mountPointName);
160 System.out.println("finished");
163 } catch (Exception e) {
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();
175 if (extfile.exists()) {
179 Files.asCharSink(testConfigurationFileName, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT);
180 } catch (IOException e) {
182 fail("problem writing a test config file: " + e.getMessage());
185 Files.asCharSink(extfile, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT2);
186 } catch (IOException e) {
188 fail("problem writing a second test config file: " + e.getMessage());
191 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
192 AaiConfig config = new AaiConfig(cfg);
193 System.out.println(config.toString());
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());
201 if (testConfigurationFileName.exists()) {
202 testConfigurationFileName.delete();
204 if (extfile.exists()) {
211 public static void initAaiTestWebserver() throws IOException {
213 Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
214 } catch (IOException e1) {
215 fail(e1.getMessage());
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
227 System.out.println("http server started");
231 public static void stopTestWebserver() {
232 if (server != null) {
234 httpThreadPool.shutdownNow();
235 System.out.println("http server stopped");
237 if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
238 ENABLEDAAI_TESTCONFIG_FILE.delete();
243 static class MyHandler implements HttpHandler {
245 public void handle(HttpExchange t) throws IOException {
246 String method = t.getRequestMethod();
247 System.out.println("req method: " + method);
248 OutputStream os = null;
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());
264 t.sendResponseHeaders(404, 0);
266 System.out.println("req handled successful");
268 } catch (Exception e) {
269 System.out.println(e.getMessage());