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;
26 import com.google.common.io.Files;
27 import com.sun.net.httpserver.HttpExchange;
28 import com.sun.net.httpserver.HttpHandler;
29 import com.sun.net.httpserver.HttpServer;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.net.InetSocketAddress;
34 import java.nio.charset.StandardCharsets;
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.concurrent.ExecutorService;
38 import java.util.concurrent.Executors;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
43 import org.onap.ccsdk.features.sdnr.wt.common.util.ResourceFileLoader;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
47 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
49 public class TestAai {
51 private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
52 private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
53 private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
54 private static final int AAI_SERVER_PORT = 45454;
55 private static final String TESTCONFIG_CONTENT = "[dcae]\n" + "dcaeUserCredentials=admin:admin\n" + "dcaeUrl=off\n"
56 + "dcaeHeartbeatPeriodSeconds=120\n" + "dcaeTestCollector=no\n" + "\n" + "[aots]\n"
57 + "userPassword=passwd\n" + "soapurladd=off\n" + "soapaddtimeout=10\n" + "soapinqtimeout=20\n"
58 + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n" + "assignedto=userid\n"
59 + "addtemplate=addreq.tmpl.xml\n" + "severitypassthrough=critical,major,minor,warning\n"
60 + "systemuser=user\n" + "prt-offset=1200\n" + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n"
61 + "#smtpUsername=\n" + "#smtpPassword=\n" + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n"
62 + "esCluster=sendateodl5\n" + "\n" + "[aai]\n" + "#keep comment\n"
63 + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=http://localhost:" + AAI_SERVER_PORT + "\n"
64 + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
65 + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" + "aaiApplicationId=SDNR\n"
66 + "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n"
67 + "aaiPcks12ClientCertPassphrase=adminadmin\n" + "aaiClientConnectionTimeout=30000\n" + "\n" + "[pm]\n"
68 + "pmCluster=sendateodl5\n" + "pmEnabled=true\n" + "\n" + "";
70 private static final String EXT_TEST_URL = "https://testaai.onap.org:8443";
71 private static final String EXT_TEST_KEY = "test.key";
72 private static final String EXT_TEST_PASSWD = "test123";
73 private static final String EXT_TEST_APPLICATIONID = "SDNC";
74 private static final long EXT_TEST_CONN_TIMEOUT = 6000;
77 private static final CharSequence TESTCONFIG_CONTENT_EXT = "[aai]\n" + "#keep comment\n"
78 + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=http://localhost:" + AAI_SERVER_PORT + "\n"
79 + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
80 + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" +
81 // "aaiApplicationId=SDNR\n" +
82 // "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
83 // "aaiPcks12ClientCertPassphrase=adminadmin\n" +
84 // "aaiClientConnectionTimeout=30000\n" +
86 private static final CharSequence TESTCONFIG_CONTENT_EXT2 = "org.onap.ccsdk.sli.adaptors.aai.ssl.key="
87 + EXT_TEST_KEY + "\n" + "org.onap.ccsdk.sli.adaptors.aai.ssl.key.psswd=" + EXT_TEST_PASSWD + "\n"
88 + "org.onap.ccsdk.sli.adaptors.aai.host.certificate.ignore=false\n"
89 + "org.onap.ccsdk.sli.adaptors.aai.application=" + EXT_TEST_APPLICATIONID + "\n"
90 + "org.onap.ccsdk.sli.adaptors.aai.uri=" + EXT_TEST_URL + "\n" + "connection.timeout="
91 + EXT_TEST_CONN_TIMEOUT + "\n";
92 private static HttpServer server;
93 private static ExecutorService httpThreadPool;
94 private static ConfigurationFileRepresentation globalCfg;
99 String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
100 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
102 AaiProviderClient provider = new AaiProviderClient(cfg, null);
104 String mountPointName = "testDevice 01";
105 String type = "Unit";
106 String model = "Horizon Compact+";
107 String vendor = "DragonWave-X";
108 String ipv4 = "127.0.0.1";
110 List<String> ifInfos = new ArrayList<>();
111 ifInfos.add("LP-MWPS-RADIO");
112 InventoryInformationDcae ii = new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
113 System.out.println("registering device");
114 provider.onDeviceRegistered(new NodeId(mountPointName), ii);
117 } catch (InterruptedException e) {
118 System.out.println("Break sleep : " + e.getMessage());
119 Thread.currentThread().interrupt();
121 System.out.println("unregistering device");
122 provider.onDeviceUnregistered(new NodeId(mountPointName));
123 System.out.println("finished");
126 } catch (Exception e) {
132 public void test2() {
135 } catch (InterruptedException e1) {
136 System.out.println("Break sleep : " + e1.getMessage());
137 Thread.currentThread().interrupt();
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(new NodeId(mountPointName));
152 provider.onDeviceRegistered(new NodeId(mountPointName), ii);
155 } catch (InterruptedException e) {
156 System.out.println("Break sleep : " + e.getMessage());
157 Thread.currentThread().interrupt();
159 System.out.println("unregistering device");
160 provider.onDeviceUnregistered(new NodeId(mountPointName));
161 System.out.println("finished");
164 } catch (Exception e) {
170 public void testExtendedProperties() {
171 File testConfigurationFileName = new File("abbsads.properties");
172 File extfile = new File("aaiclient.properties");
173 if (testConfigurationFileName.exists()) {
174 testConfigurationFileName.delete();
176 if (extfile.exists()) {
180 Files.asCharSink(testConfigurationFileName, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT);
181 } catch (IOException e) {
183 fail("problem writing a test config file: " + e.getMessage());
186 Files.asCharSink(extfile, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT2);
187 } catch (IOException e) {
189 fail("problem writing a second test config file: " + e.getMessage());
192 ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
193 AaiConfig config = new AaiConfig(cfg);
194 System.out.println(config.toString());
196 assertTrue(config.getBaseUrl().startsWith(EXT_TEST_URL));
197 assertEquals(EXT_TEST_KEY, config.getPcks12CertificateFilename());
198 assertEquals(EXT_TEST_PASSWD, config.getPcks12CertificatePassphrase());
199 assertEquals(EXT_TEST_APPLICATIONID, config.getHeaders().get("X-FromAppId"));
200 assertEquals(EXT_TEST_CONN_TIMEOUT, config.getConnectionTimeout());
202 if (testConfigurationFileName.exists()) {
203 testConfigurationFileName.delete();
205 if (extfile.exists()) {
212 public static void initAaiTestWebserver() throws IOException {
214 Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
215 } catch (IOException e1) {
216 fail(e1.getMessage());
218 //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
219 globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
220 server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
221 httpThreadPool = Executors.newFixedThreadPool(5);
222 server.setExecutor(httpThreadPool);
223 AaiConfig config = new AaiConfig(globalCfg);
224 server.createContext(config.getBaseUri(), new MyHandler());
225 //server.createContext("/", new MyRootHandler());
226 server.setExecutor(null); // creates a default executor
228 System.out.println("http server started");
232 public static void stopTestWebserver() {
233 if (server != null) {
235 httpThreadPool.shutdownNow();
236 System.out.println("http server stopped");
238 if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
239 ENABLEDAAI_TESTCONFIG_FILE.delete();
244 static class MyHandler implements HttpHandler {
246 public void handle(HttpExchange t) throws IOException {
247 String method = t.getRequestMethod();
248 System.out.println("req method: " + method);
249 OutputStream os = null;
252 if (method.equals("GET")) {
253 t.sendResponseHeaders(404, res.length());
254 os = t.getResponseBody();
255 os.write(res.getBytes());
256 } else if (method.equals("DELETE")) {
257 t.sendResponseHeaders(200, res.length());
258 os = t.getResponseBody();
259 os.write(res.getBytes());
260 } else if (method.equals("PUT")) {
261 t.sendResponseHeaders(200, res.length());
262 os = t.getResponseBody();
263 os.write(res.getBytes());
265 t.sendResponseHeaders(404, 0);
267 System.out.println("req handled successful");
269 } catch (Exception e) {
270 System.out.println(e.getMessage());