7aa06badb336f229151a3a1b43ec60f6629942d4
[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         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
247     }
248
249     /**
250      * Create AutomationCompositionElement to save in memory.
251      *
252      * @param element AcElementDeploy
253      * @return a new AutomationCompositionElement
254      */
255     public static AutomationCompositionElement createAutomationCompositionElement(AcElementDeploy element) {
256         var acElement = new AutomationCompositionElement();
257         acElement.setId(element.getId());
258         acElement.setDefinition(element.getDefinition());
259         acElement.setProperties(element.getProperties());
260         acElement.setSubState(SubState.NONE);
261         acElement.setLockState(LockState.LOCKED);
262         return acElement;
263     }
264
265     /**
266      * Create CompositionElementDto.
267      *
268      * @param compositionId the composition Id
269      * @param element AutomationComposition Element
270      * @param compositionInProperties composition definition InProperties
271      * @return the CompositionElementDto
272      */
273     public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
274             Map<String, Object> compositionInProperties) {
275         var compositionOutProperties = getAcElementsDefinitions()
276                 .get(compositionId).get(element.getDefinition()).getOutProperties();
277         return new CompositionElementDto(compositionId,
278                 element.getDefinition(), compositionInProperties, compositionOutProperties);
279     }
280
281     /**
282      * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
283      *
284      * @param automationComposition the AutomationComposition
285      * @param compositionId the compositionId
286      * @return the Map of CompositionElementDto
287      */
288     public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition,
289             UUID compositionId) {
290         Map<UUID, CompositionElementDto> map = new HashMap<>();
291         for (var element : automationComposition.getElements().values()) {
292             var compositionInProperties = getCommonProperties(compositionId, element.getDefinition());
293             var compositionElement = createCompositionElementDto(compositionId, element, compositionInProperties);
294             map.put(element.getId(), compositionElement);
295         }
296         return map;
297     }
298
299     public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
300         return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
301     }
302
303     /**
304      * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
305      *
306      * @param automationComposition the AutomationComposition
307      * @return the Map of InstanceElementDto
308      */
309     public Map<UUID, InstanceElementDto> getInstanceElementDtoMap(AutomationComposition automationComposition) {
310         Map<UUID, InstanceElementDto> map = new HashMap<>();
311         var serviceTemplateFragment = serviceTemplateFragmentMap.get(automationComposition.getCompositionId());
312         for (var element : automationComposition.getElements().values()) {
313             var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(),
314                     serviceTemplateFragment, element.getProperties(), element.getOutProperties());
315             map.put(element.getId(), instanceElement);
316         }
317         return map;
318     }
319
320     /**
321      * Create a new InstanceElementDto record with state New.
322      *
323      * @param instanceElement the InstanceElementDto
324      * @return a new InstanceElementDto
325      */
326     public static InstanceElementDto changeStateToNew(InstanceElementDto instanceElement) {
327         return new InstanceElementDto(instanceElement.instanceId(), instanceElement.elementId(),
328                 instanceElement.toscaServiceTemplateFragment(),
329                 instanceElement.inProperties(), instanceElement.outProperties(), ElementState.NEW);
330     }
331
332     /**
333      * Create a new CompositionElementDto record with state New.
334      *
335      * @param compositionElement the CompositionElementDto
336      * @return a new CompositionElementDto
337      */
338     public static CompositionElementDto changeStateToNew(CompositionElementDto compositionElement) {
339         return new CompositionElementDto(compositionElement.compositionId(), compositionElement.elementDefinitionId(),
340                 compositionElement.inProperties(), compositionElement.outProperties(), ElementState.NEW);
341     }
342 }