b85a3c35a7e46ef0cf84216b1b6484c3cd7cb843
[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             var acElement = new AutomationCompositionElement();
195             acElement.setId(element.getId());
196             acElement.setParticipantId(getParticipantId());
197             acElement.setDefinition(element.getDefinition());
198             acElement.setDeployState(element.getDeployState());
199             acElement.setLockState(element.getLockState());
200             acElement.setOperationalState(element.getOperationalState());
201             acElement.setUseState(element.getUseState());
202             acElement.setProperties(element.getProperties());
203             acElement.setOutProperties(element.getOutProperties());
204             acElement.setRestarting(true);
205             acElementMap.put(element.getId(), acElement);
206         }
207
208         var automationComposition = new AutomationComposition();
209         automationComposition.setCompositionId(compositionId);
210         automationComposition.setInstanceId(participantRestartAc.getAutomationCompositionId());
211         automationComposition.setElements(acElementMap);
212         automationCompositions.put(automationComposition.getInstanceId(), automationComposition);
213     }
214
215     /**
216      * Create CompositionElementDto.
217      *
218      * @param compositionId the composition Id
219      * @param element AutomationComposition Element
220      * @param compositionInProperties composition definition InProperties
221      * @return the CompositionElementDto
222      */
223     public CompositionElementDto createCompositionElementDto(UUID compositionId, AutomationCompositionElement element,
224                                                               Map<String, Object> compositionInProperties) {
225         var compositionOutProperties = getAcElementsDefinitions()
226                 .get(compositionId).get(element.getDefinition()).getOutProperties();
227         return new CompositionElementDto(compositionId,
228                 element.getDefinition(), compositionInProperties, compositionOutProperties);
229     }
230 }