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.parameters.ParticipantParameters;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
35 import org.onap.policy.clamp.models.acm.concepts.DeployState;
36 import org.onap.policy.clamp.models.acm.concepts.LockState;
37 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
38 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
40 import org.onap.policy.models.base.PfUtils;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
42 import org.springframework.stereotype.Component;
45 public class CacheProvider {
48 private final UUID participantId;
50 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
53 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
56 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
57 new ConcurrentHashMap<>();
60 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
65 * @param parameters the parameters of the participant
67 public CacheProvider(ParticipantParameters parameters) {
68 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
69 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
72 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
73 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
77 * Get AutomationComposition by id.
79 * @param automationCompositionId the AutomationComposition Id
80 * @return the AutomationComposition
82 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
83 return automationCompositions.get(automationCompositionId);
87 * Remove AutomationComposition.
89 * @param automationCompositionId the AutomationComposition Id
91 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
92 automationCompositions.remove(automationCompositionId);
96 * Add ElementDefinition.
98 * @param compositionId the composition Id
99 * @param list the list of AutomationCompositionElementDefinition to add
101 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
102 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
103 for (var acElementDefinition : list) {
104 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
106 acElementsDefinitions.put(compositionId, map);
109 public void removeElementDefinition(@NonNull UUID compositionId) {
110 acElementsDefinitions.remove(compositionId);
114 * Get CommonProperties.
116 * @param instanceId the Automation Composition Id
117 * @param acElementId the Automation Composition Element Id
118 * @return the common Properties as Map
120 public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
121 var automationComposition = automationCompositions.get(instanceId);
122 var map = acElementsDefinitions.get(automationComposition.getCompositionId());
123 var element = automationComposition.getElements().get(acElementId);
124 return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
128 * Get CommonProperties.
130 * @param compositionId the composition Id
131 * @param definition the AutomationCompositionElementDefinition Id
132 * @return the common Properties as Map
134 public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
135 @NonNull ToscaConceptIdentifier definition) {
136 return acElementsDefinitions.get(compositionId).get(definition)
137 .getAutomationCompositionElementToscaNodeTemplate().getProperties();
141 * Initialize an AutomationComposition from a ParticipantDeploy.
143 * @param compositionId the composition Id
144 * @param instanceId the Automation Composition Id
145 * @param participantDeploy the ParticipantDeploy
147 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
148 ParticipantDeploy participantDeploy) {
149 var acLast = automationCompositions.get(instanceId);
150 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
151 for (var element : participantDeploy.getAcElementList()) {
152 var acElement = new AutomationCompositionElement();
153 acElement.setId(element.getId());
154 acElement.setParticipantId(getParticipantId());
155 acElement.setDefinition(element.getDefinition());
156 acElement.setDeployState(DeployState.DEPLOYING);
157 acElement.setLockState(LockState.NONE);
158 acElement.setProperties(element.getProperties());
159 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
160 if (acElementLast != null) {
161 acElement.setOutProperties(acElementLast.getOutProperties());
162 acElement.setOperationalState(acElementLast.getOperationalState());
163 acElement.setUseState(acElementLast.getUseState());
165 acElementMap.put(element.getId(), acElement);
167 var automationComposition = new AutomationComposition();
168 automationComposition.setCompositionId(compositionId);
169 automationComposition.setInstanceId(instanceId);
170 automationComposition.setElements(acElementMap);
171 automationCompositions.put(instanceId, automationComposition);
175 * Initialize an AutomationComposition from a ParticipantRestartAc.
177 * @param compositionId the composition Id
178 * @param participantRestartAc the ParticipantRestartAc
180 public void initializeAutomationComposition(@NonNull UUID compositionId,
181 ParticipantRestartAc participantRestartAc) {
182 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
183 for (var element : participantRestartAc.getAcElementList()) {
184 var acElement = new AutomationCompositionElement();
185 acElement.setId(element.getId());
186 acElement.setParticipantId(getParticipantId());
187 acElement.setDefinition(element.getDefinition());
188 acElement.setDeployState(element.getDeployState());
189 acElement.setLockState(element.getLockState());
190 acElement.setOperationalState(element.getOperationalState());
191 acElement.setUseState(element.getUseState());
192 acElement.setProperties(element.getProperties());
193 acElement.setOutProperties(element.getOutProperties());
194 acElement.setRestarting(true);
195 acElementMap.put(element.getId(), acElement);
198 var automationComposition = new AutomationComposition();
199 automationComposition.setCompositionId(compositionId);
200 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
201 automationComposition.setElements(acElementMap);
202 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);