2 * ============LICENSE_START=======================================================
3 * Copyright (C) 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.api.impl;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.UUID;
28 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
33 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
34 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
39 import org.onap.policy.models.base.PfModelException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
44 * Wrapper of AutomationCompositionElementListener.
45 * Valid since 7.1.0 release.
47 public abstract class AcElementListenerV1 implements AutomationCompositionElementListener {
48 protected final ParticipantIntermediaryApi intermediaryApi;
50 protected AcElementListenerV1(ParticipantIntermediaryApi intermediaryApi) {
51 this.intermediaryApi = intermediaryApi;
55 public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
56 throws PfModelException {
57 var element = new AcElementDeploy();
58 element.setId(instanceElement.elementId());
59 element.setDefinition(compositionElement.elementDefinitionId());
60 element.setToscaServiceTemplateFragment(instanceElement.toscaServiceTemplateFragment());
61 element.setProperties(instanceElement.inProperties());
62 Map<String, Object> properties = new HashMap<>(instanceElement.inProperties());
63 properties.putAll(compositionElement.inProperties());
64 deploy(instanceElement.instanceId(), element, properties);
67 public abstract void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
68 throws PfModelException;
71 public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
72 throws PfModelException {
73 undeploy(instanceElement.instanceId(), instanceElement.elementId());
76 public abstract void undeploy(UUID instanceId, UUID elementId) throws PfModelException;
79 public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
80 throws PfModelException {
81 lock(instanceElement.instanceId(), instanceElement.elementId());
84 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
85 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
86 StateChangeResult.NO_ERROR, "Locked");
90 public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
91 throws PfModelException {
92 unlock(instanceElement.instanceId(), instanceElement.elementId());
95 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
96 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
97 StateChangeResult.NO_ERROR, "Unlocked");
101 public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
102 throws PfModelException {
103 delete(instanceElement.instanceId(), instanceElement.elementId());
106 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
107 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
108 StateChangeResult.NO_ERROR, "Deleted");
112 public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
113 InstanceElementDto instanceElementUpdated) throws PfModelException {
114 var element = new AcElementDeploy();
115 element.setId(instanceElementUpdated.elementId());
116 element.setDefinition(compositionElement.elementDefinitionId());
117 element.setProperties(instanceElementUpdated.inProperties());
118 update(instanceElementUpdated.instanceId(), element, element.getProperties());
121 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
122 throws PfModelException {
123 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
124 StateChangeResult.NO_ERROR, "Update not supported");
127 private List<AutomationCompositionElementDefinition> createAcElementDefinitionList(CompositionDto composition) {
128 List<AutomationCompositionElementDefinition> elementDefinitionList = new ArrayList<>();
129 for (var entry : composition.inPropertiesMap().entrySet()) {
130 elementDefinitionList.add(createAcElementDefinition(entry.getKey(), entry.getValue(),
131 composition.outPropertiesMap().get(entry.getKey())));
133 return elementDefinitionList;
136 private AutomationCompositionElementDefinition createAcElementDefinition(
137 ToscaConceptIdentifier toscaConceptIdentifier, Map<String, Object> property,
138 Map<String, Object> outProperties) {
139 var acElementDefinition = new AutomationCompositionElementDefinition();
140 acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier);
141 var toscaNodeTemplate = new ToscaNodeTemplate();
142 toscaNodeTemplate.setProperties(property);
143 acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(toscaNodeTemplate);
144 acElementDefinition.setOutProperties(outProperties);
145 return acElementDefinition;
149 public void prime(CompositionDto composition) throws PfModelException {
150 prime(composition.compositionId(), createAcElementDefinitionList(composition));
153 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
154 throws PfModelException {
155 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
159 public void deprime(CompositionDto composition) throws PfModelException {
160 deprime(composition.compositionId());
163 public void deprime(UUID compositionId) throws PfModelException {
164 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
169 public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException {
170 handleRestartComposition(composition.compositionId(), createAcElementDefinitionList(composition), state);
174 * Default implementation of handle Restart Composition.
176 * @param compositionId the composition Id
177 * @param elementDefinitionList the list of AutomationCompositionElementDefinition
178 * @param state the current AcTypeState
179 * @throws PfModelException in case of a model exception
181 public void handleRestartComposition(UUID compositionId,
182 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state) throws PfModelException {
184 case PRIMING -> prime(compositionId, elementDefinitionList);
185 case DEPRIMING -> deprime(compositionId);
187 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
192 public void handleRestartInstance(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
193 DeployState deployState, LockState lockState) throws PfModelException {
194 var element = new AcElementDeploy();
195 element.setId(instanceElement.elementId());
196 element.setDefinition(compositionElement.elementDefinitionId());
197 element.setProperties(instanceElement.inProperties());
198 Map<String, Object> properties = new HashMap<>(instanceElement.inProperties());
199 properties.putAll(compositionElement.inProperties());
200 handleRestartInstance(instanceElement.instanceId(), element, properties, deployState, lockState);
204 * Default implementation of handle Restart Instance.
206 * @param instanceId the instance Id
207 * @param element the AcElementDeploy
208 * @param properties the in properties
209 * @param deployState the current deployState
210 * @param lockState the current lockState
211 * @throws PfModelException in case of a model exception
213 public void handleRestartInstance(UUID instanceId, AcElementDeploy element,
214 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
216 if (DeployState.DEPLOYING.equals(deployState)) {
217 deploy(instanceId, element, properties);
220 if (DeployState.UNDEPLOYING.equals(deployState)) {
221 undeploy(instanceId, element.getId());
224 if (DeployState.UPDATING.equals(deployState)) {
225 update(instanceId, element, properties);
228 if (DeployState.DELETING.equals(deployState)) {
229 delete(instanceId, element.getId());
232 if (LockState.LOCKING.equals(lockState)) {
233 lock(instanceId, element.getId());
236 if (LockState.UNLOCKING.equals(lockState)) {
237 unlock(instanceId, element.getId());
240 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(),
241 deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
245 public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget,
246 InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate) throws PfModelException {
247 var element = new AcElementDeploy();
248 element.setId(instanceElement.elementId());
249 element.setDefinition(compositionElement.elementDefinitionId());
250 element.setProperties(instanceElement.inProperties());
251 migrate(instanceElementMigrate.instanceId(), element, compositionElementTarget.compositionId(),
252 element.getProperties());
255 public void migrate(UUID instanceId, AcElementDeploy element, UUID compositionTargetId,
256 Map<String, Object> properties) throws PfModelException {
257 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(),
258 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");