09e75e8d5bae9f370373e701008510cfad3f4415
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.acm.participant.intermediary.handler;
22
23 import java.util.HashMap;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.UUID;
28 import java.util.concurrent.ConcurrentHashMap;
29 import lombok.Getter;
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;
42
43 @Component
44 public class CacheProvider {
45
46     @Getter
47     private final UUID participantId;
48
49     private final List<ParticipantSupportedElementType> supportedAcElementTypes;
50
51     @Getter
52     private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
53
54     @Getter
55     private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
56             new ConcurrentHashMap<>();
57
58     /**
59      * Constructor.
60      *
61      * @param parameters the parameters of the participant
62      */
63     public CacheProvider(ParticipantParameters parameters) {
64         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
65         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
66     }
67
68     public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
69         return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
70     }
71
72     /**
73      * Get AutomationComposition by id.
74      *
75      * @param automationCompositionId the AutomationComposition Id
76      * @return the AutomationComposition
77      */
78     public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
79         return automationCompositions.get(automationCompositionId);
80     }
81
82     /**
83      * Remove AutomationComposition.
84      *
85      * @param automationCompositionId the AutomationComposition Id
86      */
87     public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
88         automationCompositions.remove(automationCompositionId);
89     }
90
91     /**
92      * Add ElementDefinition.
93      *
94      * @param compositionId the composition Id
95      * @param list the list of AutomationCompositionElementDefinition to add
96      */
97     public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
98         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
99         for (var acElementDefinition : list) {
100             map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
101         }
102         acElementsDefinitions.put(compositionId, map);
103     }
104
105     public void removeElementDefinition(@NonNull UUID compositionId) {
106         acElementsDefinitions.remove(compositionId);
107     }
108
109     /**
110      * Get CommonProperties.
111      *
112      * @param instanceId the Automation Composition Id
113      * @param acElementId the Automation Composition Element Id
114      * @return the common Properties as Map
115      */
116     public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
117         var automationComposition = automationCompositions.get(instanceId);
118         var map = acElementsDefinitions.get(automationComposition.getCompositionId());
119         var element = automationComposition.getElements().get(acElementId);
120         return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
121     }
122
123     /**
124      * Initialize an AutomationComposition from a ParticipantDeploy.
125      *
126      * @param compositionId the composition Id
127      * @param instanceId the Automation Composition Id
128      * @param participantDeploy the ParticipantDeploy
129      */
130     public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
131             ParticipantDeploy participantDeploy) {
132         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
133         for (var element : participantDeploy.getAcElementList()) {
134             var acElement = new AutomationCompositionElement();
135             acElement.setId(element.getId());
136             acElement.setParticipantId(getParticipantId());
137             acElement.setDefinition(element.getDefinition());
138             acElement.setDeployState(DeployState.DEPLOYING);
139             acElement.setLockState(LockState.NONE);
140             acElement.setProperties(element.getProperties());
141             acElementMap.put(element.getId(), acElement);
142         }
143
144         var automationComposition = new AutomationComposition();
145         automationComposition.setCompositionId(compositionId);
146         automationComposition.setInstanceId(instanceId);
147         automationComposition.setElements(acElementMap);
148         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
149     }
150 }