2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.asdc.activity;
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;
40 public class ActivitySpecsActions {
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\"}";
46 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
47 protected static final Logger logger = LoggerFactory.getLogger(ActivitySpecsActions.class);
49 public String createActivitySpec(String hostname, ActivitySpec activitySpec) {
50 if (activitySpec == null) {
54 String activitySpecId = null;
57 ObjectMapper mapper = new ObjectMapper();
58 mapper.setSerializationInclusion(Include.NON_NULL);
59 String payload = mapper.writer().writeValueAsString(activitySpec);
61 String urlString = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
62 URL url = new URL(urlString);
64 HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
65 httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
67 Response response = httpClient.post(payload);
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);
75 if (response.getEntity() != null) {
76 ActivitySpecCreateResponse activitySpecCreateResponse =
77 response.readEntity(ActivitySpecCreateResponse.class);
78 if (activitySpecCreateResponse != null) {
79 activitySpecId = activitySpecCreateResponse.getId();
81 logger.warn("{} {}", "Unable to read activity spec", activitySpec.getName());
84 logger.warn("{} {}", "No activity spec response returned", activitySpec.getName());
87 } catch (Exception e) {
88 logger.warn("{} {}", "Exception creating activitySpec", e.getMessage());
91 return activitySpecId;
94 public boolean certifyActivitySpec(String hostname, String activitySpecId) {
95 boolean certificationResult = false;
96 if (activitySpecId == null) {
101 String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
103 String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
104 URL url = new URL(urlString);
106 HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
107 httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
109 Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
111 int statusCode = response.getStatus();
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);
118 certificationResult = true;
121 } catch (Exception e) {
122 logger.warn("{} {}", "Exception certifying activitySpec", e.getMessage());
125 return certificationResult;