[SDC] rebase 1710 code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / lifecycle / CertificationRequestTransition.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.lifecycle;
22
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29 import java.util.Optional;
30 import java.util.Set;
31
32 import org.apache.commons.codec.binary.Base64;
33 import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
34 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
35 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
36 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
40 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
41 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
42 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
45 import org.openecomp.sdc.be.model.ArtifactDefinition;
46 import org.openecomp.sdc.be.model.CapabilityDefinition;
47 import org.openecomp.sdc.be.model.Component;
48 import org.openecomp.sdc.be.model.ComponentInstance;
49 import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
50 import org.openecomp.sdc.be.model.LifecycleStateEnum;
51 import org.openecomp.sdc.be.model.Operation;
52 import org.openecomp.sdc.be.model.RequirementAndRelationshipPair;
53 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
54 import org.openecomp.sdc.be.model.Resource;
55 import org.openecomp.sdc.be.model.Service;
56 import org.openecomp.sdc.be.model.User;
57 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElement;
58 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
59 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaElementLifecycleOperation;
60 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
61 import org.openecomp.sdc.be.model.jsontitan.utils.ModelConverter;
62 import org.openecomp.sdc.be.model.operations.api.ILifecycleOperation;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation;
65 import org.openecomp.sdc.be.model.operations.impl.ResourceOperation;
66 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
67 import org.openecomp.sdc.be.tosca.ToscaError;
68 import org.openecomp.sdc.be.tosca.ToscaExportHandler;
69 import org.openecomp.sdc.be.tosca.ToscaRepresentation;
70 import org.openecomp.sdc.be.tosca.ToscaUtils;
71 import org.openecomp.sdc.be.user.Role;
72 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
73 import org.openecomp.sdc.common.util.ValidationUtils;
74 import org.openecomp.sdc.exception.ResponseFormat;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77
78 import fj.data.Either;
79
80 public class CertificationRequestTransition extends LifeCycleTransition {
81
82         private static Logger log = LoggerFactory.getLogger(CertificationRequestTransition.class.getName());
83
84         private CapabilityOperation capabilityOperation;
85         private ServiceBusinessLogic serviceBusinessLogic;
86         public CertificationRequestTransition(ComponentsUtils componentUtils, ToscaElementLifecycleOperation lifecycleOperation, ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder, ServiceBusinessLogic serviceBusinessLogic,
87                         CapabilityOperation capabilityOperation, ToscaExportHandler toscaExportUtils, ToscaOperationFacade toscaOperationFacade, TitanDao titanDao) {
88                 super(componentUtils, lifecycleOperation, toscaOperationFacade, titanDao);
89
90                 // authorized roles
91                 Role[] resourceServiceCheckoutRoles = { Role.ADMIN, Role.DESIGNER };
92                 // Role[] productCheckoutRoles = {Role.ADMIN, Role.PRODUCT_MANAGER,
93                 // Role.PRODUCT_STRATEGIST};
94                 addAuthorizedRoles(ComponentTypeEnum.RESOURCE, Arrays.asList(resourceServiceCheckoutRoles));
95                 addAuthorizedRoles(ComponentTypeEnum.SERVICE, Arrays.asList(resourceServiceCheckoutRoles));
96                 // TODO to be later defined for product
97                 // addAuthorizedRoles(ComponentTypeEnum.PRODUCT,
98                 // Arrays.asList(productCheckoutRoles));
99                 
100                 //additional authorized roles for resource type
101                 Role[] resourceRoles = { Role.TESTER};
102                 addResouceAuthorizedRoles(ResourceTypeEnum.VFCMT, Arrays.asList(resourceRoles));
103
104                 this.serviceBusinessLogic = serviceBusinessLogic;
105                 this.capabilityOperation = capabilityOperation;
106         }
107
108         @Override
109         public LifeCycleTransitionEnum getName() {
110                 return LifeCycleTransitionEnum.CERTIFICATION_REQUEST;
111         }
112
113         @Override
114         public AuditingActionEnum getAuditingAction() {
115                 return AuditingActionEnum.CERTIFICATION_REQUEST_RESOURCE;
116         }
117
118         protected Either<Boolean, ResponseFormat> validateAllResourceInstanceCertified(Component component) {
119                 Either<Boolean, ResponseFormat> eitherResult = Either.left(true);
120
121                 List<ComponentInstance> resourceInstance = component.getComponentInstances();
122                 if (resourceInstance != null) {
123                         Optional<ComponentInstance> nonCertifiedRIOptional = resourceInstance.stream().filter(p -> !ValidationUtils.validateCertifiedVersion(p.getComponentVersion())).findAny();
124                         // Uncertified Resource Found
125                         if (nonCertifiedRIOptional.isPresent()) {
126                                 ComponentInstance nonCertifiedRI = nonCertifiedRIOptional.get();
127                                 ResponseFormat resFormat = getRelevantResponseFormatUncertifiedRI(nonCertifiedRI, component.getComponentType());
128                                 eitherResult = Either.right(resFormat);
129                         }
130                 }
131                 return eitherResult;
132         }
133
134         private ResponseFormat getRelevantResponseFormatUncertifiedRI(ComponentInstance nonCertifiedRI, ComponentTypeEnum componentType) {
135
136                 ResponseFormat responseFormat;
137                 Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(nonCertifiedRI.getComponentUid());
138                 if (eitherResource.isRight()) {
139
140                         responseFormat = componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR);
141
142                 } else {
143                         ActionStatus actionStatus;
144                         Resource resource = eitherResource.left().value();
145                         Either<Resource, StorageOperationStatus> status = toscaOperationFacade.findLastCertifiedToscaElementByUUID(resource);
146
147                         if (ValidationUtils.validateMinorVersion(nonCertifiedRI.getComponentVersion())) {
148                                 if (status.isRight() || status.left().value() == null) {
149                                         actionStatus = ActionStatus.VALIDATED_RESOURCE_NOT_FOUND;
150                                 } else {
151                                         actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
152                                 }
153                         } else {
154                                 if (status.isRight() || status.left().value() == null)
155                                         actionStatus = ActionStatus.FOUND_LIST_VALIDATED_RESOURCES;
156                                 else {
157                                         actionStatus = ActionStatus.FOUND_ALREADY_VALIDATED_RESOURCE;
158                                 }
159
160                         }
161                         String compType = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : "service";
162                         responseFormat = componentUtils.getResponseFormat(actionStatus, compType, resource.getName());
163                 }
164                 return responseFormat;
165         }
166
167         private Either<ActionStatus, Map<String, ArtifactDefinition>> validateMandatoryArtifactsSupplied(Map<String, ArtifactDefinition> artifacts) {
168
169                 if (artifacts == null || artifacts.isEmpty()) {
170                         return Either.left(ActionStatus.OK);
171                 }
172
173                 Map<String, ArtifactDefinition> invalidArtifacts = new HashMap<>();
174                 for (Entry<String, ArtifactDefinition> artifact : artifacts.entrySet()) {
175
176                         ArtifactDefinition artifactDefinition = artifact.getValue();
177                         if (artifactDefinition.getMandatory()) {
178                                 String artifactEsId = artifactDefinition.getEsId();
179                                 if (artifactEsId == null || artifactEsId.isEmpty()) {
180                                         invalidArtifacts.put(artifact.getKey(), artifactDefinition);
181                                 }
182                         }
183                 }
184
185                 if (invalidArtifacts.isEmpty()) {
186                         return Either.left(ActionStatus.OK);
187                 } else {
188                         return Either.right(invalidArtifacts);
189                 }
190         }
191
192         @Override
193         public Either<? extends Component, ResponseFormat> changeState(ComponentTypeEnum componentType, Component component, ComponentBusinessLogic componentBl, User modifier, User owner, boolean shouldLock, boolean inTransaction) {
194
195                 log.debug("start performing certification request for resource {}", component.getUniqueId());
196
197                 ActionStatus actionStatus;
198                 ResponseFormat responseFormat;
199                 Either<? extends Component, ResponseFormat> result = null;
200                 try{
201                         if (component.getToscaType().equals(ToscaElementTypeEnum.TopologyTemplate.getValue())) {
202         
203                                 Either<Boolean, ResponseFormat> statusCert = validateAllResourceInstanceCertified(component);
204                                 if (statusCert.isRight()) {
205                                         return Either.right(statusCert.right().value());
206                                 }
207         
208                                 statusCert = validateConfiguredAtomicReqCapSatisfied(component);
209                                 if (statusCert.isRight()) {
210                                         return Either.right(statusCert.right().value());
211                                 }
212                         }
213                         if (componentType == ComponentTypeEnum.SERVICE) {
214                         
215                                 Either<Service, ResponseFormat> generateHeatEnvResult = serviceBusinessLogic.generateHeatEnvArtifacts((Service) component, modifier, shouldLock);
216                                                 
217                                 if (generateHeatEnvResult.isRight()) {
218                                                 return Either.right(generateHeatEnvResult.right().value());
219                                         }
220                                 Either<Service, ResponseFormat> generateVfModuleResult = serviceBusinessLogic.generateVfModuleArtifacts((Service) component, modifier, shouldLock);
221                                 if (generateVfModuleResult.isRight()) {
222                                                 return Either.right(generateVfModuleResult.right().value());
223                                 }
224                         }
225         
226                         Either<Either<ArtifactDefinition, Operation>, ResponseFormat> eitherPopulated = componentBl.populateToscaArtifacts(component, modifier, true, inTransaction, shouldLock);
227                         if (eitherPopulated != null && eitherPopulated.isRight()) {
228                                 return Either.right(eitherPopulated.right().value());
229                         }
230         
231                         NodeTypeEnum nodeType = componentType.equals(ComponentTypeEnum.SERVICE) ? NodeTypeEnum.Service : NodeTypeEnum.Resource;
232                         
233                         
234                         Either<ToscaElement, StorageOperationStatus> certificationRequestResult = lifeCycleOperation.requestCertificationToscaElement(component.getUniqueId(), modifier.getUserId(), owner.getUserId());
235                         if (certificationRequestResult.isRight()) {
236                                 log.debug("checkout failed on graph");
237                                 StorageOperationStatus response = certificationRequestResult.right().value();
238                                 actionStatus = componentUtils.convertFromStorageResponse(response);
239         
240                                 if (response.equals(StorageOperationStatus.ENTITY_ALREADY_EXISTS)) {
241                                         actionStatus = ActionStatus.COMPONENT_VERSION_ALREADY_EXIST;
242                                 }
243                                 responseFormat = componentUtils.getResponseFormatByComponent(actionStatus, component, componentType);
244                                 result =  Either.right(responseFormat);
245                         }
246                         else {
247                                 result =  Either.left(ModelConverter.convertFromToscaElement(certificationRequestResult.left().value()));
248                         }
249         } finally {
250                 if (result == null || result.isRight()) {
251                         BeEcompErrorManager.getInstance().logBeDaoSystemError("Change LifecycleState");
252                         if (inTransaction == false) {
253                                 log.debug("operation failed. do rollback");
254                                 titanDao.rollback();
255                         }
256                 } else {
257                         if (inTransaction == false) {
258                                 log.debug("operation success. do commit");
259                                 titanDao.commit();
260                         }
261                 }
262         }
263                 return result;
264         }
265
266         private Either<Boolean, ResponseFormat> validateConfiguredAtomicReqCapSatisfied(Component component) {
267                 log.debug("Submit for testing validation - Start validating configured req/cap satisfied for inner atomic instances, component id:{}", component.getUniqueId());
268                 List<ComponentInstance> componentInstances = component.getComponentInstances();
269                 if (componentInstances != null) {
270                         // Prepare relationships data structures
271                         // Better make it list than set in case we need to count req/cap
272                         // occurrences in the future
273                         Map<String, List<String>> reqName2Ids = new HashMap<>();
274                         Map<String, List<String>> capName2Ids = new HashMap<>();
275 //                      Either<Boolean, ResponseFormat>  parseRelationsForReqCapVerificationRes = parseRelationsForReqCapVerification(component, reqName2Ids, capName2Ids);
276 //                      if(parseRelationsForReqCapVerificationRes.isRight()){
277 //                              return parseRelationsForReqCapVerificationRes;
278 //                      }
279                         Map<String, Set<String>> requirementsToFulfillBeforeCert = configurationManager.getConfiguration().getRequirementsToFulfillBeforeCert();
280                         Map<String, Set<String>> capabilitiesToConsumeBeforeCert = configurationManager.getConfiguration().getCapabilitiesToConsumeBeforeCert();
281                         for (ComponentInstance compInst : componentInstances) {
282                                 String compInstId = compInst.getUniqueId();
283                                 OriginTypeEnum originType = compInst.getOriginType();
284                                 if (originType == null) {
285                                         log.error("Origin type is not set for component instance {} - it shouldn't happen. Skipping this component instance...", compInst.getUniqueId());
286                                         continue;
287                                 }
288                                 String compInstType = originType.getValue();
289                                 // Validating configured requirements fulfilled
290                                 if (null != requirementsToFulfillBeforeCert) {
291                                         Set<String> reqToFulfillForType = requirementsToFulfillBeforeCert.get(compInstType);
292                                         if (reqToFulfillForType != null) {
293                                                 for (String reqNameToFulfill : reqToFulfillForType) {
294                                                         List<String> reqNameList = reqName2Ids.get(reqNameToFulfill);
295                                                         if (reqNameList == null || !reqNameList.contains(compInstId)) {
296                                                                 log.debug("Requirement {} wasn't fulfilled for component instance {} of type {}", reqNameToFulfill, compInstId, compInstType);
297                                                                 ComponentTypeEnum componentType = component.getComponentType();
298                                                                 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
299                                                                 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "requirement",
300                                                                                 reqNameToFulfill, "fulfilled");
301                                                                 return Either.right(responseFormat);
302                                                         }
303                                                 }
304                                         }
305                                 }
306                                 // Validating configured capabilities consumed
307                                 if (null != capabilitiesToConsumeBeforeCert) {
308                                         Set<String> capToConsumeForType = capabilitiesToConsumeBeforeCert.get(compInstType);
309                                         if (capToConsumeForType != null) {
310                                                 for (String capNameToConsume : capToConsumeForType) {
311                                                         List<String> capNameList = capName2Ids.get(capNameToConsume);
312                                                         if (capNameList == null || !capNameList.contains(compInstId)) {
313                                                                 log.debug("Capability {} wasn't consumed for component instance {} of type {}", capNameToConsume, compInstId, compInstType);
314                                                                 ComponentTypeEnum componentType = component.getComponentType();
315                                                                 String compParam = (componentType == ComponentTypeEnum.RESOURCE) ? "VF" : componentType.getValue().toLowerCase();
316                                                                 ResponseFormat responseFormat = componentUtils.getResponseFormat(ActionStatus.REQ_CAP_NOT_SATISFIED_BEFORE_CERTIFICATION, component.getName(), compParam, originType.getDisplayValue(), compInst.getName(), "capability",
317                                                                                 capNameToConsume, "consumed");
318                                                                 return Either.right(responseFormat);
319                                                         }
320                                                 }
321                                         }
322                                 }
323                         }
324                 }
325                 log.debug("Submit for testing validation - validating configured req/cap satisfied for inner atomic instances finished successfully, component id:{}", component.getUniqueId());
326                 return Either.left(true);
327         }
328
329         private Either<Boolean, ResponseFormat> parseRelationsForReqCapVerification(Component component, Map<String, List<String>> reqName2Ids, Map<String, List<String>> capName2Ids) {
330                 log.debug("Submit for testing validation - Preparing relations for inner atomic instances validation");
331                 List<RequirementCapabilityRelDef> componentInstancesRelations = component.getComponentInstancesRelations();
332                 if (componentInstancesRelations != null) {
333                         for (RequirementCapabilityRelDef reqCapRelDef : componentInstancesRelations) {
334                                 List<RequirementAndRelationshipPair> relationships = reqCapRelDef.getRelationships();
335                                 if (relationships != null) {
336                                         for (RequirementAndRelationshipPair reqRelPair : relationships) {
337                                                 String capUniqueId = reqRelPair.getCapabilityUid();
338                                                 Either<CapabilityDefinition, StorageOperationStatus> capability = capabilityOperation.getCapability(capUniqueId);
339                                                 if (capability.isRight()) {
340                                                         log.error("Couldn't fetch capability by id {}", capUniqueId);
341                                                         return Either.right(componentUtils.getResponseFormat(ActionStatus.GENERAL_ERROR));
342                                                 }
343                                                 String reqCapType = capability.left().value().getType();
344                                                 String capabilityOwnerId = reqRelPair.getCapabilityOwnerId();
345                                                 String requirementOwnerId = reqRelPair.getRequirementOwnerId();
346                                                 // Update req
347                                                 List<String> reqIds = reqName2Ids.get(reqCapType);
348                                                 if (reqIds == null) {
349                                                         reqIds = new ArrayList<>();
350                                                         reqName2Ids.put(reqCapType, reqIds);
351                                                 }
352                                                 reqIds.add(requirementOwnerId);
353                                                 // Update cap
354                                                 List<String> capIds = capName2Ids.get(reqCapType);
355                                                 if (capIds == null) {
356                                                         capIds = new ArrayList<>();
357                                                         capName2Ids.put(reqCapType, capIds);
358                                                 }
359                                                 capIds.add(capabilityOwnerId);
360                                         }
361                                 }
362                         }
363                         log.debug("Parsed req for validation: {}, parsed cap for validation: {}", reqName2Ids, capName2Ids);
364                 } else {
365                         log.debug("There are no relations found for component {}", component.getUniqueId());
366                 }
367                 return Either.left(true);
368         }
369
370         @Override
371         public Either<Boolean, ResponseFormat> validateBeforeTransition(Component component, ComponentTypeEnum componentType, User modifier, User owner, LifecycleStateEnum oldState, LifecycleChangeInfoWithAction lifecycleChangeInfo) {
372                 String componentName = component.getComponentMetadataDefinition().getMetadataDataDefinition().getName();
373                 log.debug("validate before certification request. resource name={}, oldState={}, owner userId={}", componentName, oldState, owner.getUserId());
374
375                 // validate user
376                 Either<Boolean, ResponseFormat> userValidationResponse = userRoleValidation(modifier,component, componentType, lifecycleChangeInfo);
377                 if (userValidationResponse.isRight()) {
378                         return userValidationResponse;
379                 }
380
381                 // case of "atomic" checkin and certification request - modifier must be
382                 // the owner
383                 if (oldState.equals(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) && !modifier.equals(owner) && !modifier.getRole().equals(Role.ADMIN.name())) {
384                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
385                         return Either.right(error);
386                 }
387
388                 // other states
389                 if (oldState.equals(LifecycleStateEnum.CERTIFIED)) {
390                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_ALREADY_CERTIFIED, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
391                         return Either.right(error);
392                 }
393                 if (oldState.equals(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS)) {
394                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
395                         return Either.right(error);
396                 }
397                 if (oldState.equals(LifecycleStateEnum.READY_FOR_CERTIFICATION)) {
398                         ResponseFormat error = componentUtils.getResponseFormat(ActionStatus.COMPONENT_SENT_FOR_CERTIFICATION, componentName, componentType.name().toLowerCase(), owner.getFirstName(), owner.getLastName(), owner.getUserId());
399                         return Either.right(error);
400                 }
401
402                 return Either.left(true);
403         }
404 }