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.springframework.stereotype.Component;
47 public class CacheProvider {
50 private final UUID participantId;
54 private boolean registered = false;
57 private final UUID replicaId;
59 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
62 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
65 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
66 new ConcurrentHashMap<>();
69 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
74 * @param parameters the parameters of the participant
76 public CacheProvider(ParticipantParameters parameters) {
77 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
78 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
79 this.replicaId = UUID.randomUUID();
82 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
83 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
87 * Get AutomationComposition by id.
89 * @param automationCompositionId the AutomationComposition Id
90 * @return the AutomationComposition
92 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
93 return automationCompositions.get(automationCompositionId);
97 * Remove AutomationComposition.
99 * @param automationCompositionId the AutomationComposition Id
101 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
102 automationCompositions.remove(automationCompositionId);
106 * Add ElementDefinition.
108 * @param compositionId the composition Id
109 * @param list the list of AutomationCompositionElementDefinition to add
111 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
112 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
113 for (var acElementDefinition : list) {
114 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
116 acElementsDefinitions.put(compositionId, map);
119 public void removeElementDefinition(@NonNull UUID compositionId) {
120 acElementsDefinitions.remove(compositionId);
124 * Get CommonProperties.
126 * @param instanceId the Automation Composition Id
127 * @param acElementId the Automation Composition Element Id
128 * @return the common Properties as Map
130 public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
131 var automationComposition = automationCompositions.get(instanceId);
132 var map = acElementsDefinitions.get(automationComposition.getCompositionId());
133 var element = automationComposition.getElements().get(acElementId);
134 return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
138 * Get CommonProperties.
140 * @param compositionId the composition Id
141 * @param definition the AutomationCompositionElementDefinition Id
142 * @return the common Properties as Map
144 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
145 @NonNull ToscaConceptIdentifier definition) {
146 return acElementsDefinitions.get(compositionId).get(definition)
147 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
151 * Initialize an AutomationComposition from a ParticipantDeploy.
153 * @param compositionId the composition Id
154 * @param instanceId the Automation Composition Id
155 * @param participantDeploy the ParticipantDeploy
157 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
158 ParticipantDeploy participantDeploy) {
159 var acLast = automationCompositions.get(instanceId);
160 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
161 for (var element : participantDeploy.getAcElementList()) {
162 var acElement = new AutomationCompositionElement();
163 acElement.setId(element.getId());
164 acElement.setParticipantId(getParticipantId());
165 acElement.setDefinition(element.getDefinition());
166 acElement.setDeployState(DeployState.DEPLOYING);
167 acElement.setLockState(LockState.NONE);
168 acElement.setProperties(element.getProperties());
169 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
170 if (acElementLast != null) {
171 acElement.setOutProperties(acElementLast.getOutProperties());
172 acElement.setOperationalState(acElementLast.getOperationalState());
173 acElement.setUseState(acElementLast.getUseState());
175 acElementMap.put(element.getId(), acElement);
177 var automationComposition = new AutomationComposition();
178 automationComposition.setCompositionId(compositionId);
179 automationComposition.setInstanceId(instanceId);
180 automationComposition.setElements(acElementMap);
181 automationCompositions.put(instanceId, automationComposition);
185 * Initialize an AutomationComposition from a ParticipantRestartAc.
187 * @param compositionId the composition Id
188 * @param participantRestartAc the ParticipantRestartAc
190 public void initializeAutomationComposition(@NonNull UUID compositionId,
191 ParticipantRestartAc participantRestartAc) {
192 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
193 for (var element : participantRestartAc.getAcElementList()) {
194 if (!getParticipantId().equals(element.getParticipantId())) {
197 var acElement = new AutomationCompositionElement();
198 acElement.setId(element.getId());
199 acElement.setParticipantId(getParticipantId());
200 acElement.setDefinition(element.getDefinition());
201 acElement.setDeployState(element.getDeployState());
202 acElement.setLockState(element.getLockState());
203 acElement.setOperationalState(element.getOperationalState());
204 acElement.setUseState(element.getUseState());
205 acElement.setProperties(element.getProperties());
206 acElement.setOutProperties(element.getOutProperties());
207 acElementMap.put(element.getId(), acElement);
210 var automationComposition = new AutomationComposition();
211 automationComposition.setCompositionId(compositionId);
212 automationComposition.setDeployState(participantRestartAc.getDeployState());
213 automationComposition.setLockState(participantRestartAc.getLockState());
214 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
215 automationComposition.setElements(acElementMap);
216 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
220 * Create CompositionElementDto.
222 * @param compositionId the composition Id
223 * @param element AutomationComposition Element
224 * @param compositionInProperties composition definition InProperties
225 * @return the CompositionElementDto
227 public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
228 Map<String, Object> compositionInProperties) {
229 var compositionOutProperties = getAcElementsDefinitions()
230 .get(compositionId).get(element.getDefinition()).getOutProperties();
231 return new CompositionElementDto(compositionId,
232 element.getDefinition(), compositionInProperties, compositionOutProperties);