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;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import java.net.InetSocketAddress;
29 import java.nio.charset.StandardCharsets;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.concurrent.ExecutorService;
33 import java.util.concurrent.Executors;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
41 import com.sun.net.httpserver.HttpExchange;
42 import com.sun.net.httpserver.HttpHandler;
43 import com.sun.net.httpserver.HttpServer;
44 import com.google.common.io.Files;
46 public class TestAai {
48 private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
49 private static final int AAI_SERVER_PORT=45454;
50 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
51 "dcaeUserCredentials=admin:admin\n" +
53 "dcaeHeartbeatPeriodSeconds=120\n" +
54 "dcaeTestCollector=no\n" +
57 "userPassword=passwd\n" +
59 "soapaddtimeout=10\n" +
60 "soapinqtimeout=20\n" +
62 "inqtemplate=inqreq.tmpl.xml\n" +
63 "assignedto=userid\n" +
64 "addtemplate=addreq.tmpl.xml\n" +
65 "severitypassthrough=critical,major,minor,warning\n" +
77 "esCluster=sendateodl5\n" +
81 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
82 "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
83 "aaiUserCredentials=AAI:AAI\n" +
84 "aaiDeleteOnMountpointRemove=true\n" +
85 "aaiTrustAllCerts=false\n" +
86 "aaiApiVersion=aai/v13\n" +
87 "aaiPropertiesFile=aaiclient.properties\n" +
88 "aaiApplicationId=SDNR\n" +
89 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
90 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
91 "aaiClientConnectionTimeout=30000\n" +
94 "pmCluster=sendateodl5\n" +
98 private HttpServer server;
99 private ExecutorService httpThreadPool;
100 private HtDevicemanagerConfiguration cfg;
104 HtDevicemanagerConfiguration cfg=HtDevicemanagerConfiguration.getTestConfiguration();
106 AaiProviderClient provider = new AaiProviderClient(cfg, null);
108 String mountPointName = "testDevice 01";
110 String model="Horizon Compact+";
111 String vendor="DragonWave-X";
112 String ipv4="127.0.0.1";
114 List<String> ifInfos = new ArrayList<>();
115 ifInfos.add("LP-MWPS-RADIO");
116 InventoryInformation ii=new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
117 System.out.println("registering device");
118 provider.onDeviceRegistered(mountPointName,ii);
121 } catch (InterruptedException e) {
124 System.out.println("unregistering device");
125 provider.onDeviceUnregistered(mountPointName);
126 System.out.println("finished");
129 } catch (Exception e) {
134 public void test2() {
137 } catch (InterruptedException e1) {
138 // TODO Auto-generated catch block
139 e1.printStackTrace();
141 AaiProviderClient provider = new AaiProviderClient(cfg, 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(new File(ENABLEDAAI_TESTCONFIG_FILENAME), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
173 } catch (IOException e1) {
174 fail(e1.getMessage());
176 cfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
177 cfg.getAai().reload();
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(cfg.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" );
196 static class MyHandler implements HttpHandler {
198 public void handle(HttpExchange t) throws IOException {
199 String method = t.getRequestMethod();
200 System.out.println("req method: " + method);
201 OutputStream os = null;
204 if (method.equals("GET")) {
205 t.sendResponseHeaders(404,res.length() );
206 os = t.getResponseBody();
207 os.write(res.getBytes());
208 } else if (method.equals("DELETE")) {
209 t.sendResponseHeaders(200, res.length());
210 os = t.getResponseBody();
211 os.write(res.getBytes());
212 } else if (method.equals("PUT")) {
213 t.sendResponseHeaders(200, res.length());
214 os = t.getResponseBody();
215 os.write(res.getBytes());
217 t.sendResponseHeaders(404, 0);
219 System.out.println("req handled successful");
221 } catch (Exception e) {
222 System.out.println(e.getMessage());