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.common.configuration.ConfigurationFileRepresentation;
39 import org.onap.ccsdk.features.sdnr.wt.devicemanager.dcaeconnector.impl.DcaeProviderClient;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
44 import com.google.common.io.Files;
45 import com.sun.net.httpserver.HttpExchange;
46 import com.sun.net.httpserver.HttpHandler;
47 import com.sun.net.httpserver.HttpServer;
49 @SuppressWarnings("restriction")
50 public class TestDcae {
52 private static final String ENABLEDDCAE_TESTCONFIG_FILENAME = "test2.properties";
53 private static final File ENABLEDDCAE_TESTCONFIG_FILE = new File(ENABLEDDCAE_TESTCONFIG_FILENAME);
54 private static final int DCAE_SERVER_PORT=45451;
55 private static final String URI = "/abc";
56 private static final String TESTCONFIG_CONTENT="[dcae]\n" +
57 "dcaeUserCredentials=admin:admin\n" +
58 "dcaeUrl=http://localhost:"+DCAE_SERVER_PORT+URI+"\n" +
59 "dcaeHeartbeatPeriodSeconds=120\n" +
60 "dcaeTestCollector=no\n" +
63 "userPassword=passwd\n" +
65 "soapaddtimeout=10\n" +
66 "soapinqtimeout=20\n" +
68 "inqtemplate=inqreq.tmpl.xml\n" +
69 "assignedto=userid\n" +
70 "addtemplate=addreq.tmpl.xml\n" +
71 "severitypassthrough=critical,major,minor,warning\n" +
83 "esCluster=sendateodl5\n" +
87 "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
89 "aaiUserCredentials=AAI:AAI\n" +
90 "aaiDeleteOnMountpointRemove=true\n" +
91 "aaiTrustAllCerts=false\n" +
92 "aaiApiVersion=aai/v13\n" +
93 "aaiPropertiesFile=aaiclient.properties\n" +
94 "aaiApplicationId=SDNR\n" +
95 "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
96 "aaiPcks12ClientCertPassphrase=adminadmin\n" +
97 "aaiClientConnectionTimeout=30000\n" +
100 "pmCluster=sendateodl5\n" +
104 private HttpServer server;
105 private ExecutorService httpThreadPool;
106 private ConfigurationFileRepresentation cfg;
109 public void test2() {
112 } catch (InterruptedException e1) {
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 InventoryInformationDcae(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) {
144 public void initDcaeTestWebserver() throws IOException {
146 Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
147 } catch (IOException e1) {
148 fail(e1.getMessage());
150 cfg = new ConfigurationFileRepresentation(ENABLEDDCAE_TESTCONFIG_FILENAME);
151 // cfg.reload(ENABLEDDCAE_TESTCONFIG_FILENAME);
153 * cfg = new HtDevicemanagerConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME);
154 * DcaeConfig.reload();
157 this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
158 } catch (Exception e) {
159 fail(e.getMessage());
161 this.httpThreadPool = Executors.newFixedThreadPool(5);
162 this.server.setExecutor(this.httpThreadPool);
163 this.server.createContext(URI, new MyHandler());
164 // server.createContext("/", new MyRootHandler());
165 this.server.setExecutor(null); // creates a default executor
167 System.out.println("http server started");
171 public void stopTestWebserver() {
172 if (this.server != null) {
174 this.httpThreadPool.shutdownNow();
175 System.out.println("http server stopped");
177 if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
178 ENABLEDDCAE_TESTCONFIG_FILE.delete();
182 static class MyHandler implements HttpHandler {
184 public void handle(HttpExchange t) throws IOException {
185 String method = t.getRequestMethod();
186 System.out.println("req method: " + method);
187 OutputStream os = null;
190 if (method.equals("POST")) {
191 t.sendResponseHeaders(200, res.length());
192 os = t.getResponseBody();
193 os.write(res.getBytes());
195 t.sendResponseHeaders(404, 0);
197 System.out.println("req handled successful");
199 } catch (Exception e) {
200 System.out.println(e.getMessage());