2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 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.ParticipantSupportedElementType;
39 import org.onap.policy.models.base.PfUtils;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
41 import org.springframework.stereotype.Component;
44 public class CacheProvider {
47 private final UUID participantId;
49 private final List<ParticipantSupportedElementType> supportedAcElementTypes;
52 private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
55 private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
56 new ConcurrentHashMap<>();
59 private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
64 * @param parameters the parameters of the participant
66 public CacheProvider(ParticipantParameters parameters) {
67 this.participantId = parameters.getIntermediaryParameters().getParticipantId();
68 this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
71 public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
72 return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
76 * Get AutomationComposition by id.
78 * @param automationCompositionId the AutomationComposition Id
79 * @return the AutomationComposition
81 public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
82 return automationCompositions.get(automationCompositionId);
86 * Remove AutomationComposition.
88 * @param automationCompositionId the AutomationComposition Id
90 public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
91 automationCompositions.remove(automationCompositionId);
95 * Add ElementDefinition.
97 * @param compositionId the composition Id
98 * @param list the list of AutomationCompositionElementDefinition to add
100 public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
101 Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
102 for (var acElementDefinition : list) {
103 map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
105 acElementsDefinitions.put(compositionId, map);
108 public void removeElementDefinition(@NonNull UUID compositionId) {
109 acElementsDefinitions.remove(compositionId);
113 * Get CommonProperties.
115 * @param instanceId the Automation Composition Id
116 * @param acElementId the Automation Composition Element Id
117 * @return the common Properties as Map
119 public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
120 var automationComposition = automationCompositions.get(instanceId);
121 var map = acElementsDefinitions.get(automationComposition.getCompositionId());
122 var element = automationComposition.getElements().get(acElementId);
123 return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
127 * Initialize an AutomationComposition from a ParticipantDeploy.
129 * @param compositionId the composition Id
130 * @param instanceId the Automation Composition Id
131 * @param participantDeploy the ParticipantDeploy
133 public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
134 ParticipantDeploy participantDeploy) {
135 Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
136 for (var element : participantDeploy.getAcElementList()) {
137 var acElement = new AutomationCompositionElement();
138 acElement.setId(element.getId());
139 acElement.setParticipantId(getParticipantId());
140 acElement.setDefinition(element.getDefinition());
141 acElement.setDeployState(DeployState.DEPLOYING);
142 acElement.setLockState(LockState.NONE);
143 acElement.setProperties(element.getProperties());
144 acElementMap.put(element.getId(), acElement);
147 var automationComposition = new AutomationComposition();
148 automationComposition.setCompositionId(compositionId);
149 automationComposition.setInstanceId(instanceId);
150 automationComposition.setElements(acElementMap);
151 automationCompositions.put(automationComposition.getInstanceId(), automationComposition);