9973e40227eab9a09ab5560cb5dc7f978fdeb61e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023-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.handler;
22
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
29 import lombok.Getter;
30 import lombok.NonNull;
31 import lombok.Setter;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.api.ElementState;
34 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
35 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
36 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
37 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
38 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
40 import org.onap.policy.clamp.models.acm.concepts.DeployState;
41 import org.onap.policy.clamp.models.acm.concepts.LockState;
42 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
43 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
44 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
45 import org.onap.policy.clamp.models.acm.concepts.SubState;
46 import org.onap.policy.models.base.PfUtils;
47 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaNodeTemplate;
49 import org.springframework.stereotype.Component;
50
51 @Component
52 public class CacheProvider {
53
54     @Getter
55     private final UUID participantId;
56
57     @Getter
58     @Setter
59     private boolean registered = false;
60
61     @Getter
62     private final UUID replicaId;
63
64     private final List<ParticipantSupportedElementType> supportedAcElementTypes;
65
66     @Getter
67     private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
68
69     @Getter
70     private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
71             new ConcurrentHashMap<>();
72
73     @Getter
74     private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
75
76     /**
77      * Constructor.
78      *
79      * @param parameters the parameters of the participant
80      */
81     public CacheProvider(ParticipantParameters parameters) {
82         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
83         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
84         this.replicaId = UUID.randomUUID();
85     }
86
87     public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
88         return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
89     }
90
91     /**
92      * Get AutomationComposition by id.
93      *
94      * @param automationCompositionId the AutomationComposition Id
95      * @return the AutomationComposition
96      */
97     public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
98         return automationCompositions.get(automationCompositionId);
99     }
100
101     /**
102      * Remove AutomationComposition.
103      *
104      * @param automationCompositionId the AutomationComposition Id
105      */
106     public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
107         automationCompositions.remove(automationCompositionId);
108     }
109
110     /**
111      * Add ElementDefinition.
112      *
113      * @param compositionId the composition Id
114      * @param list the list of AutomationCompositionElementDefinition to add
115      */
116     public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
117         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
118         for (var acElementDefinition : list) {
119             map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
120         }
121         acElementsDefinitions.put(compositionId, map);
122     }
123
124     public void removeElementDefinition(@NonNull UUID compositionId) {
125         acElementsDefinitions.remove(compositionId);
126     }
127
128     /**
129      * Get CommonProperties.
130      *
131      * @param instanceId the Automation Composition Id
132      * @param acElementId the Automation Composition Element Id
133      * @return the common Properties as Map
134      */
135     public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
136         var automationComposition = automationCompositions.get(instanceId);
137         var map = acElementsDefinitions.get(automationComposition.getCompositionId());
138         var element = automationComposition.getElements().get(acElementId);
139         return getAcElementDefinition(map, element.getDefinition())
140                 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
141     }
142
143     /**
144      * Get CommonProperties.
145      *
146      * @param compositionId the composition Id
147      * @param definition the AutomationCompositionElementDefinition Id
148      * @return the common Properties as Map
149      */
150     public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
151         @NonNull ToscaConceptIdentifier definition) {
152         return getAcElementDefinition(acElementsDefinitions.get(compositionId), definition)
153                 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
154     }
155
156     private AutomationCompositionElementDefinition getAcElementDefinition(
157             Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map,
158             ToscaConceptIdentifier definition) {
159         var nodeTemplate = map.get(definition);
160         if (nodeTemplate == null) {
161             nodeTemplate = new AutomationCompositionElementDefinition();
162             nodeTemplate.setAutomationCompositionElementToscaNodeTemplate(new ToscaNodeTemplate());
163             nodeTemplate.getAutomationCompositionElementToscaNodeTemplate().setProperties(new HashMap<>());
164         }
165         return nodeTemplate;
166     }
167
168     /**
169      * Initialize an AutomationComposition from a ParticipantDeploy.
170      *
171      * @param compositionId the composition Id
172      * @param instanceId the Automation Composition Id
173      * @param participantDeploy the ParticipantDeploy
174      */
175     public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
176             ParticipantDeploy participantDeploy) {
177         initializeAutomationComposition(compositionId, instanceId, participantDeploy,
178             DeployState.DEPLOYING, SubState.NONE);
179     }
180
181     /**
182      * Initialize an AutomationComposition from a ParticipantDeploy.
183      *
184      * @param compositionId the composition Id
185      * @param instanceId the Automation Composition Id
186      * @param participantDeploy the ParticipantDeploy
187      * @param deployState the DeployState
188      * @param subState the SubState
189      */
190     public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
191             ParticipantDeploy participantDeploy, DeployState deployState, SubState subState) {
192         var acLast = automationCompositions.get(instanceId);
193         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
194         for (var element : participantDeploy.getAcElementList()) {
195             var acElement = createAutomationCompositionElement(element);
196             acElement.setParticipantId(getParticipantId());
197             acElement.setDeployState(deployState);
198             acElement.setSubState(subState);
199             var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
200             if (acElementLast != null) {
201                 acElement.setOutProperties(acElementLast.getOutProperties());
202                 acElement.setOperationalState(acElementLast.getOperationalState());
203                 acElement.setUseState(acElementLast.getUseState());
204             }
205             acElementMap.put(element.getId(), acElement);
206         }
207         var automationComposition = new AutomationComposition();
208         automationComposition.setCompositionId(compositionId);
209         automationComposition.setInstanceId(instanceId);
210         automationComposition.setElements(acElementMap);
211         automationComposition.setDeployState(deployState);
212         automationComposition.setSubState(subState);
213         automationCompositions.put(instanceId, automationComposition);
214     }
215
216     /**
217      * Initialize an AutomationComposition from a ParticipantRestartAc.
218      *
219      * @param compositionId the composition Id
220      * @param participantRestartAc the ParticipantRestartAc
221      */
222     public void initializeAutomationComposition(@NonNull UUID compositionId,
223             ParticipantRestartAc participantRestartAc) {
224         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
225         for (var element : participantRestartAc.getAcElementList()) {
226             if (!getParticipantId().equals(element.getParticipantId())) {
227                 continue;
228             }
229             var acElement = new AutomationCompositionElement();
230             acElement.setId(element.getId());
231             acElement.setParticipantId(getParticipantId());
232             acElement.setDefinition(element.getDefinition());
233             acElement.setDeployState(element.getDeployState());
234             acElement.setLockState(element.getLockState());
235             acElement.setSubState(SubState.NONE);
236             acElement.setOperationalState(element.getOperationalState());
237             acElement.setUseState(element.getUseState());
238             acElement.setProperties(element.getProperties());
239             acElement.setOutProperties(element.getOutProperties());
240             acElementMap.put(element.getId(), acElement);
241         }
242
243         var automationComposition = new AutomationComposition();
244         automationComposition.setCompositionId(compositionId);
245         automationComposition.setDeployState(participantRestartAc.getDeployState());
246         automationComposition.setLockState(participantRestartAc.getLockState());
247         automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
248         automationComposition.setElements(acElementMap);
249         automationComposition.setStateChangeResult(participantRestartAc.getStateChangeResult());
250         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
251     }
252
253     /**
254      * Create AutomationCompositionElement to save in memory.
255      *
256      * @param element AcElementDeploy
257      * @return a new AutomationCompositionElement
258      */
259     public static AutomationCompositionElement createAutomationCompositionElement(AcElementDeploy element) {
260         var acElement = new AutomationCompositionElement();
261         acElement.setId(element.getId());
262         acElement.setDefinition(element.getDefinition());
263         acElement.setProperties(element.getProperties());
264         acElement.setSubState(SubState.NONE);
265         acElement.setLockState(LockState.LOCKED);
266         return acElement;
267     }
268
269     /**
270      * Create CompositionElementDto.
271      *
272      * @param compositionId the composition Id
273      * @param element AutomationComposition Element
274      * @param compositionInProperties composition definition InProperties
275      * @return the CompositionElementDto
276      */
277     public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
278             Map<String, Object> compositionInProperties) {
279         var compositionOutProperties = getAcElementDefinition(acElementsDefinitions
280                 .get(compositionId), element.getDefinition()).getOutProperties();
281         return new CompositionElementDto(compositionId,
282                 element.getDefinition(), compositionInProperties, compositionOutProperties);
283     }
284
285     /**
286      * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
287      *
288      * @param automationComposition the AutomationComposition
289      * @param compositionId the compositionId
290      * @return the Map of CompositionElementDto
291      */
292     public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition,
293             UUID compositionId) {
294         var definitions = acElementsDefinitions.get(compositionId);
295         Map<UUID, CompositionElementDto> map = new HashMap<>();
296         for (var element : automationComposition.getElements().values()) {
297             var definition = definitions.get(element.getDefinition());
298             var compositionElement = (definition != null)
299                     ? new CompositionElementDto(compositionId, element.getDefinition(),
300                             definition.getAutomationCompositionElementToscaNodeTemplate().getProperties(),
301                             definition.getOutProperties()) :
302                     new CompositionElementDto(compositionId, element.getDefinition(),
303                             Map.of(), Map.of(), ElementState.NOT_PRESENT);
304             map.put(element.getId(), compositionElement);
305         }
306         return map;
307     }
308
309     public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
310         return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
311     }
312
313     /**
314      * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
315      *
316      * @param automationComposition the AutomationComposition
317      * @return the Map of InstanceElementDto
318      */
319     public Map<UUID, InstanceElementDto> getInstanceElementDtoMap(AutomationComposition automationComposition) {
320         Map<UUID, InstanceElementDto> map = new HashMap<>();
321         for (var element : automationComposition.getElements().values()) {
322             var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(),
323                     element.getProperties(), element.getOutProperties());
324             map.put(element.getId(), instanceElement);
325         }
326         return map;
327     }
328
329     /**
330      * Create a new InstanceElementDto record with state New.
331      *
332      * @param instanceElement the InstanceElementDto
333      * @return a new InstanceElementDto
334      */
335     public static InstanceElementDto changeStateToNew(InstanceElementDto instanceElement) {
336         return new InstanceElementDto(instanceElement.instanceId(), instanceElement.elementId(),
337                 instanceElement.inProperties(), instanceElement.outProperties(), ElementState.NEW);
338     }
339
340     /**
341      * Create a new CompositionElementDto record with state New.
342      *
343      * @param compositionElement the CompositionElementDto
344      * @return a new CompositionElementDto
345      */
346     public static CompositionElementDto changeStateToNew(CompositionElementDto compositionElement) {
347         return new CompositionElementDto(compositionElement.compositionId(), compositionElement.elementDefinitionId(),
348                 compositionElement.inProperties(), compositionElement.outProperties(), ElementState.NEW);
349     }
350 }