c9ca5003f4722f5a15cc3b7f942f9b62f086e6c1
[policy/distribution.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.distribution.forwarding.lifecycle.api;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Map;
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;
41
42 /**
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.
45  *
46  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
47  */
48 public class LifecycleApiAutomationCompositionForwarder implements PolicyForwarder {
49
50     private static final String COMMISSION_AUTOMATION_COMPOSITION_URI = "/onap/acm/v2/commission";
51     private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleApiAutomationCompositionForwarder.class);
52
53     private HttpClient automationCompositionClient;
54
55     /**
56      * {@inheritDoc}.
57      */
58     @Override
59     public void configure(final String parameterGroupName) throws HttpClientConfigException {
60         LifecycleApiAutomationCompositionForwarderParameters forwarderParameters =
61             ParameterService.get(parameterGroupName);
62
63         automationCompositionClient = HttpClientFactoryInstance.getClientFactory().build(
64             forwarderParameters.getAutomationCompositionRuntimeParameters());
65     }
66
67     /**
68      * {@inheritDoc}.
69      */
70     @Override
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);
75         }
76         if (!failedEntities.isEmpty()) {
77             throw new PolicyForwardingException(
78                 "Failed forwarding the following entities: " + Arrays.toString(failedEntities.toArray()));
79         }
80     }
81
82     private void forwardSingleEntity(final List<ToscaEntity> failedEntities, final ToscaEntity entity) {
83         try {
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);
90                 }
91             } else {
92                 throw new PolicyForwardingException("The entity is not of type ToscaServiceTemplate - " + entity);
93             }
94         } catch (final Exception exp) {
95             LOGGER.error(exp.getMessage(), exp);
96             failedEntities.add(entity);
97         }
98     }
99
100     private void commissionAutomationComposition(final ToscaServiceTemplate toscaServiceTemplate)
101         throws PolicyForwardingException {
102         invokeHttpClient(Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON));
103     }
104
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);
115         }
116     }
117 }
118