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 automationComposition.setStateChangeResult(participantRestartAc.getStateChangeResult());
247 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
251 * Create AutomationCompositionElement to save in memory.
253 * @param element AcElementDeploy
254 * @return a new AutomationCompositionElement
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);
267 * Create CompositionElementDto.
269 * @param compositionId the composition Id
270 * @param element AutomationComposition Element
271 * @param compositionInProperties composition definition InProperties
272 * @return the CompositionElementDto
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);
283 * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
285 * @param automationComposition the AutomationComposition
286 * @param compositionId the compositionId
287 * @return the Map of CompositionElementDto
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);
306 public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
307 return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
311 * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
313 * @param automationComposition the AutomationComposition
314 * @return the Map of InstanceElementDto
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);
328 * Create a new InstanceElementDto record with state New.
330 * @param instanceElement the InstanceElementDto
331 * @return a new InstanceElementDto
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);
340 * Create a new CompositionElementDto record with state New.
342 * @param compositionElement the CompositionElementDto
343 * @return a new CompositionElementDto
345 public static CompositionElementDto changeStateToNew(CompositionElementDto compositionElement) {
346 return new CompositionElementDto(compositionElement.compositionId(), compositionElement.elementDefinitionId(),
347 compositionElement.inProperties(), compositionElement.outProperties(), ElementState.NEW);