450c75564e663ce03df7589640fa728cfa2edbd5
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2023 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 lombok.RequiredArgsConstructor;
28 import org.onap.policy.clamp.acm.runtime.supervision.SupervisionHandler;
29 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
30 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
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.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 @RequiredArgsConstructor
49 public class CommissioningProvider {
50     public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
51
52     private final AcDefinitionProvider acDefinitionProvider;
53     private final AutomationCompositionProvider acProvider;
54     private final SupervisionHandler supervisionHandler;
55     private final ParticipantProvider participantProvider;
56
57     private CommissioningResponse createCommissioningResponse(UUID compositionId,
58             ToscaServiceTemplate serviceTemplate) {
59         var response = new CommissioningResponse();
60         response.setCompositionId(compositionId);
61         // @formatter:off
62         response.setAffectedAutomationCompositionDefinitions(
63             serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
64                 .values()
65                 .stream()
66                 .map(template -> template.getKey().asIdentifier())
67                 .collect(Collectors.toList()));
68         // @formatter:on
69
70         return response;
71     }
72
73     /**
74      * Create automation composition from a service template.
75      *
76      * @param serviceTemplate the service template
77      * @return the result of the commissioning operation
78      */
79     public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
80
81         var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
82         serviceTemplate = acmDefinition.getServiceTemplate();
83         var participantList = participantProvider.getParticipants();
84         if (!participantList.isEmpty()) {
85             supervisionHandler.handleSendCommissionMessage(acmDefinition);
86         }
87         return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
88     }
89
90     /**
91      * Update Composition Definition.
92      *
93      * @param compositionId The UUID of the automation composition definition to update
94      * @param serviceTemplate the service template
95      * @return the result of the commissioning operation
96      */
97     public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
98         if (verifyIfInstanceExists(compositionId)) {
99             throw new PfModelRuntimeException(Status.BAD_REQUEST,
100                     "There are ACM instances, Update of ACM Definition not allowed");
101         }
102         var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
103         if (AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
104             throw new PfModelRuntimeException(Status.BAD_REQUEST,
105                     "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
106         }
107         acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
108
109         return createCommissioningResponse(compositionId, serviceTemplate);
110     }
111
112     /**
113      * Delete the automation composition definition with the given name and version.
114      *
115      * @param compositionId The UUID of the automation composition definition to delete
116      * @return the result of the deletion
117      */
118     public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
119
120         if (verifyIfInstanceExists(compositionId)) {
121             throw new PfModelRuntimeException(Status.BAD_REQUEST,
122                     "Delete instances, to commission automation composition definitions");
123         }
124         var participantList = participantProvider.getParticipants();
125         if (!participantList.isEmpty()) {
126             supervisionHandler.handleSendDeCommissionMessage(compositionId);
127         }
128         var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
129         return createCommissioningResponse(compositionId, serviceTemplate);
130     }
131
132     /**
133      * Get automation composition definition.
134      *
135      * @param acName the name of the automation composition, null for all
136      * @param acVersion the version of the automation composition, null for all
137      * @return automation composition definition
138      * @throws PfModelException on errors getting automation composition definitions
139      */
140     @Transactional(readOnly = true)
141     public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
142
143         var result = new ToscaServiceTemplates();
144         result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
145         return result;
146     }
147
148     /**
149      * Get automation composition definition.
150      *
151      * @param compositionId the compositionId
152      * @return automation composition definition
153      */
154     @Transactional(readOnly = true)
155     public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
156
157         return acDefinitionProvider.getAcDefinition(compositionId);
158     }
159
160     /**
161      * Validates to see if there is any instance saved.
162      *
163      * @return true if exists instance
164      */
165     private boolean verifyIfInstanceExists(UUID compositionId) {
166         return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
167     }
168 }