First part of onap rename
[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.onap.appc.simulator.client.main;
26
27 import org.apache.commons.io.filefilter.WildcardFileFilter;
28 import org.onap.appc.simulator.client.RequestHandler;
29 import org.onap.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
41     public static void main(String ... args) throws Exception {
42         String folder = args[0];
43         if (folder == null) {
44             folder = System.getProperty("user.dir");
45         }
46         System.out.println("== THR#" +Thread.currentThread().getId()+ " Reading files under the folder : " + folder);
47
48         String inputType = args[1];
49         if (inputType != null && !inputType.matches("JSON")) {
50             throw new RuntimeException("### ERROR ### - Unsupported file type <" + inputType + "> was provided");
51         }
52
53         Properties properties = getProperties(folder);
54         RequestHandler reqHandler = new JsonRequestHandler(properties);
55         List<File> sources = getJsonFiles(folder);
56         File log = new File(folder + "/output.txt");
57         int filesNum = 0;
58         for (File source: sources) {
59             reqHandler.proceedFile(source, log);
60             System.out.println("== THR#" +Thread.currentThread().getId()+ " File <" + source.getName() + "> processed.");
61             ++filesNum;
62         }
63         System.out.println("DONE with " + filesNum + " files under the folder : " + folder);
64         Thread.sleep(30);
65         System.out.println("Shutdown ...");
66         reqHandler.shutdown(Boolean.parseBoolean(properties.getProperty("client.force.shutdown")));
67 //        System.exit(0);
68     }
69
70      private static Properties getProperties(String folder) {
71         Properties prop = new Properties();
72
73         InputStream conf = null;
74         try {
75             conf = new FileInputStream(folder + "client-simulator.properties");
76         } catch (FileNotFoundException e) {
77
78         }
79         if (conf != null) {
80             try {
81                 prop.load(conf);
82             } catch (IOException e) {
83                 e.printStackTrace();
84             }
85         } else {
86             try {
87                 prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("client-simulator.properties"));
88             } catch (Exception e) {
89                 throw new RuntimeException("### ERROR ### - Could not load properties to test");
90             }
91         }
92         return prop;
93     }
94
95      private static List<File> getJsonFiles(String folder) throws FileNotFoundException {
96         Path dir = Paths.get(folder);
97         FileFilter fileFilter = new WildcardFileFilter("*.json");
98         return new ArrayList<File>(Arrays.asList(dir.toFile().listFiles(fileFilter)));
99     }
100 }