3f0f148392fe5d8f44f42e13a2e5cf52fdcc35c5
[ccsdk/features.git] /
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
25 import java.io.File;
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;
34
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;
48
49 @SuppressWarnings("restriction")
50 public class TestDcae {
51
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" +
61             "\n" +
62             "[aots]\n" +
63             "userPassword=passwd\n" +
64             "soapurladd=off\n" +
65             "soapaddtimeout=10\n" +
66             "soapinqtimeout=20\n" +
67             "userName=user\n" +
68             "inqtemplate=inqreq.tmpl.xml\n" +
69             "assignedto=userid\n" +
70             "addtemplate=addreq.tmpl.xml\n" +
71             "severitypassthrough=critical,major,minor,warning\n" +
72             "systemuser=user\n" +
73             "prt-offset=1200\n" +
74             "soapurlinq=off\n" +
75             "#smtpHost=\n" +
76             "#smtpPort=\n" +
77             "#smtpUsername=\n" +
78             "#smtpPassword=\n" +
79             "#smtpSender=\n" +
80             "#smtpReceivers=\n" +
81             "\n" +
82             "[es]\n" +
83             "esCluster=sendateodl5\n" +
84             "\n" +
85             "[aai]\n" +
86             "#keep comment\n" +
87             "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
88             "aaiUrl=off\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" +
98             "\n" +
99             "[pm]\n" +
100             "pmCluster=sendateodl5\n" +
101             "pmEnabled=true\n" +
102             "\n" +
103             "";
104     private HttpServer server;
105     private ExecutorService httpThreadPool;
106     private ConfigurationFileRepresentation cfg;
107
108     @Test
109     public void test2() {
110         try {
111             Thread.sleep(3000);
112         } catch (InterruptedException e1) {
113             e1.printStackTrace();
114         }
115         DcaeProviderClient provider = new DcaeProviderClient(cfg,"mountpoint",null);
116
117         String mountPointName = "testDevice 01";
118         String type="Unit";
119         String model="Horizon Compact+";
120         String vendor="DragonWave-X";
121         String ipv4="127.0.0.1";
122         String ipv6="::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);
130
131         try {
132             Thread.sleep(5000);
133         } catch (InterruptedException e) {
134             e.printStackTrace();
135         }
136         try {
137             provider.close();
138         } catch (Exception e) {
139             e.printStackTrace();
140         }
141     }
142
143     @Before
144     public void initDcaeTestWebserver() throws IOException {
145         try {
146             Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
147         } catch (IOException e1) {
148             fail(e1.getMessage());
149         }
150         cfg = new ConfigurationFileRepresentation(ENABLEDDCAE_TESTCONFIG_FILENAME);
151         // cfg.reload(ENABLEDDCAE_TESTCONFIG_FILENAME);
152         /*
153          * cfg = new HtDevicemanagerConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME);
154          * DcaeConfig.reload();
155          */
156         try {
157             this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
158         } catch (Exception e) {
159             fail(e.getMessage());
160         }
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
166         this.server.start();
167         System.out.println("http server started");
168     }
169
170     @After
171     public void stopTestWebserver() {
172         if (this.server != null) {
173             this.server.stop(0);
174             this.httpThreadPool.shutdownNow();
175             System.out.println("http server stopped");
176         }
177         if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
178             ENABLEDDCAE_TESTCONFIG_FILE.delete();
179         }
180     }
181
182     static class MyHandler implements HttpHandler {
183         @Override
184         public void handle(HttpExchange t) throws IOException {
185             String method = t.getRequestMethod();
186             System.out.println("req method: " + method);
187             OutputStream os = null;
188             try {
189                 String res = "";
190                 if (method.equals("POST")) {
191                     t.sendResponseHeaders(200, res.length());
192                     os = t.getResponseBody();
193                     os.write(res.getBytes());
194                 } else {
195                     t.sendResponseHeaders(404, 0);
196                 }
197                 System.out.println("req handled successful");
198
199             } catch (Exception e) {
200                 System.out.println(e.getMessage());
201             } finally {
202                 if (os != null) {
203                     os.close();
204                 }
205             }
206         }
207     }
208 }