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