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.ToscaNodeTemplate;
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<>();
79 * @param parameters the parameters of the participant
81 public CacheProvider(ParticipantParameters parameters) {
82 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
83 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
84 this.replicaId = UUID.randomUUID();
87 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
88 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
92 * Get AutomationComposition by id.
94 * @param automationCompositionId the AutomationComposition Id
95 * @return the AutomationComposition
97 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
98 return automationCompositions.get(automationCompositionId);
102 * Remove AutomationComposition.
104 * @param automationCompositionId the AutomationComposition Id
106 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
107 automationCompositions.remove(automationCompositionId);
111 * Add ElementDefinition.
113 * @param compositionId the composition Id
114 * @param list the list of AutomationCompositionElementDefinition to add
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);
121 acElementsDefinitions.put(compositionId, map);
124 public void removeElementDefinition(@NonNull UUID compositionId) {
125 acElementsDefinitions.remove(compositionId);
129 * Get CommonProperties.
131 * @param instanceId the Automation Composition Id
132 * @param acElementId the Automation Composition Element Id
133 * @return the common Properties as Map
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();
144 * Get CommonProperties.
146 * @param compositionId the composition Id
147 * @param definition the AutomationCompositionElementDefinition Id
148 * @return the common Properties as Map
150 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
151 @NonNull ToscaConceptIdentifier definition) {
152 return getAcElementDefinition(acElementsDefinitions.get(compositionId), definition)
153 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
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<>());
169 * Initialize an AutomationComposition from a ParticipantDeploy.
171 * @param compositionId the composition Id
172 * @param instanceId the Automation Composition Id
173 * @param participantDeploy the ParticipantDeploy
175 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
176 ParticipantDeploy participantDeploy) {
177 initializeAutomationComposition(compositionId, instanceId, participantDeploy,
178 DeployState.DEPLOYING, SubState.NONE);
182 * Initialize an AutomationComposition from a ParticipantDeploy.
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
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());
205 acElementMap.put(element.getId(), acElement);
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);
217 * Initialize an AutomationComposition from a ParticipantRestartAc.
219 * @param compositionId the composition Id
220 * @param participantRestartAc the ParticipantRestartAc
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())) {
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);
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);
254 * Create AutomationCompositionElement to save in memory.
256 * @param element AcElementDeploy
257 * @return a new AutomationCompositionElement
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);
270 * Create CompositionElementDto.
272 * @param compositionId the composition Id
273 * @param element AutomationComposition Element
274 * @param compositionInProperties composition definition InProperties
275 * @return the CompositionElementDto
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);
286 * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
288 * @param automationComposition the AutomationComposition
289 * @param compositionId the compositionId
290 * @return the Map of CompositionElementDto
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);
309 public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
310 return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
314 * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
316 * @param automationComposition the AutomationComposition
317 * @return the Map of InstanceElementDto
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);
330 * Create a new InstanceElementDto record with state New.
332 * @param instanceElement the InstanceElementDto
333 * @return a new InstanceElementDto
335 public static InstanceElementDto changeStateToNew(InstanceElementDto instanceElement) {
336 return new InstanceElementDto(instanceElement.instanceId(), instanceElement.elementId(),
337 instanceElement.inProperties(), instanceElement.outProperties(), ElementState.NEW);
341 * Create a new CompositionElementDto record with state New.
343 * @param compositionElement the CompositionElementDto
344 * @return a new CompositionElementDto
346 public static CompositionElementDto changeStateToNew(CompositionElementDto compositionElement) {
347 return new CompositionElementDto(compositionElement.compositionId(), compositionElement.elementDefinitionId(),
348 compositionElement.inProperties(), compositionElement.outProperties(), ElementState.NEW);