a403fe4a62eaa298ecd951e20dd6df84ed668cc2
[ccsdk/features.git] / sdnr / wt / devicemanager-core / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / test / TestDcae.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk feature sdnr wt
4  *  ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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;
22
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;
28 import java.io.File;
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.dcaeconnector.impl.DcaeProviderClient;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalDateAndTime;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.util.InternalSeverity;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47
48 @SuppressWarnings("restriction")
49 public class TestDcae {
50
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" + "dcaeUserCredentials=admin:admin\n"
56             + "dcaeUrl=http://localhost:" + DCAE_SERVER_PORT + URI + "\n" + "dcaeHeartbeatPeriodSeconds=120\n"
57             + "dcaeTestCollector=no\n" + "\n" + "[aots]\n" + "userPassword=passwd\n" + "soapurladd=off\n"
58             + "soapaddtimeout=10\n" + "soapinqtimeout=20\n" + "userName=user\n" + "inqtemplate=inqreq.tmpl.xml\n"
59             + "assignedto=userid\n" + "addtemplate=addreq.tmpl.xml\n"
60             + "severitypassthrough=critical,major,minor,warning\n" + "systemuser=user\n" + "prt-offset=1200\n"
61             + "soapurlinq=off\n" + "#smtpHost=\n" + "#smtpPort=\n" + "#smtpUsername=\n" + "#smtpPassword=\n"
62             + "#smtpSender=\n" + "#smtpReceivers=\n" + "\n" + "[es]\n" + "esCluster=sendateodl5\n" + "\n" + "[aai]\n"
63             + "#keep comment\n" + "aaiHeaders=[\"X-TransactionId: 9999\"]\n" + "aaiUrl=off\n"
64             + "aaiUserCredentials=AAI:AAI\n" + "aaiDeleteOnMountpointRemove=true\n" + "aaiTrustAllCerts=false\n"
65             + "aaiApiVersion=aai/v13\n" + "aaiPropertiesFile=aaiclient.properties\n" + "aaiApplicationId=SDNR\n"
66             + "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n"
67             + "aaiPcks12ClientCertPassphrase=adminadmin\n" + "aaiClientConnectionTimeout=30000\n" + "\n" + "[pm]\n"
68             + "pmCluster=sendateodl5\n" + "pmEnabled=true\n" + "\n" + "";
69     private HttpServer server;
70     private ExecutorService httpThreadPool;
71     private ConfigurationFileRepresentation cfg;
72
73     @Test
74     public void test2() {
75         try {
76             Thread.sleep(3000);
77         } catch (InterruptedException e1) {
78             e1.printStackTrace();
79         }
80         DcaeProviderClient provider = new DcaeProviderClient(cfg, "mountpoint", null);
81
82         String mountPointName = "testDevice 01";
83         String type = "Unit";
84         String model = "Horizon Compact+";
85         String vendor = "DragonWave-X";
86         String ipv4 = "127.0.0.1";
87         String ipv6 = "::1";
88         List<String> ifInfos = new ArrayList<>();
89         ifInfos.add("LP-MWPS-RADIO");
90         new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
91         System.out.println("registering device");
92         boolean neDeviceAlarm = false;
93         ProblemNotificationXml notification = new ProblemNotificationXml(mountPointName, "network-element",
94                 "problemName", InternalSeverity.Critical, 123, InternalDateAndTime.getTestpattern());
95         provider.sendProblemNotification(new NodeId(mountPointName), notification, neDeviceAlarm);
96
97         try {
98             Thread.sleep(5000);
99         } catch (InterruptedException e) {
100             e.printStackTrace();
101         }
102         try {
103             provider.close();
104         } catch (Exception e) {
105             e.printStackTrace();
106         }
107     }
108
109     @Before
110     public void initDcaeTestWebserver() throws IOException {
111         try {
112             Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
113         } catch (IOException e1) {
114             fail(e1.getMessage());
115         }
116         cfg = new ConfigurationFileRepresentation(ENABLEDDCAE_TESTCONFIG_FILENAME);
117         // cfg.reload(ENABLEDDCAE_TESTCONFIG_FILENAME);
118         /*
119          * cfg = new HtDevicemanagerConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME);
120          * DcaeConfig.reload();
121          */
122         try {
123             this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
124         } catch (Exception e) {
125             fail(e.getMessage());
126         }
127         this.httpThreadPool = Executors.newFixedThreadPool(5);
128         this.server.setExecutor(this.httpThreadPool);
129         this.server.createContext(URI, new MyHandler());
130         // server.createContext("/", new MyRootHandler());
131         this.server.setExecutor(null); // creates a default executor
132         this.server.start();
133         System.out.println("http server started");
134     }
135
136     @After
137     public void stopTestWebserver() {
138         if (this.server != null) {
139             this.server.stop(0);
140             this.httpThreadPool.shutdownNow();
141             System.out.println("http server stopped");
142         }
143         if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
144             ENABLEDDCAE_TESTCONFIG_FILE.delete();
145         }
146     }
147
148     static class MyHandler implements HttpHandler {
149         @Override
150         public void handle(HttpExchange t) throws IOException {
151             String method = t.getRequestMethod();
152             System.out.println("req method: " + method);
153             OutputStream os = null;
154             try {
155                 String res = "";
156                 if (method.equals("POST")) {
157                     t.sendResponseHeaders(200, res.length());
158                     os = t.getResponseBody();
159                     os.write(res.getBytes());
160                 } else {
161                     t.sendResponseHeaders(404, 0);
162                 }
163                 System.out.println("req handled successful");
164
165             } catch (Exception e) {
166                 System.out.println(e.getMessage());
167             } finally {
168                 if (os != null) {
169                     os.close();
170                 }
171             }
172         }
173     }
174 }