2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.lifecycle;
23 import fj.data.Either;
24 import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
25 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
27 import org.openecomp.sdc.be.config.BeEcompErrorManager;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
33 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
34 import org.openecomp.sdc.be.impl.ComponentsUtils;
35 import org.openecomp.sdc.be.model.*;
36 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
37 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
38 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
39 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
40 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
41 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
42 import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation;
43 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
44 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
45 import org.openecomp.sdc.be.user.Role;
46 import org.openecomp.sdc.common.util.ValidationUtils;
47 import org.openecomp.sdc.exception.ResponseFormat;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.HashMap;
54 import java.util.List;
56 import java.util.Map.Entry;
57 import java.util.Optional;
60 public class CertificationRequestTransition extends LifeCycleTransition {
62 private static Logger log = LoggerFactory.getLogger(CertificationRequestTransition.class.getName());
64 private CapabilityOperation capabilityOperation;
65 private ServiceBusinessLogic serviceBusinessLogic;
66 public CertificationRequestTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder, ServiceBusinessLogic serviceBusinessLogic,
67 CapabilityOperation capabilityOperation, ToscaExportHandler toscaExportUtils, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
68 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
71 Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
72 // Role[] productCheckoutRoles = {Role.ADMIN, Role.PRODUCT_MANAGER,
73 // Role.PRODUCT_STRATEGIST};
74 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
75 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
76 // TODO to be later defined for product
77 // addAuthorizedRoles(ComponentTypeEnum.PRODUCT,
78 // Arrays.asList(productCheckoutRoles));
80 //additional authorized roles for resource type
81 Role[] resourceRoles = { Role.TESTER};
82 addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
84 this.serviceBusinessLogic = serviceBusinessLogic;
85 this.capabilityOperation = capabilityOperation;
89 public LifeCycleTransitionEnum getName() {
90 return LifeCycleTransitionEnum.CERTIFICATION_REQUEST;
94 public AuditingActionEnum getAuditingAction() {
95 return AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE;
98 protected Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified(Component component) {
99 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
101 List<ComponentInstance> resourceInstance = component.getComponentInstances();
102 if (resourceInstance != null) {
103 Optional<ComponentInstance> nonCertifiedRIOptional = resourceInstance.stream().filter(p -> !ValidationUtils.validateCertifiedVersion(p.getComponentVersion())).findAny();
104 // Uncertified Resource Found
105 if (nonCertifiedRIOptional.isPresent()) {
106 ComponentInstance nonCertifiedRI = nonCertifiedRIOptional.get();
107 ResponseFormat resFormat = getRelevantResponseFormatUncertifiedRI(nonCertifiedRI, component.getComponentType());
108 eitherResult = Either.right(resFormat);
114 private ResponseFormat getRelevantResponseFormatUncertifiedRI(ComponentInstance nonCertifiedRI, ComponentTypeEnum componentType) {
116 ResponseFormat responseFormat;
117 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(nonCertifiedRI.getComponentUid());
118 if (eitherResource.isRight()) {
120 responseFormat = componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
123 ActionStatus actionStatus;
124 Resource resource = eitherResource.left().value();
125 Either<Resource, StorageOperationStatus> status = toscaOperationFacade.findLastCertifiedToscaElementByUUID(resource);
127 if (ValidationUtils.validateMinorVersion(nonCertifiedRI.getComponentVersion())) {
128 if (status.isRight() || status.left().value() == null) {
129 actionStatus = ActionStatus.VALIDATED_RESOURCE_NOT_FOUND;
131 actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
134 if (status.isRight() || status.left().value() == null)
135 actionStatus = ActionStatus.FOUND_LIST_VALIDATED_RESOURCES;
137 actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
141 String compType = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : "service";
142 responseFormat = componentUtils.getResponseFormat(actionStatus, compType, resource.getName());
144 return responseFormat;
147 private Either<ActionStatus, Map<String, ArtifactDefinition>> validateMandatoryArtifactsSupplied(Map<String, ArtifactDefinition> artifacts) {
149 if (artifacts == null || artifacts.isEmpty()) {
150 return Either.left(ActionStatus.OK);
153 Map<String, ArtifactDefinition> invalidArtifacts = new HashMap<>();
154 for (Entry<String, ArtifactDefinition> artifact : artifacts.entrySet()) {
156 ArtifactDefinition artifactDefinition = artifact.getValue();
157 if (artifactDefinition.getMandatory()) {
158 String artifactEsId = artifactDefinition.getEsId();
159 if (artifactEsId == null || artifactEsId.isEmpty()) {
160 invalidArtifacts.put(artifact.getKey(), artifactDefinition);
165 if (invalidArtifacts.isEmpty()) {
166 return Either.left(ActionStatus.OK);
168 return Either.right(invalidArtifacts);
173 public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
175 log.debug("start performing certification request for resource {}", component.getUniqueId());
177 ActionStatus actionStatus;
178 ResponseFormat responseFormat;
179 Either<? extends Component, ResponseFormat> result = null;
181 if (component.getToscaType().equals(ToscaElementTypeEnum.TopologyTemplate.getValue())) {
183 Either<Boolean, ResponseFormat> statusCert = validateAllResourceInstanceCertified(component);
184 if (statusCert.isRight()) {
185 return Either.right(statusCert.right().value());
188 statusCert = validateConfiguredAtomicReqCapSatisfied(component);
189 if (statusCert.isRight()) {
190 return Either.right(statusCert.right().value());
193 if (componentType == ComponentTypeEnum.SERVICE) {
195 Either<Service, ResponseFormat> generateHeatEnvResult = serviceBusinessLogic.generateHeatEnvArtifacts((Service) component, modifier, shouldLock);
197 if (generateHeatEnvResult.isRight()) {
198 return Either.right(generateHeatEnvResult.right().value());
200 Either<Service, ResponseFormat> generateVfModuleResult = serviceBusinessLogic.generateVfModuleArtifacts((Service) component, modifier, shouldLock);
201 if (generateVfModuleResult.isRight()) {
202 return Either.right(generateVfModuleResult.right().value());
206 Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherPopulated = componentBl.populateToscaArtifacts(component, modifier, true, inTransaction, shouldLock);
207 if (eitherPopulated != null && eitherPopulated.isRight()) {
208 return Either.right(eitherPopulated.right().value());
211 NodeTypeEnum nodeType = componentType.equals(ComponentTypeEnum.SERVICE) ? NodeTypeEnum.Service : NodeTypeEnum.Resource;
214 Either<ToscaElement, StorageOperationStatus> certificationRequestResult = lifeCycleOperation.requestCertificationToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
215 if (certificationRequestResult.isRight()) {
216 log.debug("checkout failed on graph");
217 StorageOperationStatus response = certificationRequestResult.right().value();
218 actionStatus = componentUtils.convertFromStorageResponse(response);
220 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
221 actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
223 responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
224 result = Either.right(responseFormat);
227 result = Either.left(ModelConverter.convertFromToscaElement(certificationRequestResult.left().value()));
230 if (result == null || result.isRight()) {
231 BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
232 if (inTransaction == false) {
233 log.debug("operation failed. do rollback");
237 if (inTransaction == false) {
238 log.debug("operation success. do commit");
246 private Either<Boolean, ResponseFormat> validateConfiguredAtomicReqCapSatisfied(Component component) {
247 log.debug("Submit for testing validation - Start validating configured req/cap satisfied for inner atomic instances, component id:{}", component.getUniqueId());
248 List<ComponentInstance> componentInstances = component.getComponentInstances();
249 if (componentInstances != null) {
250 // Prepare relationships data structures
251 // Better make it list than set in case we need to count req/cap
252 // occurrences in the future
253 Map<String, List<String>> reqName2Ids = new HashMap<>();
254 Map<String, List<String>> capName2Ids = new HashMap<>();
255 // Either<Boolean, ResponseFormat> parseRelationsForReqCapVerificationRes = parseRelationsForReqCapVerification(component, reqName2Ids, capName2Ids);
256 // if(parseRelationsForReqCapVerificationRes.isRight()){
257 // return parseRelationsForReqCapVerificationRes;
259 Map<String, Set<String>> requirementsToFulfillBeforeCert = configurationManager.getConfiguration().getRequirementsToFulfillBeforeCert();
260 Map<String, Set<String>> capabilitiesToConsumeBeforeCert = configurationManager.getConfiguration().getCapabilitiesToConsumeBeforeCert();
261 for (ComponentInstance compInst : componentInstances) {
262 String compInstId = compInst.getUniqueId();
263 OriginTypeEnum originType = compInst.getOriginType();
264 if (originType == null) {
265 log.error("Origin type is not set for component instance {} - it shouldn't happen. Skipping this component instance...", compInst.getUniqueId());
268 String compInstType = originType.getValue();
269 // Validating configured requirements fulfilled
270 if (null != requirementsToFulfillBeforeCert) {
271 Set<String> reqToFulfillForType = requirementsToFulfillBeforeCert.get(compInstType);
272 if (reqToFulfillForType != null) {
273 for (String reqNameToFulfill : reqToFulfillForType) {
274 List<String> reqNameList = reqName2Ids.get(reqNameToFulfill);
275 if (reqNameList == null || !reqNameList.contains(compInstId)) {
276 log.debug("Requirement {} wasn't fulfilled for component instance {} of type {}", reqNameToFulfill, compInstId, compInstType);
277 ComponentTypeEnum componentType = component.getComponentType();
278 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
279 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "requirement",
280 reqNameToFulfill, "fulfilled");
281 return Either.right(responseFormat);
286 // Validating configured capabilities consumed
287 if (null != capabilitiesToConsumeBeforeCert) {
288 Set<String> capToConsumeForType = capabilitiesToConsumeBeforeCert.get(compInstType);
289 if (capToConsumeForType != null) {
290 for (String capNameToConsume : capToConsumeForType) {
291 List<String> capNameList = capName2Ids.get(capNameToConsume);
292 if (capNameList == null || !capNameList.contains(compInstId)) {
293 log.debug("Capability {} wasn't consumed for component instance {} of type {}", capNameToConsume, compInstId, compInstType);
294 ComponentTypeEnum componentType = component.getComponentType();
295 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
296 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "capability",
297 capNameToConsume, "consumed");
298 return Either.right(responseFormat);
305 log.debug("Submit for testing validation - validating configured req/cap satisfied for inner atomic instances finished successfully, component id:{}", component.getUniqueId());
306 return Either.left(true);
310 public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
311 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
312 log.debug("validate before certification request. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
315 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
316 if (userValidationResponse.isRight()) {
317 return userValidationResponse;
320 // case of "atomic" checkin and certification request - modifier must be
322 if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
323 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
324 return Either.right(error);
328 if (oldState.equals(LifecycleStateEnum.CERTIFIED)) {
329 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CERTIFIED, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
330 return Either.right(error);
332 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
333 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
334 return Either.right(error);
336 if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION)) {
337 ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
338 return Either.right(error);
341 return Either.left(true);