f503987a27a3b3f9a18ef79d590240308d1b0659
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 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.sim.main.handler;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.UUID;
26 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
27 import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV1;
28 import org.onap.policy.clamp.models.acm.concepts.AcElementDeploy;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
31 import org.onap.policy.clamp.models.acm.concepts.DeployState;
32 import org.onap.policy.clamp.models.acm.concepts.LockState;
33 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
34 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
35 import org.onap.policy.models.base.PfModelException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
39 import org.springframework.stereotype.Component;
40
41 @ConditionalOnExpression("'${element.handler}'=='AcElementHandlerV1'")
42 @Component
43 public class AutomationCompositionElementHandlerV1 extends AcElementListenerV1 {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandlerV1.class);
46
47     private final SimulatorService simulatorService;
48
49     public AutomationCompositionElementHandlerV1(ParticipantIntermediaryApi intermediaryApi,
50         SimulatorService simulatorService) {
51         super(intermediaryApi);
52         this.simulatorService = simulatorService;
53     }
54
55     @Override
56     public void deploy(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
57             throws PfModelException {
58         LOGGER.debug("deploy call instanceId: {}, element: {}, properties: {}", instanceId, element, properties);
59         simulatorService.deploy(instanceId, element.getId());
60     }
61
62     @Override
63     public void undeploy(UUID instanceId, UUID elementId) throws PfModelException {
64         LOGGER.debug("undeploy call instanceId: {}, elementId: {}", instanceId, elementId);
65         simulatorService.undeploy(instanceId, elementId);
66     }
67
68     @Override
69     public void lock(UUID instanceId, UUID elementId) throws PfModelException {
70         LOGGER.debug("lock call instanceId: {}, elementId: {}", instanceId, elementId);
71         simulatorService.lock(instanceId, elementId);
72     }
73
74     @Override
75     public void unlock(UUID instanceId, UUID elementId) throws PfModelException {
76         LOGGER.debug("unlock call instanceId: {}, elementId: {}", instanceId, elementId);
77         simulatorService.unlock(instanceId, elementId);
78     }
79
80     @Override
81     public void delete(UUID instanceId, UUID elementId) throws PfModelException {
82         LOGGER.debug("delete call instanceId: {}, elementId: {}", instanceId, elementId);
83         simulatorService.delete(instanceId, elementId);
84     }
85
86     @Override
87     public void update(UUID instanceId, AcElementDeploy element, Map<String, Object> properties)
88             throws PfModelException {
89         LOGGER.debug("update call instanceId: {}, element: {}, properties: {}", instanceId, element, properties);
90         simulatorService.update(instanceId, element.getId());
91     }
92
93     @Override
94     public void prime(UUID compositionId, List<AutomationCompositionElementDefinition> elementDefinitionList)
95             throws PfModelException {
96         LOGGER.debug("prime call compositionId: {}, elementDefinitionList: {}", compositionId, elementDefinitionList);
97         simulatorService.prime(compositionId);
98     }
99
100     @Override
101     public void deprime(UUID compositionId) throws PfModelException {
102         LOGGER.debug("deprime call compositionId: {}", compositionId);
103         simulatorService.deprime(compositionId);
104     }
105
106     @Override
107     public void handleRestartComposition(UUID compositionId,
108         List<AutomationCompositionElementDefinition> elementDefinitionList, AcTypeState state)
109             throws PfModelException {
110         LOGGER.debug("restart composition definition call compositionId: {}, elementDefinitionList: {}, state: {}",
111                 compositionId, elementDefinitionList, state);
112
113         switch (state) {
114             case PRIMING:
115                 prime(compositionId, elementDefinitionList);
116                 break;
117
118             case DEPRIMING:
119                 deprime(compositionId);
120                 break;
121
122             default:
123                 intermediaryApi.updateCompositionState(compositionId, state, StateChangeResult.NO_ERROR, "Restarted");
124         }
125     }
126
127     @Override
128     public void handleRestartInstance(UUID instanceId, AcElementDeploy element,
129         Map<String, Object> properties, DeployState deployState, LockState lockState) throws PfModelException {
130         LOGGER.debug("restart instance call instanceId: {}, element: {}, properties: {},"
131                 + "deployState: {}, lockState: {}", instanceId, element, properties, deployState, lockState);
132
133         if (!AcmUtils.isInTransitionalState(deployState, lockState)) {
134             intermediaryApi.updateAutomationCompositionElementState(instanceId, element.getId(),
135                     deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
136             return;
137         }
138         if (DeployState.DEPLOYING.equals(deployState)) {
139             deploy(instanceId, element, properties);
140             return;
141         }
142         if (DeployState.UNDEPLOYING.equals(deployState)) {
143             undeploy(instanceId, element.getId());
144             return;
145         }
146         if (DeployState.UPDATING.equals(deployState)) {
147             update(instanceId, element, properties);
148             return;
149         }
150         if (DeployState.DELETING.equals(deployState)) {
151             delete(instanceId, element.getId());
152             return;
153         }
154         if (LockState.LOCKING.equals(lockState)) {
155             lock(instanceId, element.getId());
156             return;
157         }
158         if (LockState.UNLOCKING.equals(lockState)) {
159             unlock(instanceId, element.getId());
160         }
161     }
162
163     @Override
164     public void migrate(UUID instanceId, AcElementDeploy element, UUID compositionTargetId,
165         Map<String, Object> properties) throws PfModelException {
166         LOGGER.debug("migrate call instanceId: {}, element: {}, compositionTargetId: {}, properties: {}",
167                 instanceId, element, compositionTargetId, properties);
168         simulatorService.migrate(instanceId, element.getId());
169     }
170 }