2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-2025 OpenInfra Foundation Europe. All rights reserved.
4 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.participant.intermediary.api.impl;
25 import java.util.UUID;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
28 import org.onap.policy.clamp.acm.participant.intermediary.api.ElementState;
29 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
30 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
31 import org.onap.policy.clamp.acm.participant.intermediary.handler.AutomationCompositionOutHandler;
32 import org.onap.policy.clamp.acm.participant.intermediary.handler.cache.CacheProvider;
33 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
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.StateChangeResult;
40 import org.onap.policy.clamp.models.acm.utils.AcmStageUtils;
41 import org.onap.policy.models.base.PfUtils;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.springframework.stereotype.Component;
46 * This class is api implementation used by participant intermediary.
49 @RequiredArgsConstructor
50 public class ParticipantIntermediaryApiImpl implements ParticipantIntermediaryApi {
52 private static final int MAX_STAGES = 1000;
54 // The handler for the automationComposition intermediary
55 private final AutomationCompositionOutHandler automationCompositionHandler;
56 private final CacheProvider cacheProvider;
59 public void updateAutomationCompositionElementState(UUID instance, UUID elementId, DeployState deployState,
60 LockState lockState, StateChangeResult stateChangeResult, String message) {
61 automationCompositionHandler.updateAutomationCompositionElementState(instance, elementId, deployState,
62 lockState, stateChangeResult, message);
66 public void updateAutomationCompositionElementStage(UUID instance, UUID elementId,
67 StateChangeResult stateChangeResult, int stage, String message) {
68 automationCompositionHandler.updateAutomationCompositionElementStage(instance, elementId, stateChangeResult,
73 public void sendAcElementInfo(UUID instance, UUID elementId, String useState,
74 String operationalState, Map<String, Object> outProperties) {
75 automationCompositionHandler.sendAcElementInfo(instance, elementId, useState, operationalState,
80 public Map<UUID, AutomationComposition> getAutomationCompositions() {
81 return PfUtils.mapMap(cacheProvider.getAutomationCompositions(), AutomationComposition::new);
85 public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
87 automationCompositionHandler.updateCompositionState(compositionId, state, stateChangeResult, message);
91 public int getMigrateNextStage(CompositionElementDto compositionElementTarget, int lastStage) {
92 var stageSet = AcmStageUtils.findStageSetMigrate(compositionElementTarget.inProperties());
93 var nextStage = MAX_STAGES;
94 for (var s : stageSet) {
96 nextStage = Math.min(s, nextStage);
99 return nextStage == MAX_STAGES ? lastStage : nextStage;
103 public int getRollbackNextStage(CompositionElementDto compositionElementRollback, int lastStage) {
104 return getMigrateNextStage(compositionElementRollback, lastStage);
108 public int getPrepareNextStage(CompositionElementDto compositionElement, int lastStage) {
109 var stageSet = AcmStageUtils.findStageSetPrepare(compositionElement.inProperties());
110 var nextStage = MAX_STAGES;
111 for (var s : stageSet) {
113 nextStage = Math.min(s, nextStage);
116 return nextStage == MAX_STAGES ? lastStage : nextStage;
120 public AutomationCompositionElement getAutomationCompositionElement(UUID instanceId, UUID elementId) {
121 var automationComposition = cacheProvider.getAutomationCompositions().get(instanceId);
122 if (automationComposition == null) {
125 var element = automationComposition.getElements().get(elementId);
126 return element != null ? new AutomationCompositionElement(element) : null;
130 public InstanceElementDto getInstanceElementDto(UUID instanceId, UUID elementId) {
131 var element = getAutomationCompositionElement(instanceId, elementId);
132 return element == null
133 ? new InstanceElementDto(instanceId, elementId, Map.of(), Map.of(), ElementState.NOT_PRESENT)
134 : new InstanceElementDto(instanceId, elementId, element.getProperties(), element.getOutProperties());
138 public void sendAcDefinitionInfo(UUID compositionId, ToscaConceptIdentifier elementId,
139 Map<String, Object> outProperties) {
140 automationCompositionHandler.sendAcDefinitionInfo(compositionId, elementId, outProperties);
144 public AutomationComposition getAutomationComposition(UUID instanceId) {
145 var automationComposition = cacheProvider.getAutomationCompositions().get(instanceId);
146 return automationComposition != null ? new AutomationComposition(automationComposition) : null;
150 public Map<UUID, Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition>> getAcElementsDefinitions() {
151 return PfUtils.mapMap(cacheProvider.getAcElementsDefinitions(),
152 acDefinition -> PfUtils.mapMap(acDefinition.getElements(), AutomationCompositionElementDefinition::new));
156 public Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> getAcElementsDefinitions(
157 UUID compositionId) {
158 var acElementDefinitions = cacheProvider.getAcElementsDefinitions().get(compositionId);
159 if (acElementDefinitions == null) {
162 return PfUtils.mapMap(acElementDefinitions.getElements(), AutomationCompositionElementDefinition::new);
166 public AutomationCompositionElementDefinition getAcElementDefinition(UUID compositionId,
167 ToscaConceptIdentifier elementId) {
168 var acDefinition = cacheProvider.getAcElementsDefinitions().get(compositionId);
169 if (acDefinition == null) {
172 var acElementDefinition = acDefinition.getElements().get(elementId);
173 return acElementDefinition != null ? new AutomationCompositionElementDefinition(acElementDefinition) : null;
177 public CompositionElementDto getCompositionElementDto(UUID compositionId, ToscaConceptIdentifier elementId) {
178 var element = getAcElementDefinition(compositionId, elementId);
179 return element == null
180 ? new CompositionElementDto(compositionId, elementId, Map.of(), Map.of(), ElementState.NOT_PRESENT)
181 : new CompositionElementDto(compositionId, elementId,
182 element.getAutomationCompositionElementToscaNodeTemplate().getProperties(), element.getOutProperties());