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;
56 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
59 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
62 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
63 new ConcurrentHashMap<>();
66 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
71 * @param parameters the parameters of the participant
73 public CacheProvider(ParticipantParameters parameters) {
74 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
75 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
78 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
79 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
83 * Get AutomationComposition by id.
85 * @param automationCompositionId the AutomationComposition Id
86 * @return the AutomationComposition
88 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
89 return automationCompositions.get(automationCompositionId);
93 * Remove AutomationComposition.
95 * @param automationCompositionId the AutomationComposition Id
97 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
98 automationCompositions.remove(automationCompositionId);
102 * Add ElementDefinition.
104 * @param compositionId the composition Id
105 * @param list the list of AutomationCompositionElementDefinition to add
107 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
108 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
109 for (var acElementDefinition : list) {
110 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
112 acElementsDefinitions.put(compositionId, map);
115 public void removeElementDefinition(@NonNull UUID compositionId) {
116 acElementsDefinitions.remove(compositionId);
120 * Get CommonProperties.
122 * @param instanceId the Automation Composition Id
123 * @param acElementId the Automation Composition Element Id
124 * @return the common Properties as Map
126 public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
127 var automationComposition = automationCompositions.get(instanceId);
128 var map = acElementsDefinitions.get(automationComposition.getCompositionId());
129 var element = automationComposition.getElements().get(acElementId);
130 return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
134 * Get CommonProperties.
136 * @param compositionId the composition Id
137 * @param definition the AutomationCompositionElementDefinition Id
138 * @return the common Properties as Map
140 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
141 @NonNull ToscaConceptIdentifier definition) {
142 return acElementsDefinitions.get(compositionId).get(definition)
143 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
147 * Initialize an AutomationComposition from a ParticipantDeploy.
149 * @param compositionId the composition Id
150 * @param instanceId the Automation Composition Id
151 * @param participantDeploy the ParticipantDeploy
153 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
154 ParticipantDeploy participantDeploy) {
155 var acLast = automationCompositions.get(instanceId);
156 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
157 for (var element : participantDeploy.getAcElementList()) {
158 var acElement = new AutomationCompositionElement();
159 acElement.setId(element.getId());
160 acElement.setParticipantId(getParticipantId());
161 acElement.setDefinition(element.getDefinition());
162 acElement.setDeployState(DeployState.DEPLOYING);
163 acElement.setLockState(LockState.NONE);
164 acElement.setProperties(element.getProperties());
165 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
166 if (acElementLast != null) {
167 acElement.setOutProperties(acElementLast.getOutProperties());
168 acElement.setOperationalState(acElementLast.getOperationalState());
169 acElement.setUseState(acElementLast.getUseState());
171 acElementMap.put(element.getId(), acElement);
173 var automationComposition = new AutomationComposition();
174 automationComposition.setCompositionId(compositionId);
175 automationComposition.setInstanceId(instanceId);
176 automationComposition.setElements(acElementMap);
177 automationCompositions.put(instanceId, automationComposition);
181 * Initialize an AutomationComposition from a ParticipantRestartAc.
183 * @param compositionId the composition Id
184 * @param participantRestartAc the ParticipantRestartAc
186 public void initializeAutomationComposition(@NonNull UUID compositionId,
187 ParticipantRestartAc participantRestartAc) {
188 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
189 for (var element : participantRestartAc.getAcElementList()) {
190 var acElement = new AutomationCompositionElement();
191 acElement.setId(element.getId());
192 acElement.setParticipantId(getParticipantId());
193 acElement.setDefinition(element.getDefinition());
194 acElement.setDeployState(element.getDeployState());
195 acElement.setLockState(element.getLockState());
196 acElement.setOperationalState(element.getOperationalState());
197 acElement.setUseState(element.getUseState());
198 acElement.setProperties(element.getProperties());
199 acElement.setOutProperties(element.getOutProperties());
200 acElement.setRestarting(true);
201 acElementMap.put(element.getId(), acElement);
204 var automationComposition = new AutomationComposition();
205 automationComposition.setCompositionId(compositionId);
206 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
207 automationComposition.setElements(acElementMap);
208 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
212 * Create CompositionElementDto.
214 * @param compositionId the composition Id
215 * @param element AutomationComposition Element
216 * @param compositionInProperties composition definition InProperties
217 * @return the CompositionElementDto
219 public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
220 Map<String, Object> compositionInProperties) {
221 var compositionOutProperties = getAcElementsDefinitions()
222 .get(compositionId).get(element.getDefinition()).getOutProperties();
223 return new CompositionElementDto(compositionId,
224 element.getDefinition(), compositionInProperties, compositionOutProperties);