bcc3fd49840de2616dc191d4e632281f6d562445
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 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.a1pms.handler;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27 import javax.validation.Validation;
28 import javax.validation.ValidationException;
29 import lombok.AccessLevel;
30 import lombok.Getter;
31 import lombok.RequiredArgsConstructor;
32 import lombok.Setter;
33 import org.apache.http.HttpStatus;
34 import org.onap.policy.clamp.acm.participant.a1pms.exception.A1PolicyServiceException;
35 import org.onap.policy.clamp.acm.participant.a1pms.models.ConfigurationEntity;
36 import org.onap.policy.clamp.acm.participant.a1pms.webclient.AcA1PmsClient;
37 import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
38 import org.onap.policy.clamp.acm.participant.intermediary.api.ParticipantIntermediaryApi;
39 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionElement;
40 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionOrderedState;
41 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
42 import org.onap.policy.clamp.models.acm.messages.dmaap.participant.ParticipantMessageType;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.models.base.PfModelException;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.springframework.stereotype.Component;
50
51 /**
52  * This class handles implementation of automationCompositionElement updates.
53  */
54 @Component
55 @RequiredArgsConstructor
56 public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
57
58     private static final Coder CODER = new StandardCoder();
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
61
62     @Setter
63     private ParticipantIntermediaryApi intermediaryApi;
64
65     private final AcA1PmsClient acA1PmsClient;
66
67     // Map of acElement Id and A1PMS services
68     @Getter(AccessLevel.PACKAGE)
69     private final Map<UUID, ConfigurationEntity> configRequestMap = new HashMap<>();
70
71     /**
72      * Handle a automation composition element state change.
73      *
74      * @param automationCompositionId the ID of the automation composition
75      * @param automationCompositionElementId the ID of the automation composition element
76      * @param currentState                   the current state of the automation composition element
77      * @param newState                       the state to which the automation composition element is changing to
78      * @throws PfModelException in case of a model exception
79      */
80     @Override
81     public void automationCompositionElementStateChange(UUID automationCompositionId,
82             UUID automationCompositionElementId, AutomationCompositionState currentState,
83             AutomationCompositionOrderedState newState) throws A1PolicyServiceException {
84         switch (newState) {
85             case UNINITIALISED:
86                 var configurationEntity = configRequestMap.get(automationCompositionElementId);
87                 if (configurationEntity != null && acA1PmsClient.isPmsHealthy()) {
88                     acA1PmsClient.deleteService(configurationEntity.getPolicyServiceEntities());
89                     configRequestMap.remove(automationCompositionElementId);
90                     intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
91                             automationCompositionElementId, newState, AutomationCompositionState.UNINITIALISED,
92                             ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
93                 } else {
94                     LOGGER.warn("Failed to connect with A1PMS. Service configuration is: {}", configurationEntity);
95                     throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
96                             "Unable to connect with A1PMS");
97                 }
98                 break;
99             case PASSIVE:
100                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
101                         automationCompositionElementId, newState, AutomationCompositionState.PASSIVE,
102                         ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
103                 break;
104             case RUNNING:
105                 intermediaryApi.updateAutomationCompositionElementState(automationCompositionId,
106                         automationCompositionElementId, newState, AutomationCompositionState.RUNNING,
107                         ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
108                 break;
109             default:
110                 LOGGER.warn("Cannot transition from state {} to state {}", currentState, newState);
111                 break;
112         }
113     }
114
115     /**
116      * Callback method to handle an update on an automation composition element.
117      *
118      * @param automationCompositionId the ID of the automation composition
119      * @param element      the information on the automation composition element
120      * @param properties properties Map
121      */
122     @Override
123     public void automationCompositionElementUpdate(UUID automationCompositionId,
124             AutomationCompositionElement element, Map<String, Object> properties) throws A1PolicyServiceException {
125         try {
126             var configurationEntity = CODER.convert(properties, ConfigurationEntity.class);
127             var violations =
128                     Validation.buildDefaultValidatorFactory().getValidator().validate(configurationEntity);
129             if (violations.isEmpty()) {
130                 if (acA1PmsClient.isPmsHealthy()) {
131                     acA1PmsClient.createService(configurationEntity.getPolicyServiceEntities());
132                     configRequestMap.put(element.getId(), configurationEntity);
133
134                     intermediaryApi.updateAutomationCompositionElementState(automationCompositionId, element.getId(),
135                             AutomationCompositionOrderedState.PASSIVE, AutomationCompositionState.PASSIVE,
136                             ParticipantMessageType.AUTOMATION_COMPOSITION_STATE_CHANGE);
137                 } else {
138                     LOGGER.error("Failed to connect with A1PMS");
139                     throw new A1PolicyServiceException(HttpStatus.SC_SERVICE_UNAVAILABLE,
140                             "Unable to connect with A1PMS");
141                 }
142             } else {
143                 LOGGER.error("Violations found in the config request parameters: {}", violations);
144                 throw new ValidationException("Constraint violations in the config request");
145             }
146         } catch (ValidationException | CoderException | A1PolicyServiceException e) {
147             throw new A1PolicyServiceException(HttpStatus.SC_BAD_REQUEST, "Invalid Configuration", e);
148         }
149     }
150 }