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;
24 import com.google.common.io.Files;
25 import com.sun.net.httpserver.HttpExchange;
26 import com.sun.net.httpserver.HttpHandler;
27 import com.sun.net.httpserver.HttpServer;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.net.InetSocketAddress;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.concurrent.ExecutorService;
36 import java.util.concurrent.Executors;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.legacy.InventoryInformation;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.test.util.ResourceFileLoader;
46 public class TestAai {
48 private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
49 private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
50 private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
51 private static final int AAI_SERVER_PORT=45454;
52 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
53 "dcaeUserCredentials=admin:admin\n" +
55 "dcaeHeartbeatPeriodSeconds=120\n" +
56 "dcaeTestCollector=no\n" +
59 "userPassword=passwd\n" +
61 "soapaddtimeout=10\n" +
62 "soapinqtimeout=20\n" +
64 "inqtemplate=inqreq.tmpl.xml\n" +
65 "assignedto=userid\n" +
66 "addtemplate=addreq.tmpl.xml\n" +
67 "severitypassthrough=critical,major,minor,warning\n" +
79 "esCluster=sendateodl5\n" +
83 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
84 "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
85 "aaiUserCredentials=AAI:AAI\n" +
86 "aaiDeleteOnMountpointRemove=true\n" +
87 "aaiTrustAllCerts=false\n" +
88 "aaiApiVersion=aai/v13\n" +
89 "aaiPropertiesFile=aaiclient.properties\n" +
90 "aaiApplicationId=SDNR\n" +
91 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
92 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
93 "aaiClientConnectionTimeout=30000\n" +
96 "pmCluster=sendateodl5\n" +
100 private HttpServer server;
101 private ExecutorService httpThreadPool;
102 private ConfigurationFileRepresentation globalCfg;
107 String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
108 ConfigurationFileRepresentation cfg=new ConfigurationFileRepresentation(testConfigurationFileName);
110 AaiProviderClient provider = new AaiProviderClient(cfg, null);
112 String mountPointName = "testDevice 01";
114 String model="Horizon Compact+";
115 String vendor="DragonWave-X";
116 String ipv4="127.0.0.1";
118 List<String> ifInfos = new ArrayList<>();
119 ifInfos.add("LP-MWPS-RADIO");
120 InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
121 System.out.println("registering device");
122 provider.onDeviceRegistered(mountPointName,ii);
125 } catch (InterruptedException e) {
128 System.out.println("unregistering device");
129 provider.onDeviceUnregistered(mountPointName);
130 System.out.println("finished");
133 } catch (Exception e) {
138 public void test2() {
141 } catch (InterruptedException e1) {
142 e1.printStackTrace();
144 AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
146 String mountPointName = "testDevice 01";
148 String model="Horizon Compact+";
149 String vendor="DragonWave-X";
150 String ipv4="127.0.0.1";
152 List<String> ifInfos = new ArrayList<>();
153 ifInfos.add("LP-MWPS-RADIO");
154 InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
155 System.out.println("registering device");
156 provider.onDeviceRegistered(mountPointName);
157 provider.onDeviceRegistered(mountPointName,ii);
160 } catch (InterruptedException e) {
163 System.out.println("unregistering device");
164 provider.onDeviceUnregistered(mountPointName);
165 System.out.println("finished");
168 } catch (Exception e) {
173 public void initAaiTestWebserver() throws IOException {
175 Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
176 } catch (IOException e1) {
177 fail(e1.getMessage());
179 //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
180 globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
181 this.server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
182 this.httpThreadPool = Executors.newFixedThreadPool(5);
183 this.server.setExecutor(this.httpThreadPool);
184 AaiConfig config = new AaiConfig(globalCfg);
185 this.server.createContext(config.getBaseUri(), new MyHandler());
186 //server.createContext("/", new MyRootHandler());
187 this.server.setExecutor(null); // creates a default executor
189 System.out.println("http server started");
193 public void stopTestWebserver() {
194 if (this.server != null) {
196 this.httpThreadPool.shutdownNow();
197 System.out.println("http server stopped" );
199 if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
200 ENABLEDAAI_TESTCONFIG_FILE.delete();
204 static class MyHandler implements HttpHandler {
206 public void handle(HttpExchange t) throws IOException {
207 String method = t.getRequestMethod();
208 System.out.println("req method: " + method);
209 OutputStream os = null;
212 if (method.equals("GET")) {
213 t.sendResponseHeaders(404,res.length() );
214 os = t.getResponseBody();
215 os.write(res.getBytes());
216 } else if (method.equals("DELETE")) {
217 t.sendResponseHeaders(200, res.length());
218 os = t.getResponseBody();
219 os.write(res.getBytes());
220 } else if (method.equals("PUT")) {
221 t.sendResponseHeaders(200, res.length());
222 os = t.getResponseBody();
223 os.write(res.getBytes());
225 t.sendResponseHeaders(404, 0);
227 System.out.println("req handled successful");
229 } catch (Exception e) {
230 System.out.println(e.getMessage());