12f30cfa5837ba902f58aaa491df99094d6c9738
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.bpmn.activity;
22
23 import java.io.File;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import javax.ws.rs.core.UriBuilder;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.StatusLine;
29 import org.apache.http.client.HttpClient;
30 import org.apache.http.client.methods.HttpPost;
31 import org.apache.http.entity.StringEntity;
32 import org.apache.http.impl.client.HttpClientBuilder;
33 import org.springframework.stereotype.Component;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Component
38 public class DeployActivitySpecs {
39     private static final String ACTIVITY_FILE_LOCATION = "src/main/resources/ActivitySpec/";
40     private static final String ACTIVITY_SPEC_URI = "/activityspec-api/v1.0/activity-spec";
41     private static final String CONTENT_TYPE_JSON = "application/json";
42
43     private static final Logger logger = LoggerFactory.getLogger(DeployActivitySpecs.class);
44
45     public static void main(String[] args) throws Exception {
46
47         if (args == null || args.length == 0) {
48             logger.info("Please specify hostname argument");
49             return;
50         }
51
52         String hostname = args[0];
53
54         File dir = new File(ACTIVITY_FILE_LOCATION);
55         if (!dir.isDirectory()) {
56             logger.debug("ActivitySpec store is not a directory");
57             return;
58         }
59
60         if (dir.listFiles() != null) {
61             for (File f : dir.listFiles()) {
62                 String activitySpecName = f.getName();
63                 String errorMessage = deployActivitySpec(hostname, activitySpecName);
64                 if (errorMessage == null) {
65                     logger.debug("Deployed Activity Spec: " + activitySpecName);
66                 } else {
67                     logger.error("Error deploying Activity Spec: " + activitySpecName + " : " + errorMessage);
68                 }
69             }
70         } else {
71             logger.error("Null file list for Activity Specs.");
72         }
73     }
74
75     protected static String deployActivitySpec(String hostname, String activitySpecName) throws Exception {
76         String payload = new String(Files.readAllBytes(Paths.get(ACTIVITY_FILE_LOCATION + activitySpecName)));
77         try {
78             HttpClient client = HttpClientBuilder.create().build();
79
80             String url = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
81             HttpPost post = new HttpPost(url);
82
83             StringEntity input = new StringEntity(payload);
84             input.setContentType(CONTENT_TYPE_JSON);
85             post.setEntity(input);
86
87             HttpResponse response = client.execute(post);
88             StatusLine statusLine = response.getStatusLine();
89
90             if (statusLine != null) {
91                 if (statusLine.getStatusCode() != 200) {
92                     return (statusLine.toString());
93                 } else {
94                     return null;
95                 }
96             } else {
97                 return ("Empty response from the remote endpoint");
98             }
99
100         } catch (Exception e) {
101             return e.getMessage();
102         }
103
104     }
105 }