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