7a00e089279693e8c2540708f34e4122fdb0e604
[policy/clamp.git] /
1 /*-
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
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 lombok.Setter;
32 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
33 import org.onap.policy.clamp.acm.participant.intermediary.parameters.ParticipantParameters;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
37 import org.onap.policy.clamp.models.acm.concepts.DeployState;
38 import org.onap.policy.clamp.models.acm.concepts.LockState;
39 import org.onap.policy.clamp.models.acm.concepts.ParticipantDeploy;
40 import org.onap.policy.clamp.models.acm.concepts.ParticipantRestartAc;
41 import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
42 import org.onap.policy.models.base.PfUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
44 import org.springframework.stereotype.Component;
45
46 @Component
47 public class CacheProvider {
48
49     @Getter
50     private final UUID participantId;
51
52     @Getter
53     @Setter
54     private boolean registered = false;
55
56     @Getter
57     private final UUID replicaId;
58
59     private final List<ParticipantSupportedElementType> supportedAcElementTypes;
60
61     @Getter
62     private final Map<UUID, AutomationComposition> automationCompositions = new ConcurrentHashMap<>();
63
64     @Getter
65     private final Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> acElementsDefinitions =
66             new ConcurrentHashMap<>();
67
68     @Getter
69     private final Map<UUID, UUID> msgIdentification = new ConcurrentHashMap<>();
70
71     /**
72      * Constructor.
73      *
74      * @param parameters the parameters of the participant
75      */
76     public CacheProvider(ParticipantParameters parameters) {
77         this.participantId = parameters.getIntermediaryParameters().getParticipantId();
78         this.supportedAcElementTypes = parameters.getIntermediaryParameters().getParticipantSupportedElementTypes();
79         this.replicaId = UUID.randomUUID();
80     }
81
82     public List<ParticipantSupportedElementType> getSupportedAcElementTypes() {
83         return PfUtils.mapList(supportedAcElementTypes, ParticipantSupportedElementType::new);
84     }
85
86     /**
87      * Get AutomationComposition by id.
88      *
89      * @param automationCompositionId the AutomationComposition Id
90      * @return the AutomationComposition
91      */
92     public AutomationComposition getAutomationComposition(@NonNull UUID automationCompositionId) {
93         return automationCompositions.get(automationCompositionId);
94     }
95
96     /**
97      * Remove AutomationComposition.
98      *
99      * @param automationCompositionId the AutomationComposition Id
100      */
101     public void removeAutomationComposition(@NonNull UUID automationCompositionId) {
102         automationCompositions.remove(automationCompositionId);
103     }
104
105     /**
106      * Add ElementDefinition.
107      *
108      * @param compositionId the composition Id
109      * @param list the list of AutomationCompositionElementDefinition to add
110      */
111     public void addElementDefinition(@NonNull UUID compositionId, List<AutomationCompositionElementDefinition> list) {
112         Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> map = new HashMap<>();
113         for (var acElementDefinition : list) {
114             map.put(acElementDefinition.getAcElementDefinitionId(), acElementDefinition);
115         }
116         acElementsDefinitions.put(compositionId, map);
117     }
118
119     public void removeElementDefinition(@NonNull UUID compositionId) {
120         acElementsDefinitions.remove(compositionId);
121     }
122
123     /**
124      * Get CommonProperties.
125      *
126      * @param instanceId the Automation Composition Id
127      * @param acElementId the Automation Composition Element Id
128      * @return the common Properties as Map
129      */
130     public Map<String, Object> getCommonProperties(@NonNull UUID instanceId, @NonNull UUID acElementId) {
131         var automationComposition = automationCompositions.get(instanceId);
132         var map = acElementsDefinitions.get(automationComposition.getCompositionId());
133         var element = automationComposition.getElements().get(acElementId);
134         return map.get(element.getDefinition()).getAutomationCompositionElementToscaNodeTemplate().getProperties();
135     }
136
137     /**
138      * Get CommonProperties.
139      *
140      * @param compositionId the composition Id
141      * @param definition the AutomationCompositionElementDefinition Id
142      * @return the common Properties as Map
143      */
144     public Map<String, Object> getCommonProperties(@NonNull UUID compositionId,
145         @NonNull ToscaConceptIdentifier definition) {
146         return acElementsDefinitions.get(compositionId).get(definition)
147             .getAutomationCompositionElementToscaNodeTemplate().getProperties();
148     }
149
150     /**
151      * Initialize an AutomationComposition from a ParticipantDeploy.
152      *
153      * @param compositionId the composition Id
154      * @param instanceId the Automation Composition Id
155      * @param participantDeploy the ParticipantDeploy
156      */
157     public void initializeAutomationComposition(@NonNull UUID compositionId, @NonNull UUID instanceId,
158             ParticipantDeploy participantDeploy) {
159         var acLast = automationCompositions.get(instanceId);
160         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
161         for (var element : participantDeploy.getAcElementList()) {
162             var acElement = new AutomationCompositionElement();
163             acElement.setId(element.getId());
164             acElement.setParticipantId(getParticipantId());
165             acElement.setDefinition(element.getDefinition());
166             acElement.setDeployState(DeployState.DEPLOYING);
167             acElement.setLockState(LockState.NONE);
168             acElement.setProperties(element.getProperties());
169             var acElementLast = acLast != null ? acLast.getElements().get(element.getId()) : null;
170             if (acElementLast != null) {
171                 acElement.setOutProperties(acElementLast.getOutProperties());
172                 acElement.setOperationalState(acElementLast.getOperationalState());
173                 acElement.setUseState(acElementLast.getUseState());
174             }
175             acElementMap.put(element.getId(), acElement);
176         }
177         var automationComposition = new AutomationComposition();
178         automationComposition.setCompositionId(compositionId);
179         automationComposition.setInstanceId(instanceId);
180         automationComposition.setElements(acElementMap);
181         automationCompositions.put(instanceId, automationComposition);
182     }
183
184     /**
185      * Initialize an AutomationComposition from a ParticipantRestartAc.
186      *
187      * @param compositionId the composition Id
188      * @param participantRestartAc the ParticipantRestartAc
189      */
190     public void initializeAutomationComposition(@NonNull UUID compositionId,
191             ParticipantRestartAc participantRestartAc) {
192         Map<UUID, AutomationCompositionElement> acElementMap = new LinkedHashMap<>();
193         for (var element : participantRestartAc.getAcElementList()) {
194             if (!getParticipantId().equals(element.getParticipantId())) {
195                 continue;
196             }
197             var acElement = new AutomationCompositionElement();
198             acElement.setId(element.getId());
199             acElement.setParticipantId(getParticipantId());
200             acElement.setDefinition(element.getDefinition());
201             acElement.setDeployState(element.getDeployState());
202             acElement.setLockState(element.getLockState());
203             acElement.setOperationalState(element.getOperationalState());
204             acElement.setUseState(element.getUseState());
205             acElement.setProperties(element.getProperties());
206             acElement.setOutProperties(element.getOutProperties());
207             acElementMap.put(element.getId(), acElement);
208         }
209
210         var automationComposition = new AutomationComposition();
211         automationComposition.setCompositionId(compositionId);
212         automationComposition.setDeployState(participantRestartAc.getDeployState());
213         automationComposition.setLockState(participantRestartAc.getLockState());
214         automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
215         automationComposition.setElements(acElementMap);
216         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
217     }
218
219     /**
220      * Create CompositionElementDto.
221      *
222      * @param compositionId the composition Id
223      * @param element AutomationComposition Element
224      * @param compositionInProperties composition definition InProperties
225      * @return the CompositionElementDto
226      */
227     public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
228                                                               Map<String, Object> compositionInProperties) {
229         var compositionOutProperties = getAcElementsDefinitions()
230                 .get(compositionId).get(element.getDefinition()).getOutProperties();
231         return new CompositionElementDto(compositionId,
232                 element.getDefinition(), compositionInProperties, compositionOutProperties);
233     }
234 }