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