08d5ccd32410b78d342f9a7ab13be60033c9e937
[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.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;
43
44 @Component
45 public class CacheProvider {
46
47     @Getter
48     private final UUID participantId;
49
50     private final List<ParticipantSupportedElementType> supportedAcElementTypes;
51
52     @Getter
53     private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
54
55     @Getter
56     private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
57             new ConcurrentHashMap<>();
58
59     @Getter
60     private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
61
62     /**
63      * Constructor.
64      *
65      * @param parameters the parameters of the participant
66      */
67     public CacheProvider(ParticipantParameters parameters) {
68         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
69         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
70     }
71
72     public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
73         return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
74     }
75
76     /**
77      * Get AutomationComposition by id.
78      *
79      * @param automationCompositionId the AutomationComposition Id
80      * @return the AutomationComposition
81      */
82     public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
83         return automationCompositions.get(automationCompositionId);
84     }
85
86     /**
87      * Remove AutomationComposition.
88      *
89      * @param automationCompositionId the AutomationComposition Id
90      */
91     public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
92         automationCompositions.remove(automationCompositionId);
93     }
94
95     /**
96      * Add ElementDefinition.
97      *
98      * @param compositionId the composition Id
99      * @param list the list of AutomationCompositionElementDefinition to add
100      */
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);
105         }
106         acElementsDefinitions.put(compositionId, map);
107     }
108
109     public void removeElementDefinition(@NonNull UUID compositionId) {
110         acElementsDefinitions.remove(compositionId);
111     }
112
113     /**
114      * Get CommonProperties.
115      *
116      * @param instanceId the Automation Composition Id
117      * @param acElementId the Automation Composition Element Id
118      * @return the common Properties as Map
119      */
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();
125     }
126
127     /**
128      * Initialize an AutomationComposition from a ParticipantDeploy.
129      *
130      * @param compositionId the composition Id
131      * @param instanceId the Automation Composition Id
132      * @param participantDeploy the ParticipantDeploy
133      */
134     public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
135             ParticipantDeploy participantDeploy) {
136         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
137         for (var element : participantDeploy.getAcElementList()) {
138             var acElement = new AutomationCompositionElement();
139             acElement.setId(element.getId());
140             acElement.setParticipantId(getParticipantId());
141             acElement.setDefinition(element.getDefinition());
142             acElement.setDeployState(DeployState.DEPLOYING);
143             acElement.setLockState(LockState.NONE);
144             acElement.setProperties(element.getProperties());
145             acElementMap.put(element.getId(), acElement);
146         }
147
148         var automationComposition = new AutomationComposition();
149         automationComposition.setCompositionId(compositionId);
150         automationComposition.setInstanceId(instanceId);
151         automationComposition.setElements(acElementMap);
152         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
153     }
154
155     /**
156      * Initialize an AutomationComposition from a ParticipantRestartAc.
157      *
158      * @param compositionId the composition Id
159      * @param participantRestartAc the ParticipantRestartAc
160      */
161     public void initializeAutomationComposition(@NonNull UUID compositionId,
162             ParticipantRestartAc participantRestartAc) {
163         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
164         for (var element : participantRestartAc.getAcElementList()) {
165             var acElement = new AutomationCompositionElement();
166             acElement.setId(element.getId());
167             acElement.setParticipantId(getParticipantId());
168             acElement.setDefinition(element.getDefinition());
169             acElement.setDeployState(element.getDeployState());
170             acElement.setLockState(element.getLockState());
171             acElement.setProperties(element.getProperties());
172             acElement.setRestarting(true);
173             acElementMap.put(element.getId(), acElement);
174         }
175
176         var automationComposition = new AutomationComposition();
177         automationComposition.setCompositionId(compositionId);
178         automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
179         automationComposition.setElements(acElementMap);
180         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
181     }
182 }