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_OK) {
71 logger.warn("{} {} {}", "Error creating activity spec", activitySpec.getName(), statusCode);
73 if (response.getEntity() != null) {
74 ActivitySpecCreateResponse activitySpecCreateResponse =
75 response.readEntity(ActivitySpecCreateResponse.class);
76 if (activitySpecCreateResponse != null) {
77 activitySpecId = activitySpecCreateResponse.getId();
79 logger.warn("{} {}", "Unable to read activity spec", activitySpec.getName());
82 logger.warn("{} {}", "No activity spec response returned", activitySpec.getName());
85 } catch (Exception e) {
86 logger.warn("{} {}", "Exception creating activitySpec", e.getMessage());
89 return activitySpecId;
92 public boolean certifyActivitySpec(String hostname, String activitySpecId) {
93 boolean certificationResult = false;
94 if (activitySpecId == null) {
99 String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
101 String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
102 URL url = new URL(urlString);
104 HttpClient httpClient = httpClientFactory.newJsonClient(url, TargetEntity.SDC);
105 httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
107 Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
109 int statusCode = response.getStatus();
111 if (statusCode != HttpStatus.SC_OK) {
112 logger.warn("{} {} {}", "Error certifying activity", activitySpecId, statusCode);
114 certificationResult = true;
117 } catch (Exception e) {
118 logger.warn("{} {}", "Exception certifying activitySpec", e.getMessage());
121 return certificationResult;