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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.clamp.acm.runtime.commissioning;
24 import java.util.UUID;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.stream.Collectors;
28 import javax.ws.rs.core.Response.Status;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.policy.clamp.acm.runtime.supervision.comm.ParticipantPrimePublisher;
31 import org.onap.policy.clamp.models.acm.concepts.AcTypeState;
32 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
33 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.AcTypeStateUpdate;
34 import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
35 import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
36 import org.onap.policy.clamp.models.acm.persistence.provider.AcTypeStateResolver;
37 import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
42 import org.springframework.stereotype.Service;
43 import org.springframework.transaction.annotation.Transactional;
46 * This class provides the create, read and delete actions on Commissioning of automation composition concepts in the
47 * database to the callers.
51 @RequiredArgsConstructor
52 public class CommissioningProvider {
53 public static final String AUTOMATION_COMPOSITION_NODE_TYPE = "org.onap.policy.clamp.acm.AutomationComposition";
55 private final AcDefinitionProvider acDefinitionProvider;
56 private final AutomationCompositionProvider acProvider;
57 private final AcTypeStateResolver acTypeStateResolver;
58 private final ParticipantPrimePublisher participantPrimePublisher;
60 private final ExecutorService executor = Executors.newFixedThreadPool(1);
62 private CommissioningResponse createCommissioningResponse(UUID compositionId,
63 ToscaServiceTemplate serviceTemplate) {
64 var response = new CommissioningResponse();
65 response.setCompositionId(compositionId);
67 response.setAffectedAutomationCompositionDefinitions(
68 serviceTemplate.getToscaTopologyTemplate().getNodeTemplates()
71 .map(template -> template.getKey().asIdentifier())
72 .collect(Collectors.toList()));
79 * Create automation composition from a service template.
81 * @param serviceTemplate the service template
82 * @return the result of the commissioning operation
84 public CommissioningResponse createAutomationCompositionDefinition(ToscaServiceTemplate serviceTemplate) {
86 var acmDefinition = acDefinitionProvider.createAutomationCompositionDefinition(serviceTemplate);
87 serviceTemplate = acmDefinition.getServiceTemplate();
88 return createCommissioningResponse(acmDefinition.getCompositionId(), serviceTemplate);
92 * Update Composition Definition.
94 * @param compositionId The UUID of the automation composition definition to update
95 * @param serviceTemplate the service template
96 * @return the result of the commissioning operation
98 public CommissioningResponse updateCompositionDefinition(UUID compositionId, ToscaServiceTemplate serviceTemplate) {
99 if (verifyIfInstanceExists(compositionId)) {
100 throw new PfModelRuntimeException(Status.BAD_REQUEST,
101 "There are ACM instances, Update of ACM Definition not allowed");
103 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
104 if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
105 throw new PfModelRuntimeException(Status.BAD_REQUEST,
106 "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
108 acDefinitionProvider.updateServiceTemplate(compositionId, serviceTemplate);
110 return createCommissioningResponse(compositionId, serviceTemplate);
114 * Delete the automation composition definition with the given name and version.
116 * @param compositionId The UUID of the automation composition definition to delete
117 * @return the result of the deletion
119 public CommissioningResponse deleteAutomationCompositionDefinition(UUID compositionId) {
120 if (verifyIfInstanceExists(compositionId)) {
121 throw new PfModelRuntimeException(Status.BAD_REQUEST,
122 "Delete instances, to commission automation composition definitions");
124 var acDefinition = acDefinitionProvider.getAcDefinition(compositionId);
125 if (!AcTypeState.COMMISSIONED.equals(acDefinition.getState())) {
126 throw new PfModelRuntimeException(Status.BAD_REQUEST,
127 "ACM not in COMMISSIONED state, Update of ACM Definition not allowed");
129 var serviceTemplate = acDefinitionProvider.deleteAcDefintion(compositionId);
130 return createCommissioningResponse(compositionId, serviceTemplate);
134 * Get automation composition definition.
136 * @param acName the name of the automation composition, null for all
137 * @param acVersion the version of the automation composition, null for all
138 * @return automation composition definition
139 * @throws PfModelException on errors getting automation composition definitions
141 @Transactional(readOnly = true)
142 public ToscaServiceTemplates getAutomationCompositionDefinitions(String acName, String acVersion) {
144 var result = new ToscaServiceTemplates();
145 result.setServiceTemplates(acDefinitionProvider.getServiceTemplateList(acName, acVersion));
150 * Get automation composition definition.
152 * @param compositionId the compositionId
153 * @return automation composition definition
155 @Transactional(readOnly = true)
156 public AutomationCompositionDefinition getAutomationCompositionDefinition(UUID compositionId) {
158 return acDefinitionProvider.getAcDefinition(compositionId);
162 * Validates to see if there is any instance saved.
164 * @return true if exists instance
166 private boolean verifyIfInstanceExists(UUID compositionId) {
167 return !acProvider.getAcInstancesByCompositionId(compositionId).isEmpty();
171 * Composition Definition Priming.
173 * @param compositionId the compositionId
174 * @param acTypeStateUpdate the ACMTypeStateUpdate
176 public void compositionDefinitionPriming(UUID compositionId, AcTypeStateUpdate acTypeStateUpdate) {
177 if (verifyIfInstanceExists(compositionId)) {
178 throw new PfModelRuntimeException(Status.BAD_REQUEST, "There are instances, Priming/Depriming not allowed");
180 var acmDefinition = acDefinitionProvider.getAcDefinition(compositionId);
181 var stateOrdered = acTypeStateResolver.resolve(acTypeStateUpdate.getPrimeOrder(), acmDefinition.getState());
182 switch (stateOrdered) {
184 prime(acmDefinition);
188 deprime(acmDefinition);
193 throw new PfModelRuntimeException(Status.BAD_REQUEST, "Not valid " + acTypeStateUpdate.getPrimeOrder());
197 private void prime(AutomationCompositionDefinition acmDefinition) {
198 var prearation = participantPrimePublisher.prepareParticipantPriming(acmDefinition);
199 acDefinitionProvider.updateAcDefinition(acmDefinition);
202 () -> participantPrimePublisher.sendPriming(prearation, acmDefinition.getCompositionId(), null));
205 private void deprime(AutomationCompositionDefinition acmDefinition) {
206 if (!AcTypeState.COMMISSIONED.equals(acmDefinition.getState())) {
207 for (var elementState : acmDefinition.getElementStateMap().values()) {
208 elementState.setState(AcTypeState.DEPRIMING);
210 acmDefinition.setState(AcTypeState.DEPRIMING);
211 acDefinitionProvider.updateAcDefinition(acmDefinition);
213 executor.execute(() -> participantPrimePublisher.sendDepriming(acmDefinition.getCompositionId()));