4bad25ce7716f582d21cc546efa1b342cbdf2df2
[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             System.out.println("Break sleep : " + e.getMessage());
101             Thread.currentThread().interrupt();
102         }
103         try {
104             provider.close();
105         } catch (Exception e) {
106             System.out.println("Close provider error : " + e.getMessage());
107         }
108     }
109
110     @Before
111     public void initDcaeTestWebserver() throws IOException {
112         try {
113             Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
114         } catch (IOException e1) {
115             fail(e1.getMessage());
116         }
117         cfg = new ConfigurationFileRepresentation(ENABLEDDCAE_TESTCONFIG_FILENAME);
118         // cfg.reload(ENABLEDDCAE_TESTCONFIG_FILENAME);
119         /*
120          * cfg = new HtDevicemanagerConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME);
121          * DcaeConfig.reload();
122          */
123         try {
124             this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
125         } catch (Exception e) {
126             fail(e.getMessage());
127         }
128         this.httpThreadPool = Executors.newFixedThreadPool(5);
129         this.server.setExecutor(this.httpThreadPool);
130         this.server.createContext(URI, new MyHandler());
131         // server.createContext("/", new MyRootHandler());
132         this.server.setExecutor(null); // creates a default executor
133         this.server.start();
134         System.out.println("http server started");
135     }
136
137     @After
138     public void stopTestWebserver() {
139         if (this.server != null) {
140             this.server.stop(0);
141             this.httpThreadPool.shutdownNow();
142             System.out.println("http server stopped");
143         }
144         if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
145             ENABLEDDCAE_TESTCONFIG_FILE.delete();
146         }
147     }
148
149     static class MyHandler implements HttpHandler {
150         @Override
151         public void handle(HttpExchange t) throws IOException {
152             String method = t.getRequestMethod();
153             System.out.println("req method: " + method);
154             OutputStream os = null;
155             try {
156                 String res = "";
157                 if (method.equals("POST")) {
158                     t.sendResponseHeaders(200, res.length());
159                     os = t.getResponseBody();
160                     os.write(res.getBytes());
161                 } else {
162                     t.sendResponseHeaders(404, 0);
163                 }
164                 System.out.println("req handled successful");
165
166             } catch (Exception e) {
167                 System.out.println(e.getMessage());
168             } finally {
169                 if (os != null) {
170                     os.close();
171                 }
172             }
173         }
174     }
175 }