2601a233b9d94b7d3a4d0348389bbfd022ec7ce5
[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.ArrayList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.function.Function;
28 import java.util.stream.Collectors;
29 import javax.ws.rs.core.Response;
30 import javax.ws.rs.core.Response.Status;
31 import lombok.AllArgsConstructor;
32 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
33 import org.onap.policy.clamp.common.acm.exception.AutomationCompositionException;
34 import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
35 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
36 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
37 import org.onap.policy.clamp.models.acm.concepts.Participant;
38 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
39 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
40 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
41 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
42 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
43 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
44 import org.onap.policy.common.parameters.BeanValidationResult;
45 import org.onap.policy.common.parameters.ObjectValidationResult;
46 import org.onap.policy.common.parameters.ValidationStatus;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.springframework.stereotype.Service;
50 import org.springframework.transaction.annotation.Transactional;
51
52 /**
53  * This class is dedicated to the Instantiation of Commissioned automation composition.
54  */
55 @Service
56 @Transactional
57 @AllArgsConstructor
58 public class AutomationCompositionInstantiationProvider {
59     private static final String AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE = "AutomationCompositionElement";
60
61     private final AutomationCompositionProvider automationCompositionProvider;
62     private final SupervisionHandler supervisionHandler;
63     private final ParticipantProvider participantProvider;
64     private final AcDefinitionProvider acDefinitionProvider;
65     private static final String ENTRY = "entry ";
66
67     /**
68      * Create automation compositions.
69      *
70      * @param automationCompositions the automation composition
71      * @return the result of the instantiation operation
72      * @throws PfModelException on creation errors
73      */
74     public InstantiationResponse createAutomationCompositions(AutomationCompositions automationCompositions)
75         throws PfModelException {
76         for (AutomationComposition automationComposition : automationCompositions.getAutomationCompositionList()) {
77             var checkAutomationCompositionOpt =
78                 automationCompositionProvider.findAutomationComposition(automationComposition.getKey().asIdentifier());
79             if (checkAutomationCompositionOpt.isPresent()) {
80                 throw new PfModelException(Response.Status.BAD_REQUEST,
81                     automationComposition.getKey().asIdentifier() + " already defined");
82             }
83         }
84         BeanValidationResult validationResult = validateAutomationCompositions(automationCompositions);
85         if (!validationResult.isValid()) {
86             throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
87         }
88         automationCompositionProvider.saveAutomationCompositions(automationCompositions.getAutomationCompositionList());
89
90         var response = new InstantiationResponse();
91         response.setAffectedAutomationCompositions(automationCompositions.getAutomationCompositionList().stream()
92             .map(ac -> ac.getKey().asIdentifier()).collect(Collectors.toList()));
93
94         return response;
95     }
96
97     /**
98      * Update automation compositions.
99      *
100      * @param automationCompositions the automation composition
101      * @return the result of the instantiation operation
102      * @throws PfModelException on update errors
103      */
104     public InstantiationResponse updateAutomationCompositions(AutomationCompositions automationCompositions)
105         throws PfModelException {
106         BeanValidationResult validationResult = validateAutomationCompositions(automationCompositions);
107         if (!validationResult.isValid()) {
108             throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
109         }
110         automationCompositionProvider.saveAutomationCompositions(automationCompositions.getAutomationCompositionList());
111
112         var response = new InstantiationResponse();
113         response.setAffectedAutomationCompositions(automationCompositions.getAutomationCompositionList().stream()
114             .map(ac -> ac.getKey().asIdentifier()).collect(Collectors.toList()));
115
116         return response;
117     }
118
119     /**
120      * Validate AutomationCompositions.
121      *
122      * @param automationCompositions AutomationCompositions to validate
123      * @return the result of validation
124      * @throws PfModelException if automationCompositions is not valid
125      */
126     private BeanValidationResult validateAutomationCompositions(AutomationCompositions automationCompositions) {
127
128         var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
129         for (var automationComposition : automationCompositions.getAutomationCompositionList()) {
130             var serviceTemplate = acDefinitionProvider.findAcDefinition(automationComposition.getCompositionId());
131             if (serviceTemplate.isEmpty()) {
132                 result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
133                         "Commissioned automation composition definition not found"));
134             } else {
135                 result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate.get()));
136             }
137         }
138         return result;
139     }
140
141     /**
142      * Delete the automation composition with the given name and version.
143      *
144      * @param name the name of the automation composition to delete
145      * @param version the version of the automation composition to delete
146      * @return the result of the deletion
147      * @throws PfModelException on deletion errors
148      */
149     public InstantiationResponse deleteAutomationComposition(String name, String version) throws PfModelException {
150         var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(name, version);
151         if (automationCompositionOpt.isEmpty()) {
152             throw new PfModelException(Response.Status.NOT_FOUND, "Automation composition not found");
153         }
154         var automationComposition = automationCompositionOpt.get();
155         if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
156             throw new PfModelException(Response.Status.BAD_REQUEST,
157                 "Automation composition state is still " + automationComposition.getState());
158         }
159         var response = new InstantiationResponse();
160         response.setAffectedAutomationCompositions(
161             List.of(automationCompositionProvider.deleteAutomationComposition(name, version).getKey().asIdentifier()));
162         return response;
163     }
164
165     /**
166      * Get the requested automation compositions.
167      *
168      * @param name the name of the automation composition to get, null for all automation compositions
169      * @param version the version of the automation composition to get, null for all automation compositions
170      * @return the automation compositions
171      * @throws PfModelException on errors getting automation compositions
172      */
173     @Transactional(readOnly = true)
174     public AutomationCompositions getAutomationCompositions(String name, String version) throws PfModelException {
175         var automationCompositions = new AutomationCompositions();
176         automationCompositions
177             .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
178
179         return automationCompositions;
180     }
181
182     /**
183      * Issue a command to automation compositions, setting their ordered state.
184      *
185      * @param command the command to issue to automation compositions
186      * @return the result of the initiation command
187      * @throws PfModelException on errors setting the ordered state on the automation compositions
188      * @throws AutomationCompositionException on ordered state invalid
189      */
190     public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
191         throws AutomationCompositionException, PfModelException {
192
193         if (command.getOrderedState() == null) {
194             throw new AutomationCompositionException(Status.BAD_REQUEST,
195                 "ordered state invalid or not specified on command");
196         }
197
198         var participants = participantProvider.getParticipants();
199         if (participants.isEmpty()) {
200             throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
201         }
202         var validationResult = new BeanValidationResult("InstantiationCommand", command);
203         List<AutomationComposition> automationCompositions =
204             new ArrayList<>(command.getAutomationCompositionIdentifierList().size());
205         for (ToscaConceptIdentifier id : command.getAutomationCompositionIdentifierList()) {
206             var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(id);
207             if (automationCompositionOpt.isEmpty()) {
208                 validationResult.addResult("ToscaConceptIdentifier", id, ValidationStatus.INVALID,
209                     "AutomationComposition with id " + id + " not found");
210             } else {
211                 var automationComposition = automationCompositionOpt.get();
212                 automationComposition.setCascadedOrderedState(command.getOrderedState());
213                 automationCompositions.add(automationComposition);
214             }
215         }
216         if (validationResult.isValid()) {
217             validationResult = validateIssueAutomationCompositions(automationCompositions, participants);
218         }
219         if (!validationResult.isValid()) {
220             throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
221         }
222         automationCompositionProvider.saveAutomationCompositions(automationCompositions);
223
224         supervisionHandler.triggerAutomationCompositionSupervision(command.getAutomationCompositionIdentifierList());
225         var response = new InstantiationResponse();
226         response.setAffectedAutomationCompositions(command.getAutomationCompositionIdentifierList());
227
228         return response;
229     }
230
231     private BeanValidationResult validateIssueAutomationCompositions(List<AutomationComposition> automationCompositions,
232         List<Participant> participants) {
233         var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
234
235         Map<ToscaConceptIdentifier, Participant> participantMap = participants.stream()
236             .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
237
238         for (AutomationComposition automationComposition : automationCompositions) {
239
240             for (var element : automationComposition.getElements().values()) {
241
242                 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
243                 Participant p = participantMap.get(element.getParticipantId());
244                 if (p == null) {
245                     subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
246                         element.getDefinition().getName(), ValidationStatus.INVALID,
247                         "Participant with ID " + element.getParticipantId() + " is not registered"));
248                 } else if (!p.getParticipantType().equals(element.getParticipantType())) {
249                     subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
250                         element.getDefinition().getName(), ValidationStatus.INVALID,
251                         "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
252                             + " is not registered"));
253                 }
254                 result.addResult(subResult);
255             }
256
257         }
258
259         return result;
260     }
261 }