daf9d6e7143dc9b9a0bddc523e8969a1d9f2c119
[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.intermediary.api.impl;
22
23 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
24 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionDto;
25 import org.onap.policy.clamp.acm.participant.intermediary.api.CompositionElementDto;
26 import org.onap.policy.clamp.acm.participant.intermediary.api.InstanceElementDto;
27 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
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.models.base.PfModelException;
33
34 /**
35  * Wrapper of AutomationCompositionElementListener.
36  * Valid since 7.1.1 release.
37  */
38 public abstract class AcElementListenerV2 implements AutomationCompositionElementListener {
39     protected final ParticipantIntermediaryApi intermediaryApi;
40
41     protected AcElementListenerV2(ParticipantIntermediaryApi intermediaryApi) {
42         this.intermediaryApi = intermediaryApi;
43     }
44
45     @Override
46     public void lock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
47         throws PfModelException {
48         intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
49             instanceElement.elementId(), null, LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
50     }
51
52     @Override
53     public void unlock(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
54         throws PfModelException {
55         intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
56             instanceElement.elementId(), null, LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
57     }
58
59     @Override
60     public void delete(CompositionElementDto compositionElement, InstanceElementDto instanceElement)
61         throws PfModelException {
62         intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
63             instanceElement.elementId(), DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
64     }
65
66     @Override
67     public void update(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
68                        InstanceElementDto instanceElementUpdated) throws PfModelException {
69         intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
70             instanceElement.elementId(), DeployState.DEPLOYED, null,
71             StateChangeResult.NO_ERROR, "Update not supported");
72
73     }
74
75     @Override
76     public void prime(CompositionDto composition) throws PfModelException {
77         intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.PRIMED,
78             StateChangeResult.NO_ERROR, "Primed");
79     }
80
81     @Override
82     public void deprime(CompositionDto composition) throws PfModelException {
83         intermediaryApi.updateCompositionState(composition.compositionId(), AcTypeState.COMMISSIONED,
84             StateChangeResult.NO_ERROR, "Deprimed");
85     }
86
87     @Override
88     public void handleRestartComposition(CompositionDto composition, AcTypeState state) throws PfModelException {
89         switch (state) {
90             case PRIMING -> prime(composition);
91             case DEPRIMING -> deprime(composition);
92             default -> intermediaryApi
93                 .updateCompositionState(composition.compositionId(), state, StateChangeResult.NO_ERROR, "Restarted");
94         }
95     }
96
97     @Override
98     public void handleRestartInstance(CompositionElementDto compositionElement, InstanceElementDto instanceElement,
99                                       DeployState deployState, LockState lockState) throws PfModelException {
100
101         if (DeployState.DEPLOYING.equals(deployState)) {
102             deploy(compositionElement, instanceElement);
103             return;
104         }
105         if (DeployState.UNDEPLOYING.equals(deployState)) {
106             undeploy(compositionElement, instanceElement);
107             return;
108         }
109         if (DeployState.UPDATING.equals(deployState)) {
110             update(compositionElement, instanceElement, instanceElement);
111             return;
112         }
113         if (DeployState.DELETING.equals(deployState)) {
114             delete(compositionElement, instanceElement);
115             return;
116         }
117         if (LockState.LOCKING.equals(lockState)) {
118             lock(compositionElement, instanceElement);
119             return;
120         }
121         if (LockState.UNLOCKING.equals(lockState)) {
122             unlock(compositionElement, instanceElement);
123             return;
124         }
125         intermediaryApi.updateAutomationCompositionElementState(instanceElement.instanceId(),
126             instanceElement.elementId(), deployState, lockState, StateChangeResult.NO_ERROR, "Restarted");
127     }
128
129     @Override
130     public void migrate(CompositionElementDto compositionElement, CompositionElementDto compositionElementTarget,
131                         InstanceElementDto instanceElement, InstanceElementDto instanceElementMigrate)
132         throws PfModelException {
133         intermediaryApi.updateAutomationCompositionElementState(instanceElementMigrate.instanceId(),
134             instanceElementMigrate.elementId(), DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Migrated");
135     }
136 }