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