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