2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024-2025 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 jakarta.ws.rs.core.Response;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
28 import java.util.UUID;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
34 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
35 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
40 import org.onap.policy.models.base.PfModelException;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
45 * Wrapper of AutomationCompositionElementListener.
46 * Valid since 7.1.0 release.
48 public abstract class AcElementListenerV1 extends AcElementListenerV2
49 implements AutomationCompositionElementListener, AutomationCompositionElementListenerV1 {
51 protected AcElementListenerV1(ParticipantIntermediaryApi intermediaryApi) {
52 super(intermediaryApi);
56 public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
57 throws PfModelException {
58 var element = new AcElementDeploy();
59 element.setId(instanceElement.elementId());
60 element.setDefinition(compositionElement.elementDefinitionId());
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);
68 public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
69 throws PfModelException {
70 undeploy(instanceElement.instanceId(), instanceElement.elementId());
74 public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
75 throws PfModelException {
76 lock(instanceElement.instanceId(), instanceElement.elementId());
80 public void lock(UUID instanceId, UUID elementId) throws PfModelException {
81 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
82 StateChangeResult.NO_ERROR, "Locked");
86 public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
87 throws PfModelException {
88 unlock(instanceElement.instanceId(), instanceElement.elementId());
92 public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
93 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
94 StateChangeResult.NO_ERROR, "Unlocked");
98 public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
99 throws PfModelException {
100 delete(instanceElement.instanceId(), instanceElement.elementId());
104 public void delete(UUID instanceId, UUID elementId) throws PfModelException {
105 intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
106 StateChangeResult.NO_ERROR, "Deleted");
110 public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
111 InstanceElementDto instanceElementUpdated) throws PfModelException {
112 var element = new AcElementDeploy();
113 element.setId(instanceElementUpdated.elementId());
114 element.setDefinition(compositionElement.elementDefinitionId());
115 element.setProperties(instanceElementUpdated.inProperties());
116 update(instanceElementUpdated.instanceId(), element, element.getProperties());
119 public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
120 throws PfModelException {
121 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(), DeployState.DEPLOYED, null,
122 StateChangeResult.NO_ERROR, "Update not supported");
125 private List<AutomationCompositionElementDefinition> createAcElementDefinitionList(CompositionDto composition) {
126 List<AutomationCompositionElementDefinition> elementDefinitionList = new ArrayList<>();
127 for (var entry : composition.inPropertiesMap().entrySet()) {
128 elementDefinitionList.add(createAcElementDefinition(entry.getKey(), entry.getValue(),
129 composition.outPropertiesMap().get(entry.getKey())));
131 return elementDefinitionList;
134 private AutomationCompositionElementDefinition createAcElementDefinition(
135 ToscaConceptIdentifier toscaConceptIdentifier, Map<String, Object> property,
136 Map<String, Object> outProperties) {
137 var acElementDefinition = new AutomationCompositionElementDefinition();
138 acElementDefinition.setAcElementDefinitionId(toscaConceptIdentifier);
139 var toscaNodeTemplate = new ToscaNodeTemplate();
140 toscaNodeTemplate.setProperties(property);
141 acElementDefinition.setAutomationCompositionElementToscaNodeTemplate(toscaNodeTemplate);
142 acElementDefinition.setOutProperties(outProperties);
143 return acElementDefinition;
147 public void prime(CompositionDto composition) throws PfModelException {
148 prime(composition.compositionId(), createAcElementDefinitionList(composition));
152 public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
153 throws PfModelException {
154 intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
158 public void deprime(CompositionDto composition) throws PfModelException {
159 deprime(composition.compositionId());
163 public void deprime(UUID compositionId) throws PfModelException {
164 intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
169 * Default implementation of handle Restart Composition.
171 * @param compositionId the composition Id
172 * @param elementDefinitionList the list of AutomationCompositionElementDefinition
173 * @param state the current AcTypeState
174 * @throws PfModelException in case of a model exception
176 public void handleRestartComposition(UUID compositionId,
177 List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state) throws PfModelException {
178 throw new PfModelException(Response.Status.BAD_REQUEST, NOT_SUPPORTED);
182 * Default implementation of handle Restart Instance.
184 * @param instanceId the instance Id
185 * @param element the AcElementDeploy
186 * @param properties the in properties
187 * @param deployState the current deployState
188 * @param lockState the current lockState
189 * @throws PfModelException in case of a model exception
191 public void handleRestartInstance(UUID instanceId, AcElementDeploy element,
192 Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
193 throw new PfModelException(Response.Status.BAD_REQUEST, NOT_SUPPORTED);
198 public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget,
199 InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate,
200 int stage) throws PfModelException {
201 var element = new AcElementDeploy();
202 element.setId(instanceElementMigrate.elementId());
203 element.setDefinition(compositionElementTarget.elementDefinitionId());
204 element.setProperties(instanceElementMigrate.inProperties());
205 migrate(instanceElementMigrate.instanceId(), element, compositionElementTarget.compositionId(),
206 element.getProperties());
210 public void migrate(UUID instanceId, AcElementDeploy element, UUID compositionTargetId,
211 Map<String, Object> properties) throws PfModelException {
212 intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(),
213 DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");