e2a5367f6a586f9679775d515e7b8a865608cf98
[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.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import java.util.concurrent.ConcurrentHashMap;
28 import javax.validation.Validation;
29 import javax.validation.ValidationException;
30 import lombok.AccessLevel;
31 import lombok.Getter;
32 import lombok.RequiredArgsConstructor;
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.AcTypeState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
42 import org.onap.policy.clamp.models.acm.concepts.DeployState;
43 import org.onap.policy.clamp.models.acm.concepts.LockState;
44 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
45 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.models.base.PfModelException;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.springframework.stereotype.Component;
53
54 /**
55  * This class handles implementation of automationCompositionElement updates.
56  */
57 @Component
58 @RequiredArgsConstructor
59 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
60
61     private static final Coder CODER = new StandardCoder();
62
63     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
64
65     private final ParticipantIntermediaryApi intermediaryApi;
66
67     private final AcA1PmsClient acA1PmsClient;
68
69     // Map of acElement Id and A1PMS services
70     @Getter(AccessLevel.PACKAGE)
71     private final Map<UUID, ConfigurationEntity> configRequestMap = new ConcurrentHashMap<>();
72
73     /**
74      * Handle a automation composition element state change.
75      *
76      * @param automationCompositionId the ID of the automation composition
77      * @param automationCompositionElementId the ID of the automation composition element
78      * @throws PfModelException in case of a model exception
79      */
80     @Override
81     public void undeploy(UUID automationCompositionId, UUID automationCompositionElementId)
82             throws A1PolicyServiceException {
83         var configurationEntity = configRequestMap.get(automationCompositionElementId);
84         if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
85             acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
86             configRequestMap.remove(automationCompositionElementId);
87             intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
88                     automationCompositionElementId, DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR,
89                     "Undeployed");
90         } else {
91             LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
92             throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE, "Unable to connect with A1PMS");
93         }
94     }
95
96     /**
97      * Callback method to handle an update on an automation composition element.
98      *
99      * @param automationCompositionId the ID of the automation composition
100      * @param element the information on the automation composition element
101      * @param properties properties Map
102      */
103     @Override
104     public void deploy(UUID automationCompositionId, AcElementDeploy element, Map<String, Object> properties)
105             throws A1PolicyServiceException {
106         try {
107             var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
108             var violations = Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
109             if (violations.isEmpty()) {
110                 if (acA1PmsClient.isPmsHealthy()) {
111                     acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
112                     configRequestMap.put(element.getId(), configurationEntity);
113
114                     intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
115                             DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
116                 } else {
117                     LOGGER.error("Failed to connect with A1PMS");
118                     throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
119                             "Unable to connect with A1PMS");
120                 }
121             } else {
122                 LOGGER.error("Violations found in the config request parameters: {}", violations);
123                 throw new ValidationException("Constraint violations in the config request");
124             }
125         } catch (ValidationException | CoderException | A1PolicyServiceException e) {
126             throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
127         }
128     }
129
130     @Override
131     public void lock(UUID instanceId, UUID elementId) throws PfModelException {
132         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
133                 StateChangeResult.NO_ERROR, "Locked");
134     }
135
136     @Override
137     public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
138         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
139                 StateChangeResult.NO_ERROR, "Unlocked");
140     }
141
142     @Override
143     public void delete(UUID instanceId, UUID elementId) throws PfModelException {
144         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
145                 StateChangeResult.NO_ERROR, "Deleted");
146     }
147
148     @Override
149     public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
150             throws PfModelException {
151         intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
152                 StateChangeResult.NO_ERROR, "Update not supported");
153     }
154
155     @Override
156     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
157             throws PfModelException {
158         intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
159     }
160
161     @Override
162     public void deprime(UUID compositionId) throws PfModelException {
163         intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
164                 "Deprimed");
165     }
166
167     @Override
168     public void handleRestartComposition(UUID compositionId,
169             List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
170             throws PfModelException {
171         var finalState = AcTypeState.PRIMED.equals(state) || AcTypeState.PRIMING.equals(state) ? AcTypeState.PRIMED
172                 : AcTypeState.COMMISSIONED;
173         intermediaryApi.updateCompositionState(compositionId, finalState, StateChangeResult.NO_ERROR, "Restarted");
174     }
175
176     @Override
177     public void handleRestartInstance(UUID automationCompositionId, AcElementDeploy element,
178             Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
179         if (DeployState.DEPLOYING.equals(deployState)) {
180             deploy(automationCompositionId, element, properties);
181             return;
182         }
183         if (DeployState.UNDEPLOYING.equals(deployState) || DeployState.DEPLOYED.equals(deployState)
184                 || DeployState.UPDATING.equals(deployState)) {
185             try {
186                 var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
187                 configRequestMap.put(element.getId(), configurationEntity);
188             } catch (ValidationException | CoderException e) {
189                 throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
190             }
191         }
192         if (DeployState.UNDEPLOYING.equals(deployState)) {
193             undeploy(automationCompositionId, element.getId());
194             return;
195         }
196         deployState = AcmUtils.deployCompleted(deployState);
197         lockState = AcmUtils.lockCompleted(deployState, lockState);
198         intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(), deployState,
199                 lockState, StateChangeResult.NO_ERROR, "Restarted");
200     }
201 }