2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2020 Nokia
8 * ================================================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
24 package org.onap.so.asdc.activity;
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;
44 public class ActivitySpecsActions {
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\"}";
50 private final HttpClientFactory httpClientFactory = new HttpClientFactory();
51 protected static final Logger logger = LoggerFactory.getLogger(ActivitySpecsActions.class);
53 public String createActivitySpec(String hostname, ActivitySpec activitySpec) {
54 if (activitySpec == null) {
59 ObjectMapper mapper = new ObjectMapper();
60 mapper.setSerializationInclusion(Include.NON_NULL);
61 String payload = mapper.writer().writeValueAsString(activitySpec);
63 String urlString = UriBuilder.fromUri(hostname).path(ACTIVITY_SPEC_URI).build().toString();
64 URL url = new URL(urlString);
66 HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
67 httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
69 Response response = httpClient.post(payload);
71 int statusCode = response.getStatus();
72 if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
73 logger.warn(LoggingAnchor.THREE, "ActivitySpec", activitySpec.getName(), "already exists in SDC");
76 if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED) {
77 logger.warn(LoggingAnchor.THREE, "Error creating activity spec", activitySpec.getName(), statusCode);
81 if (response.getEntity() == null) {
82 logger.warn(LoggingAnchor.TWO, "No activity spec response returned", activitySpec.getName());
85 ActivitySpecCreateResponse activitySpecCreateResponse =
86 response.readEntity(ActivitySpecCreateResponse.class);
87 if (activitySpecCreateResponse == null) {
88 logger.warn(LoggingAnchor.TWO, "Unable to read activity spec", activitySpec.getName());
91 return activitySpecCreateResponse.getId();
94 } catch (Exception e) {
95 logger.warn(LoggingAnchor.TWO, "Exception creating activitySpec", e);
101 public boolean certifyActivitySpec(String hostname, String activitySpecId) {
102 if (activitySpecId == null) {
107 String path = ACTIVITY_SPEC_URI + "/" + activitySpecId + ACTIVITY_SPEC_URI_SUFFIX;
109 String urlString = UriBuilder.fromUri(hostname).path(path).build().toString();
110 URL url = new URL(urlString);
112 HttpClient httpClient = httpClientFactory.newJsonClient(url, ONAPComponents.SDC);
113 httpClient.addAdditionalHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
115 Response response = httpClient.put(CERTIFY_ACTIVITY_PAYLOAD);
117 int statusCode = response.getStatus();
119 if (statusCode == HttpStatus.SC_UNPROCESSABLE_ENTITY) {
120 logger.warn(LoggingAnchor.THREE, "ActivitySpec with id", activitySpecId, "is already certified in SDC");
123 if (statusCode != HttpStatus.SC_OK) {
124 logger.warn(LoggingAnchor.THREE, "Error certifying activity", activitySpecId, statusCode);
130 } catch (Exception e) {
131 logger.warn(LoggingAnchor.TWO, "Exception certifying activitySpec", e);