02d381ff2e94bf8e78febb43acdceac63ebe76bb
[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             e.printStackTrace();
119         }
120         System.out.println("unregistering device");
121         provider.onDeviceUnregistered(new NodeId(mountPointName));
122         System.out.println("finished");
123         try {
124             provider.close();
125         } catch (Exception e) {
126             e.printStackTrace();
127         }
128     }
129
130     @Test
131     public void test2() {
132         try {
133             Thread.sleep(3000);
134         } catch (InterruptedException e1) {
135             e1.printStackTrace();
136         }
137         AaiProviderClient provider = new AaiProviderClient(globalCfg, null);
138
139         String mountPointName = "testDevice 01";
140         String type = "Unit";
141         String model = "Horizon Compact+";
142         String vendor = "DragonWave-X";
143         String ipv4 = "127.0.0.1";
144         String ipv6 = "::1";
145         List<String> ifInfos = new ArrayList<>();
146         ifInfos.add("LP-MWPS-RADIO");
147         InventoryInformationDcae ii = new InventoryInformationDcae(type, model, vendor, ipv4, ipv6, ifInfos);
148         System.out.println("registering device");
149         provider.onDeviceRegistered(new NodeId(mountPointName));
150         provider.onDeviceRegistered(new NodeId(mountPointName), ii);
151         try {
152             Thread.sleep(5000);
153         } catch (InterruptedException e) {
154             e.printStackTrace();
155         }
156         System.out.println("unregistering device");
157         provider.onDeviceUnregistered(new NodeId(mountPointName));
158         System.out.println("finished");
159         try {
160             provider.close();
161         } catch (Exception e) {
162             e.printStackTrace();
163         }
164     }
165
166     @Test
167     public void testExtendedProperties() {
168         File testConfigurationFileName = new File("abbsads.properties");
169         File extfile = new File("aaiclient.properties");
170         if (testConfigurationFileName.exists()) {
171             testConfigurationFileName.delete();
172         }
173         if (extfile.exists()) {
174             extfile.delete();
175         }
176         try {
177             Files.asCharSink(testConfigurationFileName, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT);
178         } catch (IOException e) {
179             e.printStackTrace();
180             fail("problem writing a test config file: " + e.getMessage());
181         }
182         try {
183             Files.asCharSink(extfile, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT_EXT2);
184         } catch (IOException e) {
185             e.printStackTrace();
186             fail("problem writing a second test config file: " + e.getMessage());
187         }
188
189         ConfigurationFileRepresentation cfg = new ConfigurationFileRepresentation(testConfigurationFileName);
190         AaiConfig config = new AaiConfig(cfg);
191         System.out.println(config.toString());
192
193         assertTrue(config.getBaseUrl().startsWith(EXT_TEST_URL));
194         assertEquals(EXT_TEST_KEY, config.getPcks12CertificateFilename());
195         assertEquals(EXT_TEST_PASSWD, config.getPcks12CertificatePassphrase());
196         assertEquals(EXT_TEST_APPLICATIONID, config.getHeaders().get("X-FromAppId"));
197         assertEquals(EXT_TEST_CONN_TIMEOUT, config.getConnectionTimeout());
198
199         if (testConfigurationFileName.exists()) {
200             testConfigurationFileName.delete();
201         }
202         if (extfile.exists()) {
203             extfile.delete();
204         }
205
206     }
207
208     @BeforeClass
209     public static void initAaiTestWebserver() throws IOException {
210         try {
211             Files.asCharSink(ENABLEDAAI_TESTCONFIG_FILE, StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
212         } catch (IOException e1) {
213             fail(e1.getMessage());
214         }
215         //globalCfg=HtDevicemanagerConfiguration.getTestConfiguration(ENABLEDAAI_TESTCONFIG_FILENAME,true);
216         globalCfg = new ConfigurationFileRepresentation(ENABLEDAAI_TESTCONFIG_FILENAME);
217         server = HttpServer.create(new InetSocketAddress(AAI_SERVER_PORT), 0);
218         httpThreadPool = Executors.newFixedThreadPool(5);
219         server.setExecutor(httpThreadPool);
220         AaiConfig config = new AaiConfig(globalCfg);
221         server.createContext(config.getBaseUri(), new MyHandler());
222         //server.createContext("/", new MyRootHandler());
223         server.setExecutor(null); // creates a default executor
224         server.start();
225         System.out.println("http server started");
226     }
227
228     @AfterClass
229     public static void stopTestWebserver() {
230         if (server != null) {
231             server.stop(0);
232             httpThreadPool.shutdownNow();
233             System.out.println("http server stopped");
234         }
235         if (ENABLEDAAI_TESTCONFIG_FILE.exists()) {
236             ENABLEDAAI_TESTCONFIG_FILE.delete();
237         }
238
239     }
240
241     static class MyHandler implements HttpHandler {
242         @Override
243         public void handle(HttpExchange t) throws IOException {
244             String method = t.getRequestMethod();
245             System.out.println("req method: " + method);
246             OutputStream os = null;
247             try {
248                 String res = "";
249                 if (method.equals("GET")) {
250                     t.sendResponseHeaders(404, res.length());
251                     os = t.getResponseBody();
252                     os.write(res.getBytes());
253                 } else if (method.equals("DELETE")) {
254                     t.sendResponseHeaders(200, res.length());
255                     os = t.getResponseBody();
256                     os.write(res.getBytes());
257                 } else if (method.equals("PUT")) {
258                     t.sendResponseHeaders(200, res.length());
259                     os = t.getResponseBody();
260                     os.write(res.getBytes());
261                 } else {
262                     t.sendResponseHeaders(404, 0);
263                 }
264                 System.out.println("req handled successful");
265
266             } catch (Exception e) {
267                 System.out.println(e.getMessage());
268             } finally {
269                 if (os != null) {
270                     os.close();
271                 }
272             }
273         }
274     }
275 }