619d89438e787eb647b4f6ff168c53ca60bd128d
[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_UNPROCESSABLE_ENTITY) {
71                 logger.warn("{} {} {}", "ActivitySpec", activitySpec.getName(), "already exists in SDC");
72             } else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
73                 logger.warn("{} {} {}", "Error creating activity spec", activitySpec.getName(), statusCode);
74             } else {
75                 if (response.getEntity() != null) {
76                     ActivitySpecCreateResponse activitySpecCreateResponse =
77                             response.readEntity(ActivitySpecCreateResponse.class);
78                     if (activitySpecCreateResponse != null) {
79                         activitySpecId = activitySpecCreateResponse.getId();
80                     } else {
81                         logger.warn("{} {}", "Unable to read activity spec", activitySpec.getName());
82                     }
83                 } else {
84                     logger.warn("{} {}", "No activity spec response returned", activitySpec.getName());
85                 }
86             }
87         } catch (Exception e) {
88             logger.warn("{} {}", "Exception creating activitySpec", e.getMessage());
89         }
90
91         return activitySpecId;
92     }
93
94     public boolean certifyActivitySpec(String hostname, String activitySpecId) {
95         boolean certificationResult = false;
96         if (activitySpecId == null) {
97             return false;
98         }
99
100         try {
101             String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
102
103             String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
104             URL url = new URL(urlString);
105
106             HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
107             httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
108
109             Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
110
111             int statusCode = response.getStatus();
112
113             if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
114                 logger.warn("{} {} {}", "ActivitySpec with id", activitySpecId, "is already certified in SDC");
115             } else if (statusCode != HttpStatus.SC_OK) {
116                 logger.warn("{} {} {}", "Error certifying activity", activitySpecId, statusCode);
117             } else {
118                 certificationResult = true;
119             }
120
121         } catch (Exception e) {
122             logger.warn("{} {}", "Exception certifying activitySpec", e.getMessage());
123         }
124
125         return certificationResult;
126     }
127 }