5d4e1fe7ca475db3926d3ce0609a247692a8cc3c
[policy/clamp.git] /
1 /*-
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
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.intermediary.api.impl;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
42
43 /**
44  * Wrapper of AutomationCompositionElementListener.
45  * Valid since 7.1.0 release.
46  */
47 public abstract class AcElementListenerV1 implements AutomationCompositionElementListener {
48     protected final ParticipantIntermediaryApi intermediaryApi;
49
50     protected AcElementListenerV1(ParticipantIntermediaryApi intermediaryApi) {
51         this.intermediaryApi = intermediaryApi;
52     }
53
54     @Override
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);
65     }
66
67     public abstract void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
68         throws PfModelException;
69
70     @Override
71     public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
72         throws PfModelException {
73         undeploy(instanceElement.instanceId(), instanceElement.elementId());
74     }
75
76     public abstract void undeploy(UUID instanceId, UUID elementId) throws PfModelException;
77
78     @Override
79     public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
80         throws PfModelException {
81         lock(instanceElement.instanceId(), instanceElement.elementId());
82     }
83
84     public void lock(UUID instanceId, UUID elementId) throws PfModelException {
85         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
86             StateChangeResult.NO_ERROR, "Locked");
87     }
88
89     @Override
90     public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
91         throws PfModelException {
92         unlock(instanceElement.instanceId(), instanceElement.elementId());
93     }
94
95     public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
96         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.UNLOCKED,
97             StateChangeResult.NO_ERROR, "Unlocked");
98     }
99
100     @Override
101     public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
102         throws PfModelException {
103         delete(instanceElement.instanceId(), instanceElement.elementId());
104     }
105
106     public void delete(UUID instanceId, UUID elementId) throws PfModelException {
107         intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, DeployState.DELETED, null,
108             StateChangeResult.NO_ERROR, "Deleted");
109     }
110
111     @Override
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());
119     }
120
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");
125     }
126
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())));
132         }
133         return elementDefinitionList;
134     }
135
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;
146     }
147
148     @Override
149     public void prime(CompositionDto composition) throws PfModelException {
150         prime(composition.compositionId(), createAcElementDefinitionList(composition));
151     }
152
153     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
154         throws PfModelException {
155         intermediaryApi.updateCompositionState(compositionId, AcTypeState.PRIMED, StateChangeResult.NO_ERROR, "Primed");
156     }
157
158     @Override
159     public void deprime(CompositionDto composition) throws PfModelException {
160         deprime(composition.compositionId());
161     }
162
163     public void deprime(UUID compositionId) throws PfModelException {
164         intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
165             "Deprimed");
166     }
167
168     @Override
169     public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException {
170         handleRestartComposition(composition.compositionId(), createAcElementDefinitionList(composition), state);
171     }
172
173     /**
174      * Default implementation of handle Restart Composition.
175      *
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
180      */
181     public void handleRestartComposition(UUID compositionId,
182         List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state) throws PfModelException {
183         switch (state) {
184             case PRIMING -> prime(compositionId, elementDefinitionList);
185             case DEPRIMING -> deprime(compositionId);
186             default ->
187                 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
188         }
189     }
190
191     @Override
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);
201     }
202
203     /**
204      * Default implementation of handle Restart Instance.
205      *
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
212      */
213     public void handleRestartInstance(UUID instanceId, AcElementDeploy element,
214         Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
215
216         if (DeployState.DEPLOYING.equals(deployState)) {
217             deploy(instanceId, element, properties);
218             return;
219         }
220         if (DeployState.UNDEPLOYING.equals(deployState)) {
221             undeploy(instanceId, element.getId());
222             return;
223         }
224         if (DeployState.UPDATING.equals(deployState)) {
225             update(instanceId, element, properties);
226             return;
227         }
228         if (DeployState.DELETING.equals(deployState)) {
229             delete(instanceId, element.getId());
230             return;
231         }
232         if (LockState.LOCKING.equals(lockState)) {
233             lock(instanceId, element.getId());
234             return;
235         }
236         if (LockState.UNLOCKING.equals(lockState)) {
237             unlock(instanceId, element.getId());
238             return;
239         }
240         intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(),
241             deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
242     }
243
244     @Override
245     public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget,
246         InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate) throws PfModelException {
247         var element = new  AcElementDeploy();
248         element.setId(instanceElementMigrate.elementId());
249         element.setDefinition(compositionElementTarget.elementDefinitionId());
250         element.setProperties(instanceElementMigrate.inProperties());
251         migrate(instanceElementMigrate.instanceId(), element, compositionElementTarget.compositionId(),
252             element.getProperties());
253     }
254
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");
259     }
260 }