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.base.internalTypes.InternalDateAndTime;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InternalSeverity;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.HtDevicemanagerConfiguration;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.config.impl.DcaeConfig;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeProviderClient;
46 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
48 @SuppressWarnings("restriction")
49 public class TestDcae {
51 private static final String ENABLEDDCAE_TESTCONFIG_FILENAME = "test2.properties";
52 private static final File ENABLEDDCAE_TESTCONFIG_FILE = new File(ENABLEDDCAE_TESTCONFIG_FILENAME);
53 private static final int DCAE_SERVER_PORT=45451;
54 private static final String URI = "/abc";
55 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
56 "dcaeUserCredentials=admin:admin\n" +
57 "dcaeUrl=http://localhost:"+DCAE_SERVER_PORT+URI+"\n" +
58 "dcaeHeartbeatPeriodSeconds=120\n" +
59 "dcaeTestCollector=no\n" +
62 "userPassword=passwd\n" +
64 "soapaddtimeout=10\n" +
65 "soapinqtimeout=20\n" +
67 "inqtemplate=inqreq.tmpl.xml\n" +
68 "assignedto=userid\n" +
69 "addtemplate=addreq.tmpl.xml\n" +
70 "severitypassthrough=critical,major,minor,warning\n" +
82 "esCluster=sendateodl5\n" +
86 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
88 "aaiUserCredentials=AAI:AAI\n" +
89 "aaiDeleteOnMountpointRemove=true\n" +
90 "aaiTrustAllCerts=false\n" +
91 "aaiApiVersion=aai/v13\n" +
92 "aaiPropertiesFile=aaiclient.properties\n" +
93 "aaiApplicationId=SDNR\n" +
94 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
95 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
96 "aaiClientConnectionTimeout=30000\n" +
99 "pmCluster=sendateodl5\n" +
103 private HttpServer server;
104 private ExecutorService httpThreadPool;
105 private HtDevicemanagerConfiguration cfg;
108 public void test2() {
111 } catch (InterruptedException e1) {
112 // TODO Auto-generated catch block
113 e1.printStackTrace();
115 DcaeProviderClient provider = new DcaeProviderClient(cfg,"mountpoint",null);
117 String mountPointName = "testDevice 01";
119 String model="Horizon Compact+";
120 String vendor="DragonWave-X";
121 String ipv4="127.0.0.1";
123 List<String> ifInfos = new ArrayList<>();
124 ifInfos.add("LP-MWPS-RADIO");
125 new InventoryInformation(type, model, vendor, ipv4, ipv6, ifInfos);
126 System.out.println("registering device");
127 boolean neDeviceAlarm = false;
128 ProblemNotificationXml notification = new ProblemNotificationXml(mountPointName, "network-element", "problemName", InternalSeverity.Critical,"123", InternalDateAndTime.getTestpattern());
129 provider.sendProblemNotification(mountPointName, notification, neDeviceAlarm);
133 } catch (InterruptedException e) {
138 } catch (Exception e) {
143 public void initDcaeTestWebserver() throws IOException {
145 Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
146 } catch (IOException e1) {
147 fail(e1.getMessage());
149 cfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME,true);
153 this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
156 fail(e.getMessage());
158 this.httpThreadPool = Executors.newFixedThreadPool(5);
159 this.server.setExecutor(this.httpThreadPool);
160 this.server.createContext(URI, new MyHandler());
161 //server.createContext("/", new MyRootHandler());
162 this.server.setExecutor(null); // creates a default executor
164 System.out.println("http server started");
168 public void stopTestWebserver() {
169 if (this.server != null) {
171 this.httpThreadPool.shutdownNow();
172 System.out.println("http server stopped" );
174 if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
175 ENABLEDDCAE_TESTCONFIG_FILE.delete();
178 static class MyHandler implements HttpHandler {
180 public void handle(HttpExchange t) throws IOException {
181 String method = t.getRequestMethod();
182 System.out.println("req method: " + method);
183 OutputStream os = null;
186 if (method.equals("POST")) {
187 t.sendResponseHeaders(200,res.length() );
188 os = t.getResponseBody();
189 os.write(res.getBytes());
191 t.sendResponseHeaders(404, 0);
193 System.out.println("req handled successful");
195 } catch (Exception e) {
196 System.out.println(e.getMessage());