c2564887c7e984f67a0e1fadaa3b4ba61b608df2
[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.commissioning;
23
24 import java.util.List;
25 import java.util.UUID;
26 import java.util.stream.Collectors;
27 import javax.ws.rs.core.Response.Status;
28 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
29 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionState;
30 import org.onap.policy.clamp.models.acm.concepts.Participant;
31 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
32 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
33 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
34 import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
35 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
36 import org.onap.policy.common.parameters.BeanValidationResult;
37 import org.onap.policy.models.base.PfModelException;
38 import org.onap.policy.models.base.PfModelRuntimeException;
39 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
41 import org.springframework.stereotype.Service;
42 import org.springframework.transaction.annotation.Transactional;
43
44 /**
45  * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
46  * database to the callers.
47  */
48 @Service
49 @Transactional
50 public class CommissioningProvider {
51     public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
52
53     private final AcDefinitionProvider acDefinitionProvider;
54     private final AutomationCompositionProvider acProvider;
55     private final ParticipantProvider participantProvider;
56     private final SupervisionHandler supervisionHandler;
57
58     /**
59      * Create a commissioning provider.
60      *
61      * @param acDefinitionProvider the ServiceTemplate Provider
62      * @param acProvider the AutomationComposition Provider
63      * @param supervisionHandler the Supervision Handler
64      * @param participantProvider the Participant Provider
65      */
66     public CommissioningProvider(AcDefinitionProvider acDefinitionProvider,
67             AutomationCompositionProvider acProvider, SupervisionHandler supervisionHandler,
68             ParticipantProvider participantProvider) {
69         this.acDefinitionProvider = acDefinitionProvider;
70         this.acProvider = acProvider;
71         this.supervisionHandler = supervisionHandler;
72         this.participantProvider = participantProvider;
73     }
74
75     private CommissioningResponse createCommissioningResponse(UUID compositionId,
76             ToscaServiceTemplate serviceTemplate) {
77         var response = new CommissioningResponse();
78         response.setCompositionId(compositionId);
79         // @formatter:off
80         response.setAffectedAutomationCompositionDefinitions(
81             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
82                 .values()
83                 .stream()
84                 .map(template -> template.getKey().asIdentifier())
85                 .collect(Collectors.toList()));
86         // @formatter:on
87
88         return response;
89     }
90
91     /**
92      * Create automation compositions from a service template.
93      *
94      * @param serviceTemplate the service template
95      * @return the result of the commissioning operation
96      */
97     public CommissioningResponse createAutomationCompositionDefinitions(ToscaServiceTemplate serviceTemplate) {
98
99         if (verifyIfDefinitionExists()) {
100             throw new PfModelRuntimeException(Status.BAD_REQUEST,
101                     "Delete instances, to commission automation composition definitions");
102         }
103         var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
104         serviceTemplate = acmDefinition.getServiceTemplate();
105         var participantList = participantProvider.getParticipants();
106         if (!participantList.isEmpty()) {
107             supervisionHandler.handleSendCommissionMessage(serviceTemplate.getName(), serviceTemplate.getVersion());
108         }
109         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
110     }
111
112     /**
113      * Update Composition Definition.
114      *
115      * @param compositionId The UUID of the automation composition definition to update
116      * @param serviceTemplate the service template
117      * @return the result of the commissioning operation
118      */
119     public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
120
121         var automationCompositions = acProvider.getAutomationCompositions();
122         var result = new BeanValidationResult("AutomationCompositions", automationCompositions);
123         for (var automationComposition : automationCompositions) {
124             if (!AutomationCompositionState.UNINITIALISED.equals(automationComposition.getState())) {
125                 throw new PfModelRuntimeException(Status.BAD_REQUEST,
126                         "There is an Automation Composition instantioation with state in "
127                                 + automationComposition.getState());
128             }
129             result.addResult(AcmUtils.validateAutomationComposition(automationComposition, serviceTemplate));
130         }
131         if (!result.isValid()) {
132             throw new PfModelRuntimeException(Status.BAD_REQUEST, "Service template non valid: " + result.getMessage());
133         }
134
135         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
136
137         return createCommissioningResponse(compositionId, serviceTemplate);
138     }
139
140     /**
141      * Delete the automation composition definition with the given name and version.
142      *
143      * @param compositionId The UUID of the automation composition definition to delete
144      * @return the result of the deletion
145      */
146     public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
147
148         if (verifyIfInstanceExists()) {
149             throw new PfModelRuntimeException(Status.BAD_REQUEST,
150                     "Delete instances, to commission automation composition definitions");
151         }
152         List<Participant> participantList = participantProvider.getParticipants();
153         if (!participantList.isEmpty()) {
154             supervisionHandler.handleSendDeCommissionMessage();
155         }
156         var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
157         return createCommissioningResponse(compositionId, serviceTemplate);
158     }
159
160     /**
161      * Get automation composition definition.
162      *
163      * @param acName the name of the automation composition, null for all
164      * @param acVersion the version of the automation composition, null for all
165      * @return automation composition definition
166      * @throws PfModelException on errors getting automation composition definitions
167      */
168     @Transactional(readOnly = true)
169     public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
170
171         var result = new ToscaServiceTemplates();
172         result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
173         return result;
174     }
175
176     /**
177      * Get automation composition definition.
178      *
179      * @param compositionId the compositionId
180      * @return automation composition definition
181      */
182     @Transactional(readOnly = true)
183     public ToscaServiceTemplate getAutomationCompositionDefinitions(UUID compositionId) {
184
185         return acDefinitionProvider.getAcDefinition(compositionId);
186     }
187
188     /**
189      * Validates to see if there is any instance saved.
190      *
191      * @return true if exists instance
192      */
193     private boolean verifyIfInstanceExists() {
194         return !acProvider.getAutomationCompositions().isEmpty();
195     }
196
197     /**
198      * Validates to see if there is any instance saved.
199      *
200      * @return true if exists instance
201      */
202     private boolean verifyIfDefinitionExists() {
203         return !acDefinitionProvider.getAllServiceTemplates().isEmpty();
204     }
205 }