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 * Initialize an AutomationComposition from a ParticipantDeploy.
130 * @param compositionId the composition Id
131 * @param instanceId the Automation Composition Id
132 * @param participantDeploy the ParticipantDeploy
134 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
135 ParticipantDeploy participantDeploy) {
136 var acLast = automationCompositions.get(instanceId);
137 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
138 for (var element : participantDeploy.getAcElementList()) {
139 var acElement = new AutomationCompositionElement();
140 acElement.setId(element.getId());
141 acElement.setParticipantId(getParticipantId());
142 acElement.setDefinition(element.getDefinition());
143 acElement.setDeployState(DeployState.DEPLOYING);
144 acElement.setLockState(LockState.NONE);
145 acElement.setProperties(element.getProperties());
146 var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
147 if (acElementLast != null) {
148 acElement.setOutProperties(acElementLast.getOutProperties());
149 acElement.setOperationalState(acElementLast.getOperationalState());
150 acElement.setUseState(acElementLast.getUseState());
152 acElementMap.put(element.getId(), acElement);
154 var automationComposition = new AutomationComposition();
155 automationComposition.setCompositionId(compositionId);
156 automationComposition.setInstanceId(instanceId);
157 automationComposition.setElements(acElementMap);
158 automationCompositions.put(instanceId, automationComposition);
162 * Initialize an AutomationComposition from a ParticipantRestartAc.
164 * @param compositionId the composition Id
165 * @param participantRestartAc the ParticipantRestartAc
167 public void initializeAutomationComposition(@NonNull UUID compositionId,
168 ParticipantRestartAc participantRestartAc) {
169 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
170 for (var element : participantRestartAc.getAcElementList()) {
171 var acElement = new AutomationCompositionElement();
172 acElement.setId(element.getId());
173 acElement.setParticipantId(getParticipantId());
174 acElement.setDefinition(element.getDefinition());
175 acElement.setDeployState(element.getDeployState());
176 acElement.setLockState(element.getLockState());
177 acElement.setProperties(element.getProperties());
178 acElement.setRestarting(true);
179 acElementMap.put(element.getId(), acElement);
182 var automationComposition = new AutomationComposition();
183 automationComposition.setCompositionId(compositionId);
184 automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
185 automationComposition.setElements(acElementMap);
186 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);