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.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;
50 public class CacheProvider {
53 private final UUID participantId;
57 private boolean registered = false;
60 private final UUID replicaId;
62 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
65 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
68 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
69 new ConcurrentHashMap<>();
72 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
75 private final Map<UUID, ToscaServiceTemplate> serviceTemplateFragmentMap = new ConcurrentHashMap<>();
80 * @param parameters the parameters of the participant
82 public CacheProvider(ParticipantParameters parameters) {
83 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
84 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
85 this.replicaId = UUID.randomUUID();
88 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
89 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
93 * Get AutomationComposition by id.
95 * @param automationCompositionId the AutomationComposition Id
96 * @return the AutomationComposition
98 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
99 return automationCompositions.get(automationCompositionId);
103 * Remove AutomationComposition.
105 * @param automationCompositionId the AutomationComposition Id
107 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
108 automationCompositions.remove(automationCompositionId);
112 * Add ElementDefinition.
114 * @param compositionId the composition Id
115 * @param list the list of AutomationCompositionElementDefinition to add
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);
122 acElementsDefinitions.put(compositionId, map);
125 public void removeElementDefinition(@NonNull UUID compositionId) {
126 acElementsDefinitions.remove(compositionId);
127 serviceTemplateFragmentMap.remove(compositionId);
131 * Get CommonProperties.
133 * @param instanceId the Automation Composition Id
134 * @param acElementId the Automation Composition Element Id
135 * @return the common Properties as Map
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();
145 * Get CommonProperties.
147 * @param compositionId the composition Id
148 * @param definition the AutomationCompositionElementDefinition Id
149 * @return the common Properties as Map
151 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
152 @NonNull ToscaConceptIdentifier definition) {
153 return acElementsDefinitions.get(compositionId).get(definition)
154 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
158 * Initialize an AutomationComposition from a ParticipantDeploy.
160 * @param compositionId the composition Id
161 * @param instanceId the Automation Composition Id
162 * @param participantDeploy the ParticipantDeploy
164 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
165 ParticipantDeploy participantDeploy) {
166 initializeAutomationComposition(compositionId, instanceId, participantDeploy,
167 DeployState.DEPLOYING, SubState.NONE);
171 * Initialize an AutomationComposition from a ParticipantDeploy.
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
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());
201 acElementMap.put(element.getId(), acElement);
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);
213 * Initialize an AutomationComposition from a ParticipantRestartAc.
215 * @param compositionId the composition Id
216 * @param participantRestartAc the ParticipantRestartAc
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())) {
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());
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);
252 * Create CompositionElementDto.
254 * @param compositionId the composition Id
255 * @param element AutomationComposition Element
256 * @param compositionInProperties composition definition InProperties
257 * @return the CompositionElementDto
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);
268 * Get a Map of CompositionElementDto by elementId from the elements of an AutomationComposition.
270 * @param automationComposition the AutomationComposition
271 * @param compositionId the compositionId
272 * @return the Map of CompositionElementDto
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);
285 public Map<UUID, CompositionElementDto> getCompositionElementDtoMap(AutomationComposition automationComposition) {
286 return getCompositionElementDtoMap(automationComposition, automationComposition.getCompositionId());
290 * Get a Map of InstanceElementDto by elementId from the elements of an AutomationComposition.
292 * @param automationComposition the AutomationComposition
293 * @return the Map of InstanceElementDto
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);