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