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.fail;
25 import com.google.common.io.Files;
26 import com.sun.net.httpserver.HttpExchange;
27 import com.sun.net.httpserver.HttpHandler;
28 import com.sun.net.httpserver.HttpServer;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.net.InetSocketAddress;
33 import java.nio.charset.StandardCharsets;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
42 import org.onap.ccsdk.features.sdnr.wt.common.util.ResourceFileLoader;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
47 public class TestAai {
49 private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
50 private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
51 private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
52 private static final int AAI_SERVER_PORT=45454;
53 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
54 "dcaeUserCredentials=admin:admin\n" +
56 "dcaeHeartbeatPeriodSeconds=120\n" +
57 "dcaeTestCollector=no\n" +
60 "userPassword=passwd\n" +
62 "soapaddtimeout=10\n" +
63 "soapinqtimeout=20\n" +
65 "inqtemplate=inqreq.tmpl.xml\n" +
66 "assignedto=userid\n" +
67 "addtemplate=addreq.tmpl.xml\n" +
68 "severitypassthrough=critical,major,minor,warning\n" +
80 "esCluster=sendateodl5\n" +
84 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
85 "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
86 "aaiUserCredentials=AAI:AAI\n" +
87 "aaiDeleteOnMountpointRemove=true\n" +
88 "aaiTrustAllCerts=false\n" +
89 "aaiApiVersion=aai/v13\n" +
90 "aaiPropertiesFile=aaiclient.properties\n" +
91 "aaiApplicationId=SDNR\n" +
92 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
93 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
94 "aaiClientConnectionTimeout=30000\n" +
97 "pmCluster=sendateodl5\n" +
101 private HttpServer server;
102 private ExecutorService httpThreadPool;
103 private ConfigurationFileRepresentation globalCfg;
108 String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
109 ConfigurationFileRepresentation cfg=new ConfigurationFileRepresentation(testConfigurationFileName);
111 AaiProviderClient provider = new AaiProviderClient(cfg, null);
113 String mountPointName = "testDevice 01";
115 String model="Horizon Compact+";
116 String vendor="DragonWave-X";
117 String ipv4="127.0.0.1";
119 List<String> ifInfos = new ArrayList<>();
120 ifInfos.add("LP-MWPS-RADIO");
121 InventoryInformationDcae ii=new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
122 System.out.println("registering device");
123 provider.onDeviceRegistered(mountPointName,ii);
126 } catch (InterruptedException e) {
129 System.out.println("unregistering device");
130 provider.onDeviceUnregistered(mountPointName);
131 System.out.println("finished");
134 } catch (Exception e) {
139 public void test2() {
142 } catch (InterruptedException e1) {
143 e1.printStackTrace();
145 AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
147 String mountPointName = "testDevice 01";
149 String model="Horizon Compact+";
150 String vendor="DragonWave-X";
151 String ipv4="127.0.0.1";
153 List<String> ifInfos = new ArrayList<>();
154 ifInfos.add("LP-MWPS-RADIO");
155 InventoryInformationDcae ii=new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
156 System.out.println("registering device");
157 provider.onDeviceRegistered(mountPointName);
158 provider.onDeviceRegistered(mountPointName,ii);
161 } catch (InterruptedException e) {
164 System.out.println("unregistering device");
165 provider.onDeviceUnregistered(mountPointName);
166 System.out.println("finished");
169 } catch (Exception e) {
174 public void initAaiTestWebserver() throws IOException {
176 Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
177 } catch (IOException e1) {
178 fail(e1.getMessage());
180 //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
181 globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
182 this.server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
183 this.httpThreadPool = Executors.newFixedThreadPool(5);
184 this.server.setExecutor(this.httpThreadPool);
185 AaiConfig config = new AaiConfig(globalCfg);
186 this.server.createContext(config.getBaseUri(), new MyHandler());
187 //server.createContext("/", new MyRootHandler());
188 this.server.setExecutor(null); // creates a default executor
190 System.out.println("http server started");
194 public void stopTestWebserver() {
195 if (this.server != null) {
197 this.httpThreadPool.shutdownNow();
198 System.out.println("http server stopped" );
200 if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
201 ENABLEDAAI_TESTCONFIG_FILE.delete();
205 static class MyHandler implements HttpHandler {
207 public void handle(HttpExchange t) throws IOException {
208 String method = t.getRequestMethod();
209 System.out.println("req method: " + method);
210 OutputStream os = null;
213 if (method.equals("GET")) {
214 t.sendResponseHeaders(404,res.length() );
215 os = t.getResponseBody();
216 os.write(res.getBytes());
217 } else if (method.equals("DELETE")) {
218 t.sendResponseHeaders(200, res.length());
219 os = t.getResponseBody();
220 os.write(res.getBytes());
221 } else if (method.equals("PUT")) {
222 t.sendResponseHeaders(200, res.length());
223 os = t.getResponseBody();
224 os.write(res.getBytes());
226 t.sendResponseHeaders(404, 0);
228 System.out.println("req handled successful");
230 } catch (Exception e) {
231 System.out.println(e.getMessage());