605c68cdd80a3b7a6cee249df68d7a06cf5fbe89
[policy/distribution.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.distribution.forwarding.lifecycle.api;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Map;
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;
43
44 /**
45  * This class provides an implementation of {@link PolicyForwarder} interface for forwarding the
46  * controlloop design template to the life cycle api's of controlloop components.
47  *
48  * @author Sirisha Manchikanti (sirisha.manchikanti@est.tech)
49  */
50 public class LifecycleApiControlLoopForwarder implements PolicyForwarder {
51
52     private static final String COMMISSION_CONTROLLOOP_URI = "/onap/controlloop/v2/commission";
53     private static final Logger LOGGER = LoggerFactory.getLogger(LifecycleApiControlLoopForwarder.class);
54
55     private LifecycleApiControlLoopForwarderParameters forwarderParameters;
56     private HttpClient controlLoopClient;
57
58     /**
59      * {@inheritDoc}.
60      */
61     @Override
62     public void configure(final String parameterGroupName) throws HttpClientConfigException {
63         forwarderParameters = ParameterService.get(parameterGroupName);
64
65         controlLoopClient = HttpClientFactoryInstance.getClientFactory().build(
66                 forwarderParameters.getControlLoopRuntimeParameters());
67     }
68
69     /**
70      * {@inheritDoc}.
71      */
72     @Override
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);
77         }
78         if (!failedEntities.isEmpty()) {
79             throw new PolicyForwardingException(
80                     "Failed forwarding the following entities: " + Arrays.toString(failedEntities.toArray()));
81         }
82     }
83
84     private void forwardSingleEntity(final List<ToscaEntity> failedEntities, final ToscaEntity entity) {
85         try {
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                     commissionControlLoop(toscaServiceTemplate);
92                 }
93             } else {
94                 throw new PolicyForwardingException("The entity is not of type ToscaServiceTemplate - " + entity);
95             }
96         } catch (final Exception exp) {
97             LOGGER.error(exp.getMessage(), exp);
98             failedEntities.add(entity);
99         }
100     }
101
102     private Response commissionControlLoop(final ToscaServiceTemplate toscaServiceTemplate)
103             throws PolicyForwardingException {
104         return invokeHttpClient(Entity.entity(toscaServiceTemplate, MediaType.APPLICATION_JSON),
105                 COMMISSION_CONTROLLOOP_URI);
106     }
107
108     private Response invokeHttpClient(final Entity<?> entity, final String path)
109             throws PolicyForwardingException {
110         var response = controlLoopClient.post(path, entity, Map.of(HttpHeaders.ACCEPT,
111                         MediaType.APPLICATION_JSON, HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON));
112         if (response.getStatus() / 100 != 2) {
113             LOGGER.error(
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);
117         }
118         return response;
119     }
120 }
121