8401b934b8639e512aa1d27dc21e4b83eb8a2386
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 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.clamp.acm.participant.a1pms.handler;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27 import javax.validation.Validation;
28 import javax.validation.ValidationException;
29 import lombok.AccessLevel;
30 import lombok.Getter;
31 import lombok.RequiredArgsConstructor;
32 import lombok.Setter;
33 import org.apache.http.HttpStatus;
34 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
35 import org.onap.policy.clamp.acm.participant.a1pms.models.ConfigurationEntity;
36 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
38 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
39 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.common.utils.coder.Coder;
42 import org.onap.policy.common.utils.coder.CoderException;
43 import org.onap.policy.common.utils.coder.StandardCoder;
44 import org.onap.policy.models.base.PfModelException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
48
49 /**
50  * This class handles implementation of automationCompositionElement updates.
51  */
52 @Component
53 @RequiredArgsConstructor
54 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
55
56     private static final Coder CODER = new StandardCoder();
57
58     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
59
60     @Setter
61     private ParticipantIntermediaryApi intermediaryApi;
62
63     private final AcA1PmsClient acA1PmsClient;
64
65     // Map of acElement Id and A1PMS services
66     @Getter(AccessLevel.PACKAGE)
67     private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
68
69     /**
70      * Handle a automation composition element state change.
71      *
72      * @param automationCompositionId the ID of the automation composition
73      * @param automationCompositionElementId the ID of the automation composition element
74      * @throws PfModelException in case of a model exception
75      */
76     @Override
77     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId)
78             throws A1PolicyServiceException {
79         var configurationEntity = configRequestMap.get(automationCompositionElementId);
80         if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
81             acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
82             configRequestMap.remove(automationCompositionElementId);
83             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
84                     automationCompositionElementId, DeployState.UNDEPLOYED, null, "Undeployed");
85         } else {
86             LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
87             throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
88         }
89     }
90
91     /**
92      * Callback method to handle an update on an automation composition element.
93      *
94      * @param automationCompositionId the ID of the automation composition
95      * @param element the information on the automation composition element
96      * @param properties properties Map
97      */
98     @Override
99     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
100             throws A1PolicyServiceException {
101         try {
102             var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
103             var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
104             if (violations.isEmpty()) {
105                 if (acA1PmsClient.isPmsHealthy()) {
106                     acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
107                     configRequestMap.put(element.getId(), configurationEntity);
108
109                     intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
110                             DeployState.DEPLOYED, null, "Deployed");
111                 } else {
112                     LOGGER.error("Failed to connect with A1PMS");
113                     throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
114                             "Unable to connect with A1PMS");
115                 }
116             } else {
117                 LOGGER.error("Violations found in the config request parameters: {}", violations);
118                 throw new ValidationException("Constraint violations in the config request");
119             }
120         } catch (ValidationException | CoderException | A1PolicyServiceException e) {
121             throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
122         }
123     }
124 }