aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-intermediary/src/main/java/org/onap/policy/clamp/acm/participant/intermediary/handler/AutomationCompositionOutHandler.java
blob: 3023cddda9905f4b8e032b2e65d028d42be8ceaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.acm.participant.intermediary.handler;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.onap.policy.clamp.acm.participant.intermediary.comm.ParticipantMessagePublisher;
import org.onap.policy.clamp.models.acm.concepts.AcElementDeployAck;
import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementDefinition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElementInfo;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionInfo;
import org.onap.policy.clamp.models.acm.concepts.DeployState;
import org.onap.policy.clamp.models.acm.concepts.LockState;
import org.onap.policy.clamp.models.acm.concepts.ParticipantDefinition;
import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.AutomationCompositionDeployAck;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantPrimeAck;
import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantStatus;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class AutomationCompositionOutHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(AutomationCompositionOutHandler.class);

    private final ParticipantMessagePublisher publisher;
    private final CacheProvider cacheProvider;

    /**
     * Handle a automation composition element state change message.
     *
     * @param automationCompositionId the automationComposition Id
     * @param elementId the automationComposition Element Id
     * @param deployState the DeployState state
     * @param lockState the LockState state
     * @param message the message
     * @param stateChangeResult the indicator if error occurs
     */
    public void updateAutomationCompositionElementState(UUID automationCompositionId, UUID elementId,
            DeployState deployState, LockState lockState, StateChangeResult stateChangeResult, String message) {

        if (automationCompositionId == null || elementId == null) {
            LOGGER.error("Cannot update Automation composition element state, id is null");
            return;
        }

        var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
        if (automationComposition == null) {
            LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
                    automationCompositionId);
            return;
        }

        var element = automationComposition.getElements().get(elementId);
        if (element == null) {
            var msg = "Cannot update Automation composition element state, AC Element id {} not present";
            LOGGER.error(msg, elementId);
            return;
        }

        if ((element.getRestarting() == null)
                && ((deployState != null && lockState != null) || (deployState == null && lockState == null))) {
            LOGGER.error("state error {} and {} cannot be handled", deployState, lockState);
            return;
        }
        element.setRestarting(null);

        if (deployState != null) {
            handleDeployState(automationComposition, element, deployState);
        }
        if (lockState != null) {
            handleLockState(automationComposition, element, lockState);
        }

        var automationCompositionStateChangeAck =
                new AutomationCompositionDeployAck(ParticipantMessageType.AUTOMATION_COMPOSITION_STATECHANGE_ACK);
        automationCompositionStateChangeAck.setParticipantId(cacheProvider.getParticipantId());
        automationCompositionStateChangeAck.setMessage(message);
        automationCompositionStateChangeAck.setResponseTo(cacheProvider.getMsgIdentification().get(element.getId()));
        automationCompositionStateChangeAck.setStateChangeResult(stateChangeResult);
        automationCompositionStateChangeAck.setAutomationCompositionId(automationCompositionId);
        automationCompositionStateChangeAck.getAutomationCompositionResultMap().put(element.getId(),
                new AcElementDeployAck(element.getDeployState(), element.getLockState(), element.getOperationalState(),
                        element.getUseState(), element.getOutProperties(), true, message));
        LOGGER.debug("Automation composition element {} state changed to {}", elementId, deployState);
        automationCompositionStateChangeAck.setResult(true);
        publisher.sendAutomationCompositionAck(automationCompositionStateChangeAck);
        cacheProvider.getMsgIdentification().remove(element.getId());
    }

    private void handleDeployState(AutomationComposition automationComposition, AutomationCompositionElement element,
            DeployState deployState) {
        element.setDeployState(deployState);
        element.setLockState(DeployState.DEPLOYED.equals(element.getDeployState()) ? LockState.LOCKED : LockState.NONE);
        var checkOpt = automationComposition.getElements().values().stream()
                .filter(acElement -> !deployState.equals(acElement.getDeployState())).findAny();
        if (checkOpt.isEmpty()) {
            automationComposition.setDeployState(deployState);
            automationComposition.setLockState(element.getLockState());

            if (DeployState.DELETED.equals(deployState)) {
                cacheProvider.removeAutomationComposition(automationComposition.getInstanceId());
            }
        }
    }

    private void handleLockState(AutomationComposition automationComposition, AutomationCompositionElement element,
            LockState lockState) {
        element.setLockState(lockState);
        var checkOpt = automationComposition.getElements().values().stream()
                .filter(acElement -> !lockState.equals(acElement.getLockState())).findAny();
        if (checkOpt.isEmpty()) {
            automationComposition.setLockState(lockState);
        }
    }

    /**
     * Send Ac Element Info.
     *
     * @param automationCompositionId the automationComposition Id
     * @param elementId the automationComposition Element id
     * @param useState the use State
     * @param operationalState the operational State
     * @param outProperties the output Properties Map
     */
    public void sendAcElementInfo(UUID automationCompositionId, UUID elementId, String useState,
            String operationalState, Map<String, Object> outProperties) {

        if (automationCompositionId == null || elementId == null) {
            LOGGER.error("Cannot update Automation composition element state, id is null");
            return;
        }

        var automationComposition = cacheProvider.getAutomationComposition(automationCompositionId);
        if (automationComposition == null) {
            LOGGER.error("Cannot update Automation composition element state, Automation composition id {} not present",
                    automationCompositionId);
            return;
        }

        var element = automationComposition.getElements().get(elementId);
        if (element == null) {
            var msg = "Cannot update Automation composition element state, AC Element id {} not present";
            LOGGER.error(msg, elementId);
            return;
        }
        element.setOperationalState(operationalState);
        element.setUseState(useState);
        element.setOutProperties(outProperties);

        var acInfo = new AutomationCompositionInfo();
        acInfo.setAutomationCompositionId(automationCompositionId);
        acInfo.setDeployState(automationComposition.getDeployState());
        acInfo.setLockState(automationComposition.getLockState());
        acInfo.setElements(List.of(getAutomationCompositionElementInfo(element)));
        var statusMsg = createParticipantStatus();
        statusMsg.setCompositionId(automationComposition.getCompositionId());
        statusMsg.setAutomationCompositionInfoList(List.of(acInfo));
        publisher.sendParticipantStatus(statusMsg);
    }

    /**
     * Get AutomationCompositionElementInfo from AutomationCompositionElement.
     *
     * @param element the AutomationCompositionElement
     * @return the AutomationCompositionElementInfo
     */
    public AutomationCompositionElementInfo getAutomationCompositionElementInfo(AutomationCompositionElement element) {
        var elementInfo = new AutomationCompositionElementInfo();
        elementInfo.setAutomationCompositionElementId(element.getId());
        elementInfo.setDeployState(element.getDeployState());
        elementInfo.setLockState(element.getLockState());
        elementInfo.setOperationalState(element.getOperationalState());
        elementInfo.setUseState(element.getUseState());
        elementInfo.setOutProperties(element.getOutProperties());
        return elementInfo;
    }

    /**
     * Update Composition State for prime and deprime.
     *
     * @param compositionId the composition id
     * @param state the Composition State
     * @param stateChangeResult the indicator if error occurs
     * @param message the message
     */
    public void updateCompositionState(UUID compositionId, AcTypeState state, StateChangeResult stateChangeResult,
            String message) {
        var participantPrimeAck = new ParticipantPrimeAck();
        participantPrimeAck.setCompositionId(compositionId);
        participantPrimeAck.setMessage(message);
        participantPrimeAck.setResult(true);
        participantPrimeAck.setResponseTo(cacheProvider.getMsgIdentification().get(compositionId));
        participantPrimeAck.setCompositionState(state);
        participantPrimeAck.setStateChangeResult(stateChangeResult);
        participantPrimeAck.setParticipantId(cacheProvider.getParticipantId());
        participantPrimeAck.setState(ParticipantState.ON_LINE);
        publisher.sendParticipantPrimeAck(participantPrimeAck);
        cacheProvider.getMsgIdentification().remove(compositionId);
    }

    /**
     * Send Composition Definition Info.
     *
     * @param compositionId the composition id
     * @param elementId the Composition Definition Element id
     * @param outProperties the output Properties Map
     */
    public void sendAcDefinitionInfo(UUID compositionId, ToscaConceptIdentifier elementId,
            Map<String, Object> outProperties) {
        if (compositionId == null) {
            LOGGER.error("Cannot send Composition outProperties, id is null");
            return;
        }
        var statusMsg = createParticipantStatus();
        statusMsg.setCompositionId(compositionId);
        var acElementDefsMap = cacheProvider.getAcElementsDefinitions();
        var acElementsDefinitions = acElementDefsMap.get(compositionId);
        if (acElementsDefinitions == null) {
            LOGGER.error("Cannot send Composition outProperties, id {} is null", compositionId);
            return;
        }
        var acElementDefinition = getAutomationCompositionElementDefinition(acElementsDefinitions, elementId);
        if (acElementDefinition == null) {
            LOGGER.error("Cannot send Composition outProperties, elementId {} not present", elementId);
            return;
        }
        acElementDefinition.setOutProperties(outProperties);
        var participantDefinition = new ParticipantDefinition();
        participantDefinition.setParticipantId(cacheProvider.getParticipantId());
        participantDefinition.setAutomationCompositionElementDefinitionList(List.of(acElementDefinition));
        statusMsg.setParticipantDefinitionUpdates(List.of(participantDefinition));
        publisher.sendHeartbeat(statusMsg);
    }

    private AutomationCompositionElementDefinition getAutomationCompositionElementDefinition(
            Map<ToscaConceptIdentifier, AutomationCompositionElementDefinition> acElementsDefinition,
            ToscaConceptIdentifier elementId) {

        if (elementId == null) {
            if (acElementsDefinition.size() == 1) {
                return acElementsDefinition.values().iterator().next();
            }
            return null;
        }
        return acElementsDefinition.get(elementId);
    }

    private ParticipantStatus createParticipantStatus() {
        var statusMsg = new ParticipantStatus();
        statusMsg.setParticipantId(cacheProvider.getParticipantId());
        statusMsg.setState(ParticipantState.ON_LINE);
        statusMsg.setParticipantSupportedElementType(cacheProvider.getSupportedAcElementTypes());
        return statusMsg;
    }
}