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