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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
30 import lombok.NonNull;
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;
52 public class CacheProvider {
55 private final UUID participantId;
59 private boolean registered = false;
62 private final UUID replicaId;
64 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
67 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
70 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
71 new ConcurrentHashMap<>();
74 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
77 private final Map<UUID, ToscaServiceTemplate> serviceTemplateFragmentMap = new ConcurrentHashMap<>();
82 * @param parameters the parameters of the participant
84 public CacheProvider(ParticipantParameters parameters) {
85 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
86 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
87 this.replicaId = UUID.randomUUID();
90 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
91 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
95 * Get AutomationComposition by id.
97 * @param automationCompositionId the AutomationComposition Id
98 * @return the AutomationComposition
100 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
101 return automationCompositions.get(automationCompositionId);
105 * Remove AutomationComposition.
107 * @param automationCompositionId the AutomationComposition Id
109 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
110 automationCompositions.remove(automationCompositionId);
114 * Add ElementDefinition.
116 * @param compositionId the composition Id
117 * @param list the list of AutomationCompositionElementDefinition to add
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);
124 acElementsDefinitions.put(compositionId, map);
127 public void removeElementDefinition(@NonNull UUID compositionId) {
128 acElementsDefinitions.remove(compositionId);
129 serviceTemplateFragmentMap.remove(compositionId);
133 * Get CommonProperties.
135 * @param instanceId the Automation Composition Id
136 * @param acElementId the Automation Composition Element Id
137 * @return the common Properties as Map
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();
147 * Get CommonProperties.
149 * @param compositionId the composition Id
150 * @param definition the AutomationCompositionElementDefinition Id
151 * @return the common Properties as Map
153 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
154 @NonNull ToscaConceptIdentifier definition) {
155 return acElementsDefinitions.get(compositionId).get(definition)
156 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
160 * Initialize an AutomationComposition from a ParticipantDeploy.
162 * @param compositionId the composition Id
163 * @param instanceId the Automation Composition Id
164 * @param participantDeploy the ParticipantDeploy
166 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
167 ParticipantDeploy participantDeploy) {
168 initializeAutomationComposition(compositionId, instanceId, participantDeploy,
169 DeployState.DEPLOYING, SubState.NONE);
173 * Initialize an AutomationComposition from a ParticipantDeploy.
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
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());
199 acElementMap.put(element.getId(), acElement);
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);
211 * Initialize an AutomationComposition from a ParticipantRestartAc.
213 * @param compositionId the composition Id
214 * @param participantRestartAc the ParticipantRestartAc
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())) {
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());
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);
250 * Create AutomationCompositionElement to save in memory.
252 * @param element AcElementDeploy
253 * @return a new AutomationCompositionElement
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);
266 * Create CompositionElementDto.
268 * @param compositionId the composition Id
269 * @param element AutomationComposition Element
270 * @param compositionInProperties composition definition InProperties
271 * @return the CompositionElementDto
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);
282 * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
284 * @param automationComposition the AutomationComposition
285 * @param compositionId the compositionId
286 * @return the Map of CompositionElementDto
288 public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition,
289 UUID compositionId) {
290 var definitions = acElementsDefinitions.get(compositionId);
291 Map<UUID, CompositionElementDto> map = new HashMap<>();
292 for (var element : automationComposition.getElements().values()) {
293 var definition = definitions.get(element.getDefinition());
294 var compositionElement = (definition != null)
295 ? new CompositionElementDto(compositionId, element.getDefinition(),
296 definition.getAutomationCompositionElementToscaNodeTemplate().getProperties(),
297 definition.getOutProperties()) :
298 new CompositionElementDto(compositionId, element.getDefinition(),
299 Map.of(), Map.of(), ElementState.NOT_PRESENT);
300 map.put(element.getId(), compositionElement);
305 public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
306 return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
310 * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
312 * @param automationComposition the AutomationComposition
313 * @return the Map of InstanceElementDto
315 public Map<UUID, InstanceElementDto> getInstanceElementDtoMap(AutomationComposition automationComposition) {
316 Map<UUID, InstanceElementDto> map = new HashMap<>();
317 var serviceTemplateFragment = serviceTemplateFragmentMap.get(automationComposition.getCompositionId());
318 for (var element : automationComposition.getElements().values()) {
319 var instanceElement = new InstanceElementDto(automationComposition.getInstanceId(), element.getId(),
320 serviceTemplateFragment, element.getProperties(), element.getOutProperties());
321 map.put(element.getId(), instanceElement);
327 * Create a new InstanceElementDto record with state New.
329 * @param instanceElement the InstanceElementDto
330 * @return a new InstanceElementDto
332 public static InstanceElementDto changeStateToNew(InstanceElementDto instanceElement) {
333 return new InstanceElementDto(instanceElement.instanceId(), instanceElement.elementId(),
334 instanceElement.toscaServiceTemplateFragment(),
335 instanceElement.inProperties(), instanceElement.outProperties(), ElementState.NEW);
339 * Create a new CompositionElementDto record with state New.
341 * @param compositionElement the CompositionElementDto
342 * @return a new CompositionElementDto
344 public static CompositionElementDto changeStateToNew(CompositionElementDto compositionElement) {
345 return new CompositionElementDto(compositionElement.compositionId(), compositionElement.elementDefinitionId(),
346 compositionElement.inProperties(), compositionElement.outProperties(), ElementState.NEW);