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