29b337edd780f78bc144ae24dac9963f1aac393e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 Nordix Foundation.
4  * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.clamp.acm.runtime.instantiation;
23
24 import java.util.List;
25 import java.util.function.Function;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
29 import lombok.AllArgsConstructor;
30 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
31 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
33 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
35 import org.onap.policy.clamp.models.acm.concepts.Participant;
36 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
37 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
38 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
39 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
40 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
41 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
42 import org.onap.policy.common.parameters.BeanValidationResult;
43 import org.onap.policy.common.parameters.ObjectValidationResult;
44 import org.onap.policy.common.parameters.ValidationStatus;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.springframework.stereotype.Service;
47 import org.springframework.transaction.annotation.Transactional;
48
49 /**
50  * This class is dedicated to the Instantiation of Commissioned automation composition.
51  */
52 @Service
53 @Transactional
54 @AllArgsConstructor
55 public class AutomationCompositionInstantiationProvider {
56     private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
57
58     private final AutomationCompositionProvider automationCompositionProvider;
59     private final SupervisionHandler supervisionHandler;
60     private final ParticipantProvider participantProvider;
61     private final AcDefinitionProvider acDefinitionProvider;
62     private static final String ENTRY = "entry ";
63
64     /**
65      * Create automation composition.
66      *
67      * @param automationComposition the automation composition
68      * @return the result of the instantiation operation
69      */
70     public InstantiationResponse createAutomationComposition(AutomationComposition automationComposition) {
71
72         var checkAutomationCompositionOpt =
73                 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
74         if (checkAutomationCompositionOpt.isPresent()) {
75             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
76                     automationComposition.getKey().asIdentifier() + " already defined");
77         }
78
79         var validationResult = validateAutomationComposition(automationComposition);
80         if (!validationResult.isValid()) {
81             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
82         }
83         automationComposition = automationCompositionProvider.saveAutomationComposition(automationComposition);
84
85         var response = new InstantiationResponse();
86         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
87
88         return response;
89     }
90
91     /**
92      * Update automation composition.
93      *
94      * @param automationComposition the automation composition
95      * @return the result of the instantiation operation
96      */
97     public InstantiationResponse updateAutomationComposition(AutomationComposition automationComposition) {
98         var validationResult = validateAutomationComposition(automationComposition);
99         if (!validationResult.isValid()) {
100             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
101         }
102         automationCompositionProvider.saveAutomationComposition(automationComposition);
103
104         var response = new InstantiationResponse();
105         response.setAffectedAutomationComposition(automationComposition.getKey().asIdentifier());
106
107         return response;
108     }
109
110     /**
111      * Validate AutomationComposition.
112      *
113      * @param automationComposition AutomationComposition to validate
114      * @return the result of validation
115      */
116     private BeanValidationResult validateAutomationComposition(AutomationComposition automationComposition) {
117
118         var result = new BeanValidationResult("AutomationComposition", automationComposition);
119         var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
120         if (serviceTemplate.isEmpty()) {
121             result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
122                     "Commissioned automation composition definition not found"));
123         } else {
124             result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
125         }
126         return result;
127     }
128
129     /**
130      * Delete the automation composition with the given name and version.
131      *
132      * @param name the name of the automation composition to delete
133      * @param version the version of the automation composition to delete
134      * @return the result of the deletion
135      */
136     public InstantiationResponse deleteAutomationComposition(String name, String version) {
137         var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(name, version);
138         if (automationCompositionOpt.isEmpty()) {
139             throw new PfModelRuntimeException(Response.Status.NOT_FOUND, "Automation composition not found");
140         }
141         var automationComposition = automationCompositionOpt.get();
142         if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
143             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
144                     "Automation composition state is still " + automationComposition.getState());
145         }
146         var response = new InstantiationResponse();
147         response.setAffectedAutomationComposition(
148                 automationCompositionProvider.deleteAutomationComposition(name, version).getKey().asIdentifier());
149         return response;
150     }
151
152     /**
153      * Get the requested automation compositions.
154      *
155      * @param name the name of the automation composition to get, null for all automation compositions
156      * @param version the version of the automation composition to get, null for all automation compositions
157      * @return the automation compositions
158      */
159     @Transactional(readOnly = true)
160     public AutomationCompositions getAutomationCompositions(String name, String version) {
161         var automationCompositions = new AutomationCompositions();
162         automationCompositions
163                 .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
164
165         return automationCompositions;
166     }
167
168     /**
169      * Issue a command to automation compositions, setting their ordered state.
170      *
171      * @param command the command to issue to automation compositions
172      * @return the result of the initiation command
173      * @throws AutomationCompositionException on ordered state invalid
174      */
175     public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
176             throws AutomationCompositionException {
177
178         if (command.getOrderedState() == null) {
179             throw new AutomationCompositionException(Status.BAD_REQUEST,
180                     "ordered state invalid or not specified on command");
181         }
182
183         var participants = participantProvider.getParticipants();
184         if (participants.isEmpty()) {
185             throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
186         }
187         var automationCompositionOpt =
188                 automationCompositionProvider.findAutomationComposition(command.getAutomationCompositionIdentifier());
189         if (automationCompositionOpt.isEmpty()) {
190             throw new AutomationCompositionException(Response.Status.BAD_REQUEST,
191                     "AutomationComposition with id " + command.getAutomationCompositionIdentifier() + " not found");
192         }
193
194         var automationComposition = automationCompositionOpt.get();
195         var validationResult = validateIssueAutomationComposition(automationComposition, participants);
196         if (!validationResult.isValid()) {
197             throw new AutomationCompositionException(Response.Status.BAD_REQUEST, validationResult.getResult());
198         }
199
200         automationComposition.setCascadedOrderedState(command.getOrderedState());
201         supervisionHandler.triggerAutomationCompositionSupervision(automationComposition);
202         automationCompositionProvider.saveAutomationComposition(automationComposition);
203         var response = new InstantiationResponse();
204         response.setAffectedAutomationComposition(command.getAutomationCompositionIdentifier());
205
206         return response;
207     }
208
209     private BeanValidationResult validateIssueAutomationComposition(AutomationComposition automationComposition,
210             List<Participant> participants) {
211         var result = new BeanValidationResult("AutomationComposition", automationComposition);
212
213         var participantMap = participants.stream()
214                 .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
215
216         for (var element : automationComposition.getElements().values()) {
217
218             var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
219             var p = participantMap.get(element.getParticipantId());
220             if (p == null) {
221                 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
222                         element.getDefinition().getName(), ValidationStatus.INVALID,
223                         "Participant with ID " + element.getParticipantId() + " is not registered"));
224             } else if (!p.getParticipantType().equals(element.getParticipantType())) {
225                 subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
226                         element.getDefinition().getName(), ValidationStatus.INVALID,
227                         "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
228                                 + " is not registered"));
229             }
230             result.addResult(subResult);
231         }
232
233         return result;
234     }
235 }