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