[SO] Release so 1.13.0 image
[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  * Modifications Copyright (c) 2020 Nokia
8  * ================================================================================
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.so.asdc.activity;
25
26 import javax.ws.rs.core.Response;
27 import javax.ws.rs.core.UriBuilder;
28 import org.onap.so.logger.LoggingAnchor;
29 import org.apache.http.HttpStatus;
30 import org.apache.http.entity.ContentType;
31 import org.onap.so.asdc.activity.beans.ActivitySpec;
32 import org.onap.so.asdc.activity.beans.ActivitySpecCreateResponse;
33 import org.onap.so.client.HttpClient;
34 import org.onap.so.client.HttpClientFactory;
35 import org.onap.logging.filter.base.ONAPComponents;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39 import com.fasterxml.jackson.annotation.JsonInclude.Include;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41 import java.net.URL;
42
43 @Component
44 public class ActivitySpecsActions {
45
46     private static final String ACTIVITY_SPEC_URI = "/v1.0/activity-spec";
47     private static final String ACTIVITY_SPEC_URI_SUFFIX = "/versions/latest/actions";
48     private static final String CERTIFY_ACTIVITY_PAYLOAD = "{\"action\": \"CERTIFY\"}";
49
50     private final HttpClientFactory httpClientFactory = new HttpClientFactory();
51     protected static final Logger logger = LoggerFactory.getLogger(ActivitySpecsActions.class);
52
53     public String createActivitySpec(String hostname, ActivitySpec activitySpec) {
54         if (activitySpec == null) {
55             return null;
56         }
57
58         try {
59             ObjectMapper mapper = new ObjectMapper();
60             mapper.setSerializationInclusion(Include.NON_NULL);
61             String payload = mapper.writer().writeValueAsString(activitySpec);
62
63             String urlString = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
64             URL url = new URL(urlString);
65
66             HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
67             httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
68
69             Response response = httpClient.post(payload);
70
71             int statusCode = response.getStatus();
72             if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
73                 logger.warn(LoggingAnchor.THREE, "ActivitySpec", activitySpec.getName(), "already exists in SDC");
74                 return null;
75             }
76             if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
77                 logger.warn(LoggingAnchor.THREE, "Error creating activity spec", activitySpec.getName(), statusCode);
78                 return null;
79             }
80
81             if (response.getEntity() == null) {
82                 logger.warn(LoggingAnchor.TWO, "No activity spec response returned", activitySpec.getName());
83                 return null;
84             }
85             ActivitySpecCreateResponse activitySpecCreateResponse =
86                     response.readEntity(ActivitySpecCreateResponse.class);
87             if (activitySpecCreateResponse == null) {
88                 logger.warn(LoggingAnchor.TWO, "Unable to read activity spec", activitySpec.getName());
89                 return null;
90             }
91             return activitySpecCreateResponse.getId();
92
93
94         } catch (Exception e) {
95             logger.warn(LoggingAnchor.TWO, "Exception creating activitySpec", e);
96         }
97
98         return null;
99     }
100
101     public boolean certifyActivitySpec(String hostname, String activitySpecId) {
102         if (activitySpecId == null) {
103             return false;
104         }
105
106         try {
107             String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
108
109             String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
110             URL url = new URL(urlString);
111
112             HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
113             httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
114
115             Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
116
117             int statusCode = response.getStatus();
118
119             if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
120                 logger.warn(LoggingAnchor.THREE, "ActivitySpec with id", activitySpecId, "is already certified in SDC");
121                 return false;
122             }
123             if (statusCode != HttpStatus.SC_OK) {
124                 logger.warn(LoggingAnchor.THREE, "Error certifying activity", activitySpecId, statusCode);
125                 return false;
126             }
127
128             return true;
129
130         } catch (Exception e) {
131             logger.warn(LoggingAnchor.TWO, "Exception certifying activitySpec", e);
132             return false;
133         }
134
135     }
136 }