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;
31 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
32 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
36 import org.onap.policy.clamp.models.acm.concepts.DeployState;
37 import org.onap.policy.clamp.models.acm.concepts.LockState;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.springframework.stereotype.Component;
46 public class CacheProvider {
49 private final UUID participantId;
51 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
54 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
57 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
58 new ConcurrentHashMap<>();
61 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
66 * @param parameters the parameters of the participant
68 public CacheProvider(ParticipantParameters parameters) {
69 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
70 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
73 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
74 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
78 * Get AutomationComposition by id.
80 * @param automationCompositionId the AutomationComposition Id
81 * @return the AutomationComposition
83 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
84 return automationCompositions.get(automationCompositionId);
88 * Remove AutomationComposition.
90 * @param automationCompositionId the AutomationComposition Id
92 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
93 automationCompositions.remove(automationCompositionId);
97 * Add ElementDefinition.
99 * @param compositionId the composition Id
100 * @param list the list of AutomationCompositionElementDefinition to add
102 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
103 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
104 for (var acElementDefinition : list) {
105 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
107 acElementsDefinitions.put(compositionId, map);
110 public void removeElementDefinition(@NonNull UUID compositionId) {
111 acElementsDefinitions.remove(compositionId);
115 * Get CommonProperties.
117 * @param instanceId the Automation Composition Id
118 * @param acElementId the Automation Composition Element Id
119 * @return the common Properties as Map
121 public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
122 var automationComposition = automationCompositions.get(instanceId);
123 var map = acElementsDefinitions.get(automationComposition.getCompositionId());
124 var element = automationComposition.getElements().get(acElementId);
125 return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
129 * Get CommonProperties.
131 * @param compositionId the composition Id
132 * @param definition the AutomationCompositionElementDefinition Id
133 * @return the common Properties as Map
135 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
136 @NonNull ToscaConceptIdentifier definition) {
137 return acElementsDefinitions.get(compositionId).get(definition)
138 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
142 * Initialize an AutomationComposition from a ParticipantDeploy.
144 * @param compositionId the composition Id
145 * @param instanceId the Automation Composition Id
146 * @param participantDeploy the ParticipantDeploy
148 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
149 ParticipantDeploy participantDeploy) {
150 var acLast = automationCompositions.get(instanceId);
151 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
152 for (var element : participantDeploy.getAcElementList()) {
153 var acElement = new AutomationCompositionElement();
154 acElement.setId(element.getId());
155 acElement.setParticipantId(getParticipantId());
156 acElement.setDefinition(element.getDefinition());
157 acElement.setDeployState(DeployState.DEPLOYING);
158 acElement.setLockState(LockState.NONE);
159 acElement.setProperties(element.getProperties());
160 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
161 if (acElementLast != null) {
162 acElement.setOutProperties(acElementLast.getOutProperties());
163 acElement.setOperationalState(acElementLast.getOperationalState());
164 acElement.setUseState(acElementLast.getUseState());
166 acElementMap.put(element.getId(), acElement);
168 var automationComposition = new AutomationComposition();
169 automationComposition.setCompositionId(compositionId);
170 automationComposition.setInstanceId(instanceId);
171 automationComposition.setElements(acElementMap);
172 automationCompositions.put(instanceId, automationComposition);
176 * Initialize an AutomationComposition from a ParticipantRestartAc.
178 * @param compositionId the composition Id
179 * @param participantRestartAc the ParticipantRestartAc
181 public void initializeAutomationComposition(@NonNull UUID compositionId,
182 ParticipantRestartAc participantRestartAc) {
183 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
184 for (var element : participantRestartAc.getAcElementList()) {
185 var acElement = new AutomationCompositionElement();
186 acElement.setId(element.getId());
187 acElement.setParticipantId(getParticipantId());
188 acElement.setDefinition(element.getDefinition());
189 acElement.setDeployState(element.getDeployState());
190 acElement.setLockState(element.getLockState());
191 acElement.setOperationalState(element.getOperationalState());
192 acElement.setUseState(element.getUseState());
193 acElement.setProperties(element.getProperties());
194 acElement.setOutProperties(element.getOutProperties());
195 acElement.setRestarting(true);
196 acElementMap.put(element.getId(), acElement);
199 var automationComposition = new AutomationComposition();
200 automationComposition.setCompositionId(compositionId);
201 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
202 automationComposition.setElements(acElementMap);
203 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
207 * Create CompositionElementDto.
209 * @param compositionId the composition Id
210 * @param element AutomationComposition Element
211 * @param compositionInProperties composition definition InProperties
212 * @return the CompositionElementDto
214 public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
215 Map<String, Object> compositionInProperties) {
216 var compositionOutProperties = getAcElementsDefinitions()
217 .get(compositionId).get(element.getDefinition()).getOutProperties();
218 return new CompositionElementDto(compositionId,
219 element.getDefinition(), compositionInProperties, compositionOutProperties);