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.parameters.ParticipantParameters;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
45 import org.springframework.stereotype.Component;
48 public class CacheProvider {
51 private final UUID participantId;
55 private boolean registered = false;
58 private final UUID replicaId;
60 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
63 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
66 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
67 new ConcurrentHashMap<>();
70 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
73 private final Map<UUID, ToscaServiceTemplate> serviceTemplateFragmentMap = new ConcurrentHashMap<>();
78 * @param parameters the parameters of the participant
80 public CacheProvider(ParticipantParameters parameters) {
81 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
82 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
83 this.replicaId = UUID.randomUUID();
86 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
87 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
91 * Get AutomationComposition by id.
93 * @param automationCompositionId the AutomationComposition Id
94 * @return the AutomationComposition
96 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
97 return automationCompositions.get(automationCompositionId);
101 * Remove AutomationComposition.
103 * @param automationCompositionId the AutomationComposition Id
105 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
106 automationCompositions.remove(automationCompositionId);
110 * Add ElementDefinition.
112 * @param compositionId the composition Id
113 * @param list the list of AutomationCompositionElementDefinition to add
115 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
116 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
117 for (var acElementDefinition : list) {
118 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
120 acElementsDefinitions.put(compositionId, map);
123 public void removeElementDefinition(@NonNull UUID compositionId) {
124 acElementsDefinitions.remove(compositionId);
125 serviceTemplateFragmentMap.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 map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
143 * Get CommonProperties.
145 * @param compositionId the composition Id
146 * @param definition the AutomationCompositionElementDefinition Id
147 * @return the common Properties as Map
149 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
150 @NonNull ToscaConceptIdentifier definition) {
151 return acElementsDefinitions.get(compositionId).get(definition)
152 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
156 * Initialize an AutomationComposition from a ParticipantDeploy.
158 * @param compositionId the composition Id
159 * @param instanceId the Automation Composition Id
160 * @param participantDeploy the ParticipantDeploy
162 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
163 ParticipantDeploy participantDeploy) {
164 var acLast = automationCompositions.get(instanceId);
165 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
166 for (var element : participantDeploy.getAcElementList()) {
167 var acElement = new AutomationCompositionElement();
168 acElement.setId(element.getId());
169 acElement.setParticipantId(getParticipantId());
170 acElement.setDefinition(element.getDefinition());
171 acElement.setDeployState(DeployState.DEPLOYING);
172 acElement.setLockState(LockState.NONE);
173 acElement.setProperties(element.getProperties());
174 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
175 if (acElementLast != null) {
176 acElement.setOutProperties(acElementLast.getOutProperties());
177 acElement.setOperationalState(acElementLast.getOperationalState());
178 acElement.setUseState(acElementLast.getUseState());
179 if (element.getToscaServiceTemplateFragment() != null) {
180 serviceTemplateFragmentMap.put(compositionId, element.getToscaServiceTemplateFragment());
183 acElementMap.put(element.getId(), acElement);
185 var automationComposition = new AutomationComposition();
186 automationComposition.setCompositionId(compositionId);
187 automationComposition.setInstanceId(instanceId);
188 automationComposition.setElements(acElementMap);
189 automationCompositions.put(instanceId, automationComposition);
193 * Initialize an AutomationComposition from a ParticipantRestartAc.
195 * @param compositionId the composition Id
196 * @param participantRestartAc the ParticipantRestartAc
198 public void initializeAutomationComposition(@NonNull UUID compositionId,
199 ParticipantRestartAc participantRestartAc) {
200 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
201 for (var element : participantRestartAc.getAcElementList()) {
202 if (!getParticipantId().equals(element.getParticipantId())) {
205 var acElement = new AutomationCompositionElement();
206 acElement.setId(element.getId());
207 acElement.setParticipantId(getParticipantId());
208 acElement.setDefinition(element.getDefinition());
209 acElement.setDeployState(element.getDeployState());
210 acElement.setLockState(element.getLockState());
211 acElement.setOperationalState(element.getOperationalState());
212 acElement.setUseState(element.getUseState());
213 acElement.setProperties(element.getProperties());
214 acElement.setOutProperties(element.getOutProperties());
215 acElementMap.put(element.getId(), acElement);
216 if (element.getToscaServiceTemplateFragment() != null) {
217 serviceTemplateFragmentMap.put(compositionId, element.getToscaServiceTemplateFragment());
221 var automationComposition = new AutomationComposition();
222 automationComposition.setCompositionId(compositionId);
223 automationComposition.setDeployState(participantRestartAc.getDeployState());
224 automationComposition.setLockState(participantRestartAc.getLockState());
225 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
226 automationComposition.setElements(acElementMap);
227 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
231 * Create CompositionElementDto.
233 * @param compositionId the composition Id
234 * @param element AutomationComposition Element
235 * @param compositionInProperties composition definition InProperties
236 * @return the CompositionElementDto
238 public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
239 Map<String, Object> compositionInProperties) {
240 var compositionOutProperties = getAcElementsDefinitions()
241 .get(compositionId).get(element.getDefinition()).getOutProperties();
242 return new CompositionElementDto(compositionId,
243 element.getDefinition(), compositionInProperties, compositionOutProperties);