YANG Model update for A1 Adapter
[ccsdk/features.git] / sdnr / wt / devicemanager / 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.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;
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" +
56             "dcaeUserCredentials=admin:admin\n" +
57             "dcaeUrl=http://localhost:"+DCAE_SERVER_PORT+URI+"\n" +
58             "dcaeHeartbeatPeriodSeconds=120\n" +
59             "dcaeTestCollector=no\n" +
60             "\n" +
61             "[aots]\n" +
62             "userPassword=passwd\n" +
63             "soapurladd=off\n" +
64             "soapaddtimeout=10\n" +
65             "soapinqtimeout=20\n" +
66             "userName=user\n" +
67             "inqtemplate=inqreq.tmpl.xml\n" +
68             "assignedto=userid\n" +
69             "addtemplate=addreq.tmpl.xml\n" +
70             "severitypassthrough=critical,major,minor,warning\n" +
71             "systemuser=user\n" +
72             "prt-offset=1200\n" +
73             "soapurlinq=off\n" +
74             "#smtpHost=\n" +
75             "#smtpPort=\n" +
76             "#smtpUsername=\n" +
77             "#smtpPassword=\n" +
78             "#smtpSender=\n" +
79             "#smtpReceivers=\n" +
80             "\n" +
81             "[es]\n" +
82             "esCluster=sendateodl5\n" +
83             "\n" +
84             "[aai]\n" +
85             "#keep comment\n" +
86             "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
87             "aaiUrl=off\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" +
97             "\n" +
98             "[pm]\n" +
99             "pmCluster=sendateodl5\n" +
100             "pmEnabled=true\n" +
101             "\n" +
102             "";
103     private HttpServer server;
104     private ExecutorService httpThreadPool;
105     private HtDevicemanagerConfiguration cfg;
106
107     @Test
108     public void test2() {
109         try {
110             Thread.sleep(3000);
111         } catch (InterruptedException e1) {
112             // TODO Auto-generated catch block
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 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);
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     @Before
143     public void initDcaeTestWebserver() throws IOException {
144         try {
145             Files.asCharSink(ENABLEDDCAE_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
146         } catch (IOException e1) {
147             fail(e1.getMessage());
148         }
149         cfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDDCAE_TESTCONFIG_FILENAME,true);
150         DcaeConfig.reload();
151         try
152         {
153         this.server = HttpServer.create(new InetSocketAddress(DCAE_SERVER_PORT), 0);
154         }
155         catch(Exception e) {
156             fail(e.getMessage());
157         }
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
163         this.server.start();
164         System.out.println("http server started");
165     }
166
167     @After
168     public void stopTestWebserver() {
169         if (this.server != null) {
170             this.server.stop(0);
171             this.httpThreadPool.shutdownNow();
172             System.out.println("http server stopped" );
173         }
174         if (ENABLEDDCAE_TESTCONFIG_FILE.exists()) {
175             ENABLEDDCAE_TESTCONFIG_FILE.delete();
176         }
177     }
178     static class MyHandler implements HttpHandler {
179         @Override
180         public void handle(HttpExchange t) throws IOException {
181             String method = t.getRequestMethod();
182             System.out.println("req method: " + method);
183             OutputStream os = null;
184             try {
185                 String res="";
186                 if (method.equals("POST")) {
187                     t.sendResponseHeaders(200,res.length() );
188                     os = t.getResponseBody();
189                     os.write(res.getBytes());
190                } else {
191                     t.sendResponseHeaders(404, 0);
192                 }
193                 System.out.println("req handled successful");
194
195             } catch (Exception e) {
196                 System.out.println(e.getMessage());
197             }
198             finally {
199                 if (os != null)
200                 {
201                     os.close();
202                 }
203             }
204         }
205     }
206 }