e836c9874e6e0dec842b6bf4062ede2a2766b8fb
[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.sim.main.handler;
22
23 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
24 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
25 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
26 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
27 import org.onap.policy.clamp.acm.participant.intermediary.api.impl.AcElementListenerV2;
28 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
29 import org.onap.policy.clamp.models.acm.concepts.DeployState;
30 import org.onap.policy.clamp.models.acm.concepts.LockState;
31 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
32 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
33 import org.onap.policy.models.base.PfModelException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * This class handles implementation of automationCompositionElement updates.
41  */
42 @ConditionalOnExpression("'${element.handler:AcElementHandlerV2}' == 'AcElementHandlerV2'")
43 @Component
44 public class AutomationCompositionElementHandlerV2 extends AcElementListenerV2 {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionElementHandlerV2.class);
47
48     private final SimulatorService simulatorService;
49
50     public AutomationCompositionElementHandlerV2(ParticipantIntermediaryApi intermediaryApi,
51         SimulatorService simulatorService) {
52         super(intermediaryApi);
53         this.simulatorService = simulatorService;
54     }
55
56     /**
57      * Handle a deploy on a automation composition element.
58      *
59      * @param compositionElement the information of the Automation Composition Definition Element
60      * @param instanceElement the information of the Automation Composition Instance Element
61      * @throws PfModelException from Policy framework
62      */
63     @Override
64     public void deploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
65             throws PfModelException {
66         LOGGER.debug("deploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement);
67         simulatorService.deploy(instanceElement.instanceId(), instanceElement.elementId());
68     }
69
70     /**
71      * Handle a automation composition element state change.
72      *
73      * @param compositionElement the information of the Automation Composition Definition Element
74      * @param instanceElement the information of the Automation Composition Instance Element
75      * @throws PfModelException from Policy framework
76      */
77     @Override
78     public void undeploy(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
79             throws PfModelException {
80         LOGGER.debug("undeploy call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement);
81         simulatorService.undeploy(instanceElement.instanceId(), instanceElement.elementId());
82     }
83
84     @Override
85     public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
86             throws PfModelException {
87         LOGGER.debug("lock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement);
88         simulatorService.lock(instanceElement.instanceId(), instanceElement.elementId());
89     }
90
91     @Override
92     public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
93             throws PfModelException {
94         LOGGER.debug("unlock call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement);
95         simulatorService.unlock(instanceElement.instanceId(), instanceElement.elementId());
96     }
97
98     @Override
99     public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
100             throws PfModelException {
101         LOGGER.debug("delete call compositionElement: {}, instanceElement: {}", compositionElement, instanceElement);
102         simulatorService.delete(instanceElement.instanceId(), instanceElement.elementId());
103     }
104
105     @Override
106     public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
107                        InstanceElementDto instanceElementUpdated) throws PfModelException {
108         LOGGER.debug("update call compositionElement: {}, instanceElement: {}, instanceElementUpdated: {}",
109                 compositionElement, instanceElement, instanceElementUpdated);
110         simulatorService.update(instanceElement.instanceId(), instanceElement.elementId());
111     }
112
113     @Override
114     public void prime(CompositionDto composition) throws PfModelException {
115         LOGGER.debug("prime call composition: {}", composition);
116         simulatorService.prime(composition.compositionId());
117     }
118
119     @Override
120     public void deprime(CompositionDto composition) throws PfModelException {
121         LOGGER.debug("deprime call composition: {}", composition);
122         simulatorService.deprime(composition.compositionId());
123     }
124
125     @Override
126     public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException {
127         LOGGER.debug("restart composition definition call");
128         switch (state) {
129             case PRIMING:
130                 prime(composition);
131                 break;
132
133             case DEPRIMING:
134                 deprime(composition);
135                 break;
136
137             default:
138                 intermediaryApi.updateCompositionState(composition.compositionId(), state,
139                         StateChangeResult.NO_ERROR, "Restarted");
140         }
141     }
142
143     @Override
144     public void handleRestartInstance(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
145                                       DeployState deployState, LockState lockState) throws PfModelException {
146         LOGGER.debug("restart instance call");
147         if (!AcmUtils.isInTransitionalState(deployState, lockState)) {
148             intermediaryApi.updateAutomationCompositionElementState(
149                     instanceElement.instanceId(), instanceElement.elementId(), deployState, lockState,
150                     StateChangeResult.NO_ERROR, "Restarted");
151             return;
152         }
153         if (DeployState.DEPLOYING.equals(deployState)) {
154             deploy(compositionElement, instanceElement);
155             return;
156         }
157         if (DeployState.UNDEPLOYING.equals(deployState)) {
158             undeploy(compositionElement, instanceElement);
159             return;
160         }
161         if (DeployState.UPDATING.equals(deployState)) {
162             update(compositionElement, instanceElement, instanceElement);
163             return;
164         }
165         if (DeployState.DELETING.equals(deployState)) {
166             delete(compositionElement, instanceElement);
167             return;
168         }
169         if (LockState.LOCKING.equals(lockState)) {
170             lock(compositionElement, instanceElement);
171             return;
172         }
173         if (LockState.UNLOCKING.equals(lockState)) {
174             unlock(compositionElement, instanceElement);
175         }
176     }
177
178     @Override
179     public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget,
180                         InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate)
181             throws PfModelException {
182         LOGGER.debug("migrate call compositionElement: {}, compositionElementTarget: {}, instanceElement: {},"
183                         + " instanceElementMigrate: {}",
184                 compositionElement, compositionElementTarget, instanceElement, instanceElementMigrate);
185         simulatorService.migrate(instanceElement.instanceId(), instanceElement.elementId());
186     }
187 }