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