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.devicemanager.aaiconnector.impl.AaiProviderClient;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.AaiConfig;
45 @SuppressWarnings("restriction")
46 public class TestAai {
48 private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
49 private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
50 private static final int AAI_SERVER_PORT=45454;
51 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
52 "dcaeUserCredentials=admin:admin\n" +
54 "dcaeHeartbeatPeriodSeconds=120\n" +
55 "dcaeTestCollector=no\n" +
58 "userPassword=passwd\n" +
60 "soapaddtimeout=10\n" +
61 "soapinqtimeout=20\n" +
63 "inqtemplate=inqreq.tmpl.xml\n" +
64 "assignedto=userid\n" +
65 "addtemplate=addreq.tmpl.xml\n" +
66 "severitypassthrough=critical,major,minor,warning\n" +
78 "esCluster=sendateodl5\n" +
82 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
83 "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
84 "aaiUserCredentials=AAI:AAI\n" +
85 "aaiDeleteOnMountpointRemove=true\n" +
86 "aaiTrustAllCerts=false\n" +
87 "aaiApiVersion=aai/v13\n" +
88 "aaiPropertiesFile=aaiclient.properties\n" +
89 "aaiApplicationId=SDNR\n" +
90 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
91 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
92 "aaiClientConnectionTimeout=30000\n" +
95 "pmCluster=sendateodl5\n" +
99 private HttpServer server;
100 private ExecutorService httpThreadPool;
101 private HtDevicemanagerConfiguration globalCfg;
105 HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration();
107 AaiProviderClient provider = new AaiProviderClient(cfg, null);
109 String mountPointName = "testDevice 01";
111 String model="Horizon Compact+";
112 String vendor="DragonWave-X";
113 String ipv4="127.0.0.1";
115 List<String> ifInfos = new ArrayList<>();
116 ifInfos.add("LP-MWPS-RADIO");
117 InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
118 System.out.println("registering device");
119 provider.onDeviceRegistered(mountPointName,ii);
122 } catch (InterruptedException e) {
125 System.out.println("unregistering device");
126 provider.onDeviceUnregistered(mountPointName);
127 System.out.println("finished");
130 } catch (Exception e) {
135 public void test2() {
138 } catch (InterruptedException e1) {
139 e1.printStackTrace();
141 AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
143 String mountPointName = "testDevice 01";
145 String model="Horizon Compact+";
146 String vendor="DragonWave-X";
147 String ipv4="127.0.0.1";
149 List<String> ifInfos = new ArrayList<>();
150 ifInfos.add("LP-MWPS-RADIO");
151 InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
152 System.out.println("registering device");
153 provider.onDeviceRegistered(mountPointName);
154 provider.onDeviceRegistered(mountPointName,ii);
157 } catch (InterruptedException e) {
160 System.out.println("unregistering device");
161 provider.onDeviceUnregistered(mountPointName);
162 System.out.println("finished");
165 } catch (Exception e) {
170 public void initAaiTestWebserver() throws IOException {
172 Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
173 } catch (IOException e1) {
174 fail(e1.getMessage());
176 globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
178 this.server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
179 this.httpThreadPool = Executors.newFixedThreadPool(5);
180 this.server.setExecutor(this.httpThreadPool);
181 this.server.createContext(globalCfg.getAai().getBaseUri(), new MyHandler());
182 //server.createContext("/", new MyRootHandler());
183 this.server.setExecutor(null); // creates a default executor
185 System.out.println("http server started");
189 public void stopTestWebserver() {
190 if (this.server != null) {
192 this.httpThreadPool.shutdownNow();
193 System.out.println("http server stopped" );
195 if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
196 ENABLEDAAI_TESTCONFIG_FILE.delete();
200 static class MyHandler implements HttpHandler {
202 public void handle(HttpExchange t) throws IOException {
203 String method = t.getRequestMethod();
204 System.out.println("req method: " + method);
205 OutputStream os = null;
208 if (method.equals("GET")) {
209 t.sendResponseHeaders(404,res.length() );
210 os = t.getResponseBody();
211 os.write(res.getBytes());
212 } else if (method.equals("DELETE")) {
213 t.sendResponseHeaders(200, res.length());
214 os = t.getResponseBody();
215 os.write(res.getBytes());
216 } else if (method.equals("PUT")) {
217 t.sendResponseHeaders(200, res.length());
218 os = t.getResponseBody();
219 os.write(res.getBytes());
221 t.sendResponseHeaders(404, 0);
223 System.out.println("req handled successful");
225 } catch (Exception e) {
226 System.out.println(e.getMessage());