c1d24b4d123ac66d1db95b7ccb1f9f1c92569629
[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 com.google.common.io.Files;
26 import com.sun.net.httpserver.HttpExchange;
27 import com.sun.net.httpserver.HttpHandler;
28 import com.sun.net.httpserver.HttpServer;
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.net.InetSocketAddress;
33 import java.nio.charset.StandardCharsets;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
42 import org.onap.ccsdk.features.sdnr.wt.common.util.ResourceFileLoader;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.AaiProviderClient;
44 import org.onap.ccsdk.features.sdnr.wt.devicemanager.aaiconnector.impl.config.AaiConfig;
45 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.InventoryInformationDcae;
46
47 public class TestAai {
48
49     private static final String CONFIGURATIONTESTFILE = "test.properties"; // for
50     private static final String ENABLEDAAI_TESTCONFIG_FILENAME = "test2.properties";
51     private static final File ENABLEDAAI_TESTCONFIG_FILE = new File(ENABLEDAAI_TESTCONFIG_FILENAME);
52     private static final int AAI_SERVER_PORT=45454;
53     private static final String TESTCONFIG_CONTENT="[dcae]\n" +
54             "dcaeUserCredentials=admin:admin\n" +
55             "dcaeUrl=off\n" +
56             "dcaeHeartbeatPeriodSeconds=120\n" +
57             "dcaeTestCollector=no\n" +
58             "\n" +
59             "[aots]\n" +
60             "userPassword=passwd\n" +
61             "soapurladd=off\n" +
62             "soapaddtimeout=10\n" +
63             "soapinqtimeout=20\n" +
64             "userName=user\n" +
65             "inqtemplate=inqreq.tmpl.xml\n" +
66             "assignedto=userid\n" +
67             "addtemplate=addreq.tmpl.xml\n" +
68             "severitypassthrough=critical,major,minor,warning\n" +
69             "systemuser=user\n" +
70             "prt-offset=1200\n" +
71             "soapurlinq=off\n" +
72             "#smtpHost=\n" +
73             "#smtpPort=\n" +
74             "#smtpUsername=\n" +
75             "#smtpPassword=\n" +
76             "#smtpSender=\n" +
77             "#smtpReceivers=\n" +
78             "\n" +
79             "[es]\n" +
80             "esCluster=sendateodl5\n" +
81             "\n" +
82             "[aai]\n" +
83             "#keep comment\n" +
84             "aaiHeaders=[\"X-TransactionId: 9999\"]\n" +
85             "aaiUrl=http://localhost:"+AAI_SERVER_PORT+"\n" +
86             "aaiUserCredentials=AAI:AAI\n" +
87             "aaiDeleteOnMountpointRemove=true\n" +
88             "aaiTrustAllCerts=false\n" +
89             "aaiApiVersion=aai/v13\n" +
90             "aaiPropertiesFile=aaiclient.properties\n" +
91             "aaiApplicationId=SDNR\n" +
92             "aaiPcks12ClientCertFile=/opt/logs/externals/data/stores/keystore.client.p12\n" +
93             "aaiPcks12ClientCertPassphrase=adminadmin\n" +
94             "aaiClientConnectionTimeout=30000\n" +
95             "\n" +
96             "[pm]\n" +
97             "pmCluster=sendateodl5\n" +
98             "pmEnabled=true\n" +
99             "\n" +
100             "";
101     private HttpServer server;
102     private ExecutorService httpThreadPool;
103     private ConfigurationFileRepresentation globalCfg;
104
105     @Test
106     public void test() {
107
108         String testConfigurationFileName = ResourceFileLoader.getFile(this, CONFIGURATIONTESTFILE).getAbsolutePath();
109         ConfigurationFileRepresentation cfg=new ConfigurationFileRepresentation(testConfigurationFileName);
110
111         AaiProviderClient provider = new AaiProviderClient(cfg, null);
112
113         String mountPointName = "testDevice 01";
114         String type="Unit";
115         String model="Horizon Compact+";
116         String vendor="DragonWave-X";
117         String ipv4="127.0.0.1";
118         String ipv6="::1";
119         List<String> ifInfos = new ArrayList<>();
120         ifInfos.add("LP-MWPS-RADIO");
121         InventoryInformationDcae ii=new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
122         System.out.println("registering device");
123         provider.onDeviceRegistered(mountPointName,ii);
124         try {
125             Thread.sleep(5000);
126         } catch (InterruptedException e) {
127             e.printStackTrace();
128         }
129         System.out.println("unregistering device");
130         provider.onDeviceUnregistered(mountPointName);
131         System.out.println("finished");
132         try {
133             provider.close();
134         } catch (Exception e) {
135             e.printStackTrace();
136         }
137     }
138     @Test
139     public void test2() {
140         try {
141             Thread.sleep(3000);
142         } catch (InterruptedException e1) {
143             e1.printStackTrace();
144         }
145         AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
146
147         String mountPointName = "testDevice 01";
148         String type="Unit";
149         String model="Horizon Compact+";
150         String vendor="DragonWave-X";
151         String ipv4="127.0.0.1";
152         String ipv6="::1";
153         List<String> ifInfos = new ArrayList<>();
154         ifInfos.add("LP-MWPS-RADIO");
155         InventoryInformationDcae ii=new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
156         System.out.println("registering device");
157         provider.onDeviceRegistered(mountPointName);
158         provider.onDeviceRegistered(mountPointName,ii);
159         try {
160             Thread.sleep(5000);
161         } catch (InterruptedException e) {
162             e.printStackTrace();
163         }
164         System.out.println("unregistering device");
165         provider.onDeviceUnregistered(mountPointName);
166         System.out.println("finished");
167         try {
168             provider.close();
169         } catch (Exception e) {
170             e.printStackTrace();
171         }
172     }
173     @Before
174     public void initAaiTestWebserver() throws IOException {
175         try {
176             Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
177         } catch (IOException e1) {
178             fail(e1.getMessage());
179         }
180         //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
181         globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
182         this.server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
183         this.httpThreadPool = Executors.newFixedThreadPool(5);
184         this.server.setExecutor(this.httpThreadPool);
185         AaiConfig config = new AaiConfig(globalCfg);
186         this.server.createContext(config.getBaseUri(), new MyHandler());
187         //server.createContext("/", new MyRootHandler());
188         this.server.setExecutor(null); // creates a default executor
189         this.server.start();
190         System.out.println("http server started");
191     }
192
193     @After
194     public void stopTestWebserver() {
195         if (this.server != null) {
196             this.server.stop(0);
197             this.httpThreadPool.shutdownNow();
198             System.out.println("http server stopped" );
199         }
200         if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
201             ENABLEDAAI_TESTCONFIG_FILE.delete();
202         }
203
204     }
205     static class MyHandler implements HttpHandler {
206         @Override
207         public void handle(HttpExchange t) throws IOException {
208             String method = t.getRequestMethod();
209             System.out.println("req method: " + method);
210             OutputStream os = null;
211             try {
212                 String res="";
213                 if (method.equals("GET")) {
214                     t.sendResponseHeaders(404,res.length() );
215                     os = t.getResponseBody();
216                     os.write(res.getBytes());
217                 } else if (method.equals("DELETE")) {
218                     t.sendResponseHeaders(200, res.length());
219                     os = t.getResponseBody();
220                     os.write(res.getBytes());
221                 } else if (method.equals("PUT")) {
222                     t.sendResponseHeaders(200, res.length());
223                     os = t.getResponseBody();
224                     os.write(res.getBytes());
225                 } else {
226                     t.sendResponseHeaders(404, 0);
227                 }
228                 System.out.println("req handled successful");
229
230             } catch (Exception e) {
231                 System.out.println(e.getMessage());
232             }
233             finally {
234                 if (os != null)
235                 {
236                     os.close();
237                 }
238             }
239         }
240     }
241 }