c80e84b57419a2ac6cd0cc02a74f53d729e9936d
[so.git] / asdc-controller / src / main / java / org / onap / so / asdc / activity / ActivitySpecsActions.java
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.asdc.activity;
22
23 import javax.ws.rs.core.Response;
24 import javax.ws.rs.core.UriBuilder;
25 import org.apache.http.HttpStatus;
26 import org.apache.http.entity.ContentType;
27 import org.onap.so.asdc.activity.beans.ActivitySpec;
28 import org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse;
29 import org.onap.so.client.HttpClient;
30 import org.onap.so.client.HttpClientFactory;
31 import org.onap.so.utils.TargetEntity;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.stereotype.Component;
35 import com.fasterxml.jackson.annotation.JsonInclude.Include;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import java.net.URL;
38
39 @Component
40 public class ActivitySpecsActions {
41
42     private static final String ACTIVITY_SPEC_URI = "/v1.0/activity-spec";
43     private static final String ACTIVITY_SPEC_URI_SUFFIX = "/versions/latest/actions";
44     private static final String CERTIFY_ACTIVITY_PAYLOAD = "{\"action\": \"CERTIFY\"}";
45
46     private final HttpClientFactory httpClientFactory = new HttpClientFactory();
47     protected static final Logger logger = LoggerFactory.getLogger(ActivitySpecsActions.class);
48
49     public String createActivitySpec(String hostname, ActivitySpec activitySpec) {
50         if (activitySpec == null) {
51             return null;
52         }
53
54         String activitySpecId = null;
55
56         try {
57             ObjectMapper mapper = new ObjectMapper();
58             mapper.setSerializationInclusion(Include.NON_NULL);
59             String payload = mapper.writer().writeValueAsString(activitySpec);
60
61             String urlString = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
62             URL url = new URL(urlString);
63
64             HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
65             httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
66
67             Response response = httpClient.post(payload);
68
69             int statusCode = response.getStatus();
70             if (statusCode != HttpStatus.SC_OK) {
71                 logger.warn("{} {} {}", "Error creating activity spec", activitySpec.getName(), statusCode);
72             } else {
73                 if (response.getEntity() != null) {
74                     ActivitySpecCreateResponse activitySpecCreateResponse =
75                             response.readEntity(ActivitySpecCreateResponse.class);
76                     if (activitySpecCreateResponse != null) {
77                         activitySpecId = activitySpecCreateResponse.getId();
78                     } else {
79                         logger.warn("{} {}", "Unable to read activity spec", activitySpec.getName());
80                     }
81                 } else {
82                     logger.warn("{} {}", "No activity spec response returned", activitySpec.getName());
83                 }
84             }
85         } catch (Exception e) {
86             logger.warn("{} {}", "Exception creating activitySpec", e.getMessage());
87         }
88
89         return activitySpecId;
90     }
91
92     public boolean certifyActivitySpec(String hostname, String activitySpecId) {
93         boolean certificationResult = false;
94         if (activitySpecId == null) {
95             return false;
96         }
97
98         try {
99             String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
100
101             String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
102             URL url = new URL(urlString);
103
104             HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
105             httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
106
107             Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
108
109             int statusCode = response.getStatus();
110
111             if (statusCode != HttpStatus.SC_OK) {
112                 logger.warn("{} {} {}", "Error certifying activity", activitySpecId, statusCode);
113             } else {
114                 certificationResult = true;
115             }
116
117         } catch (Exception e) {
118             logger.warn("{} {}", "Exception certifying activitySpec", e.getMessage());
119         }
120
121         return certificationResult;
122     }
123 }