Initial add of APPC client libraries
[appc.git] / appc-client / client-simulator / src / main / java / org / openecomp / appc / simulator / client / main / ClientRunner.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.simulator.client.main;
26
27 import org.apache.commons.io.filefilter.WildcardFileFilter;
28 import org.openecomp.appc.simulator.client.RequestHandler;
29 import org.openecomp.appc.simulator.client.impl.JsonRequestHandler;
30
31 import java.io.*;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Properties;
38
39 public class ClientRunner {
40     public static void main(String ... args) throws Exception {
41         String folder = args[0];
42         if (folder == null) {
43             folder = System.getProperty("user.dir");
44         }
45         System.out.println("== THR#" +Thread.currentThread().getId()+ " Reading files under the folder : " + folder);
46
47         String inputType = args[1];
48         if (inputType != null && !inputType.matches("JSON")) {
49             throw new RuntimeException("### ERROR ### - Unsupported file type <" + inputType + "> was provided");
50         }
51
52         Properties properties = getProperties(folder);
53         RequestHandler reqHandler = new JsonRequestHandler(properties);
54         List<File> sources = getJsonFiles(folder);
55         File log = new File(folder + "/output.txt");
56         int filesNum = 0;
57         for (File source: sources) {
58             reqHandler.proceedFile(source, log);
59             System.out.println("== THR#" +Thread.currentThread().getId()+ " File <" + source.getName() + "> processed.");
60             ++filesNum;
61         }
62         System.out.println("DONE with " + filesNum + " files under the folder : " + folder);
63         Thread.sleep(30);
64         System.out.println("Shutdown ...");
65         reqHandler.shutdown(Boolean.parseBoolean(properties.getProperty("client.force.shutdown")));
66 //        System.exit(0);
67     }
68
69     private static Properties getProperties(String folder) {
70         Properties prop = new Properties();
71
72         InputStream conf = null;
73         try {
74             conf = new FileInputStream(folder + "client-simulator.properties");
75         } catch (FileNotFoundException e) {
76
77         }
78         if (conf != null) {
79             try {
80                 prop.load(conf);
81             } catch (IOException e) {
82                 e.printStackTrace();
83             }
84         } else {
85             try {
86                 prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties"));
87             } catch (Exception e) {
88                 throw new RuntimeException("### ERROR ### - Could not load properties to test");
89             }
90         }
91         return prop;
92     }
93
94     private static List<File> getJsonFiles(String folder) throws FileNotFoundException {
95         Path dir = Paths.get(folder);
96         FileFilter fileFilter = new WildcardFileFilter("*.json");
97         return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));
98     }
99 }