119cc11b503e2597f070c068c1cd2c3f1798be1e
[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     @Getter
59     private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
60
61     /**
62      * Constructor.
63      *
64      * @param parameters the parameters of the participant
65      */
66     public CacheProvider(ParticipantParameters parameters) {
67         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
68         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
69     }
70
71     public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
72         return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
73     }
74
75     /**
76      * Get AutomationComposition by id.
77      *
78      * @param automationCompositionId the AutomationComposition Id
79      * @return the AutomationComposition
80      */
81     public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
82         return automationCompositions.get(automationCompositionId);
83     }
84
85     /**
86      * Remove AutomationComposition.
87      *
88      * @param automationCompositionId the AutomationComposition Id
89      */
90     public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
91         automationCompositions.remove(automationCompositionId);
92     }
93
94     /**
95      * Add ElementDefinition.
96      *
97      * @param compositionId the composition Id
98      * @param list the list of AutomationCompositionElementDefinition to add
99      */
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);
104         }
105         acElementsDefinitions.put(compositionId, map);
106     }
107
108     public void removeElementDefinition(@NonNull UUID compositionId) {
109         acElementsDefinitions.remove(compositionId);
110     }
111
112     /**
113      * Get CommonProperties.
114      *
115      * @param instanceId the Automation Composition Id
116      * @param acElementId the Automation Composition Element Id
117      * @return the common Properties as Map
118      */
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();
124     }
125
126     /**
127      * Initialize an AutomationComposition from a ParticipantDeploy.
128      *
129      * @param compositionId the composition Id
130      * @param instanceId the Automation Composition Id
131      * @param participantDeploy the ParticipantDeploy
132      */
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);
145         }
146
147         var automationComposition = new AutomationComposition();
148         automationComposition.setCompositionId(compositionId);
149         automationComposition.setInstanceId(instanceId);
150         automationComposition.setElements(acElementMap);
151         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
152     }
153 }