2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation.
4 * Modifications Copyright (C) 2022 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.distribution.forwarding.lifecycle.api;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.List;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.HttpHeaders;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.onap.policy.common.endpoints.http.client.HttpClient;
34 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.onap.policy.distribution.forwarding.PolicyForwarder;
38 import org.onap.policy.distribution.forwarding.PolicyForwardingException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the
46 * automationComposition design template to the life cycle api's of automationComposition components.
48 * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
50 public class LifecycleApiAutomationCompositionForwarder implements PolicyForwarder {
52 private static final String COMMISSION_AUTOMATION_COMPOSITION_URI = "/onap/acm/v2/commission";
53 private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleApiAutomationCompositionForwarder.class);
55 private LifecycleApiAutomationCompositionForwarderParameters forwarderParameters;
56 private HttpClient automationCompositionClient;
62 public void configure(final String parameterGroupName) throws HttpClientConfigException {
63 forwarderParameters = ParameterService.get(parameterGroupName);
65 automationCompositionClient = HttpClientFactoryInstance.getClientFactory().build(
66 forwarderParameters.getAutomationCompositionRuntimeParameters());
73 public void forward(final Collection<ToscaEntity> entities) throws PolicyForwardingException {
74 final List<ToscaEntity> failedEntities = new ArrayList<>();
75 for (final ToscaEntity entity : entities) {
76 forwardSingleEntity(failedEntities, entity);
78 if (!failedEntities.isEmpty()) {
79 throw new PolicyForwardingException(
80 "Failed forwarding the following entities: " + Arrays.toString(failedEntities.toArray()));
84 private void forwardSingleEntity(final List<ToscaEntity> failedEntities, final ToscaEntity entity) {
86 if (entity instanceof ToscaServiceTemplate) {
87 final var toscaServiceTemplate = (ToscaServiceTemplate) entity;
88 if (null != toscaServiceTemplate.getToscaTopologyTemplate()
89 && null != toscaServiceTemplate.getNodeTypes()
90 && null != toscaServiceTemplate.getDataTypes()) {
91 commissionAutomationComposition(toscaServiceTemplate);
94 throw new PolicyForwardingException("The entity is not of type ToscaServiceTemplate - " + entity);
96 } catch (final Exception exp) {
97 LOGGER.error(exp.getMessage(), exp);
98 failedEntities.add(entity);
102 private Response commissionAutomationComposition(final ToscaServiceTemplate toscaServiceTemplate)
103 throws PolicyForwardingException {
104 return invokeHttpClient(Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON),
105 COMMISSION_AUTOMATION_COMPOSITION_URI);
108 private Response invokeHttpClient(final Entity<?> entity, final String path)
109 throws PolicyForwardingException {
110 var response = automationCompositionClient.post(path, entity, Map.of(HttpHeaders.ACCEPT,
111 MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
112 if (response.getStatus() / 100 != 2) {
114 "Invocation of path {} failed for entity {}. Response status: {}, Response status info: {}",
115 path, entity, response.getStatus(), response.getStatusInfo());
116 throw new PolicyForwardingException("Failed creating the entity - " + entity);