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