df1f0ead41334dc4292e6e96382dcd9aefaa218f
[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         var serviceTemplates = acDefinitionProvider.getAllServiceTemplates();
130         if (serviceTemplates.isEmpty()) {
131             result.addResult(new ObjectValidationResult("ServiceTemplate", "", ValidationStatus.INVALID,
132                     "Commissioned automation composition definition not found"));
133             return result;
134         }
135
136         var serviceTemplate = acDefinitionProvider.getAllServiceTemplates().get(0);
137
138         for (var automationComposition : automationCompositions.getAutomationCompositionList()) {
139             result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate));
140         }
141         return result;
142     }
143
144     /**
145      * Delete the automation composition with the given name and version.
146      *
147      * @param name the name of the automation composition to delete
148      * @param version the version of the automation composition to delete
149      * @return the result of the deletion
150      * @throws PfModelException on deletion errors
151      */
152     public InstantiationResponse deleteAutomationComposition(String name, String version) throws PfModelException {
153         var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(name, version);
154         if (automationCompositionOpt.isEmpty()) {
155             throw new PfModelException(Response.Status.NOT_FOUND, "Automation composition not found");
156         }
157         var automationComposition = automationCompositionOpt.get();
158         if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
159             throw new PfModelException(Response.Status.BAD_REQUEST,
160                 "Automation composition state is still " + automationComposition.getState());
161         }
162         var response = new InstantiationResponse();
163         response.setAffectedAutomationCompositions(
164             List.of(automationCompositionProvider.deleteAutomationComposition(name, version).getKey().asIdentifier()));
165         return response;
166     }
167
168     /**
169      * Get the requested automation compositions.
170      *
171      * @param name the name of the automation composition to get, null for all automation compositions
172      * @param version the version of the automation composition to get, null for all automation compositions
173      * @return the automation compositions
174      * @throws PfModelException on errors getting automation compositions
175      */
176     @Transactional(readOnly = true)
177     public AutomationCompositions getAutomationCompositions(String name, String version) throws PfModelException {
178         var automationCompositions = new AutomationCompositions();
179         automationCompositions
180             .setAutomationCompositionList(automationCompositionProvider.getAutomationCompositions(name, version));
181
182         return automationCompositions;
183     }
184
185     /**
186      * Issue a command to automation compositions, setting their ordered state.
187      *
188      * @param command the command to issue to automation compositions
189      * @return the result of the initiation command
190      * @throws PfModelException on errors setting the ordered state on the automation compositions
191      * @throws AutomationCompositionException on ordered state invalid
192      */
193     public InstantiationResponse issueAutomationCompositionCommand(InstantiationCommand command)
194         throws AutomationCompositionException, PfModelException {
195
196         if (command.getOrderedState() == null) {
197             throw new AutomationCompositionException(Status.BAD_REQUEST,
198                 "ordered state invalid or not specified on command");
199         }
200
201         var participants = participantProvider.getParticipants();
202         if (participants.isEmpty()) {
203             throw new AutomationCompositionException(Status.BAD_REQUEST, "No participants registered");
204         }
205         var validationResult = new BeanValidationResult("InstantiationCommand", command);
206         List<AutomationComposition> automationCompositions =
207             new ArrayList<>(command.getAutomationCompositionIdentifierList().size());
208         for (ToscaConceptIdentifier id : command.getAutomationCompositionIdentifierList()) {
209             var automationCompositionOpt = automationCompositionProvider.findAutomationComposition(id);
210             if (automationCompositionOpt.isEmpty()) {
211                 validationResult.addResult("ToscaConceptIdentifier", id, ValidationStatus.INVALID,
212                     "AutomationComposition with id " + id + " not found");
213             } else {
214                 var automationComposition = automationCompositionOpt.get();
215                 automationComposition.setCascadedOrderedState(command.getOrderedState());
216                 automationCompositions.add(automationComposition);
217             }
218         }
219         if (validationResult.isValid()) {
220             validationResult = validateIssueAutomationCompositions(automationCompositions, participants);
221         }
222         if (!validationResult.isValid()) {
223             throw new PfModelException(Response.Status.BAD_REQUEST, validationResult.getResult());
224         }
225         automationCompositionProvider.saveAutomationCompositions(automationCompositions);
226
227         supervisionHandler.triggerAutomationCompositionSupervision(command.getAutomationCompositionIdentifierList());
228         var response = new InstantiationResponse();
229         response.setAffectedAutomationCompositions(command.getAutomationCompositionIdentifierList());
230
231         return response;
232     }
233
234     private BeanValidationResult validateIssueAutomationCompositions(List<AutomationComposition> automationCompositions,
235         List<Participant> participants) {
236         var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
237
238         Map<ToscaConceptIdentifier, Participant> participantMap = participants.stream()
239             .collect(Collectors.toMap(participant -> participant.getKey().asIdentifier(), Function.identity()));
240
241         for (AutomationComposition automationComposition : automationCompositions) {
242
243             for (var element : automationComposition.getElements().values()) {
244
245                 var subResult = new BeanValidationResult(ENTRY + element.getDefinition().getName(), element);
246                 Participant p = participantMap.get(element.getParticipantId());
247                 if (p == null) {
248                     subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
249                         element.getDefinition().getName(), ValidationStatus.INVALID,
250                         "Participant with ID " + element.getParticipantId() + " is not registered"));
251                 } else if (!p.getParticipantType().equals(element.getParticipantType())) {
252                     subResult.addResult(new ObjectValidationResult(AUTOMATION_COMPOSITION_NODE_ELEMENT_TYPE,
253                         element.getDefinition().getName(), ValidationStatus.INVALID,
254                         "Participant with ID " + element.getParticipantType() + " - " + element.getParticipantId()
255                             + " is not registered"));
256                 }
257                 result.addResult(subResult);
258             }
259
260         }
261
262         return result;
263     }
264 }