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