aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/lifecycle/LifecycleBusinessLogic.java
blob: befca2480ad72e2f6c4834962a16bb661240d936 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 * Modifications copyright (c) 2019 Nokia
 * ================================================================================
 */

package org.openecomp.sdc.be.components.lifecycle;

import fj.data.Either;
import org.apache.commons.lang3.StringUtils;
import org.openecomp.sdc.be.components.distribution.engine.ServiceDistributionArtifactsBuilder;
import org.openecomp.sdc.be.components.impl.*;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction.LifecycleChanceActionEnum;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElement;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTemplateOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter;
import org.openecomp.sdc.be.model.operations.api.ICacheMangerOperation;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CapabilityOperation;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceCommonInfo;
import org.openecomp.sdc.be.resources.data.auditing.model.ResourceVersionInfo;
import org.openecomp.sdc.be.tosca.ToscaExportHandler;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.common.util.ValidationUtils;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

@org.springframework.stereotype.Component("lifecycleBusinessLogic")
public class LifecycleBusinessLogic {

    private static final String COMMENT = "comment";

    @Autowired
    private IGraphLockOperation graphLockOperation = null;

    @Autowired
    private ArtifactsBusinessLogic artifactsBusinessLogic;

    @Autowired
    private JanusGraphDao janusGraphDao;

    @Autowired
    private CapabilityOperation capabilityOperation;

    private static final Logger log = Logger.getLogger(LifecycleBusinessLogic.class);

    @javax.annotation.Resource
    private ComponentsUtils componentUtils;

    @javax.annotation.Resource
    private ToscaElementLifecycleOperation lifecycleOperation;
    @javax.annotation.Resource
    ArtifactsBusinessLogic artifactsManager;

    @javax.annotation.Resource
    private ServiceDistributionArtifactsBuilder serviceDistributionArtifactsBuilder;

    @javax.annotation.Resource
    private ServiceBusinessLogic serviceBusinessLogic;

    @javax.annotation.Resource
    private ResourceBusinessLogic resourceBusinessLogic;

    @javax.annotation.Resource
    private ProductBusinessLogic productBusinessLogic;

    @Autowired
    private ToscaExportHandler toscaExportUtils;

    @Autowired
    ICacheMangerOperation cacheManagerOperation;

    @Autowired
    ToscaOperationFacade toscaOperationFacade;
    
    @Autowired
    NodeTemplateOperation nodeTemplateOperation;

    private Map<String, LifeCycleTransition> stateTransitions;
    private static volatile boolean isInitialized = false;

    @PostConstruct
    public void init() {
        // init parameters
        if (!isInitialized) {
            synchronized (this) {
                if (!isInitialized) {
                    initStateOperations();
                    isInitialized = true;
                }
            }
        }
    }

    private void initStateOperations() {
        stateTransitions = new HashMap<>();

        LifeCycleTransition checkoutOp = new CheckoutTransition(componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(checkoutOp.getName().name(), checkoutOp);

        UndoCheckoutTransition undoCheckoutOp = new UndoCheckoutTransition(componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        undoCheckoutOp.setArtifactsBusinessLogic(artifactsBusinessLogic);
        stateTransitions.put(undoCheckoutOp.getName().name(), undoCheckoutOp);

        LifeCycleTransition checkinOp = new CheckinTransition(componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(checkinOp.getName().name(), checkinOp);

        LifeCycleTransition certificationRequest = new CertificationRequestTransition(componentUtils, lifecycleOperation, serviceBusinessLogic, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(certificationRequest.getName().name(), certificationRequest);

        LifeCycleTransition startCertification = new StartCertificationTransition(componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(startCertification.getName().name(), startCertification);

        LifeCycleTransition failCertification = new CertificationChangeTransition(LifeCycleTransitionEnum.FAIL_CERTIFICATION, componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(failCertification.getName().name(), failCertification);

        LifeCycleTransition cancelCertification = new CertificationChangeTransition(LifeCycleTransitionEnum.CANCEL_CERTIFICATION, componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        stateTransitions.put(cancelCertification.getName().name(), cancelCertification);

        CertificationChangeTransition successCertification = new CertificationChangeTransition(LifeCycleTransitionEnum.CERTIFY, componentUtils, lifecycleOperation, toscaOperationFacade,
            janusGraphDao);
        successCertification.setArtifactsManager(artifactsBusinessLogic);
        successCertification.setNodeTemplateOperation(nodeTemplateOperation);
        stateTransitions.put(successCertification.getName().name(), successCertification);
    }

    public LifeCycleTransition getLifecycleTransition(LifeCycleTransitionEnum transitionEnum) {
        return stateTransitions.get(transitionEnum.name());
    }

    public Either<Service, ResponseFormat> changeServiceState(String serviceId, User modifier, LifeCycleTransitionEnum transitionEnum, LifecycleChangeInfoWithAction changeInfo, boolean inTransaction, boolean needLock) {
        return (Either<Service, ResponseFormat>) changeComponentState(ComponentTypeEnum.SERVICE, serviceId, modifier, transitionEnum, changeInfo, inTransaction, needLock);
    }

    // TODO: rhalili - should use changeComponentState when possible
    public Either<Resource, ResponseFormat> changeState(String resourceId, User modifier, LifeCycleTransitionEnum transitionEnum, LifecycleChangeInfoWithAction changeInfo, boolean inTransaction, boolean needLock) {
        return (Either<Resource, ResponseFormat>) changeComponentState(ComponentTypeEnum.RESOURCE, resourceId, modifier, transitionEnum, changeInfo, inTransaction, needLock);
    }

    private boolean isComponentVFCMT(Component component, ComponentTypeEnum componentType) {
        if (componentType.equals(ComponentTypeEnum.RESOURCE)) {
            ResourceTypeEnum resourceType = ((ResourceMetadataDataDefinition) component.getComponentMetadataDefinition().getMetadataDataDefinition()).getResourceType();
            if (resourceType.equals(ResourceTypeEnum.VFCMT)) {
                return true;
            }
        }
        return false;
    }

    public Either<? extends Component, ResponseFormat> changeComponentState(ComponentTypeEnum componentType, String componentId, User modifier, LifeCycleTransitionEnum transitionEnum, LifecycleChangeInfoWithAction changeInfo, boolean inTransaction,
            boolean needLock) {

        LifeCycleTransition lifeCycleTransition = stateTransitions.get(transitionEnum.name());
        if (lifeCycleTransition == null) {
            log.debug("state operation is not valid. operations allowed are: {}", LifeCycleTransitionEnum.valuesAsString());
            ResponseFormat error = componentUtils.getInvalidContentErrorAndAudit(modifier, componentId, AuditingActionEnum.CHECKOUT_RESOURCE);
            return Either.right(error);
        }
        Component component = null;
        log.debug("get resource from graph");
        ResponseFormat errorResponse;

        Either<? extends Component, ResponseFormat> eitherResourceResponse = getComponentForChange(componentType, componentId, modifier, lifeCycleTransition, changeInfo);
        if (eitherResourceResponse.isRight()) {
            return eitherResourceResponse;
        }
        component = eitherResourceResponse.left().value();
        String resourceCurrVersion = component.getVersion();
        LifecycleStateEnum resourceCurrState = component.getLifecycleState();

        // lock resource
        if (!inTransaction && needLock) {
            log.debug("lock component {}", componentId);
            Either<Boolean, ResponseFormat> eitherLockResource = lockComponent(componentType, component);
            if (eitherLockResource.isRight()) {
                errorResponse = eitherLockResource.right().value();
                componentUtils.auditComponent(errorResponse, modifier, component, lifeCycleTransition.getAuditingAction(),
                        new ResourceCommonInfo(componentType.getValue()),
                        ResourceVersionInfo.newBuilder()
                                .state(resourceCurrState.name())
                                .version(resourceCurrVersion)
                                .build());


                log.error("lock component {} failed", componentId);
                return Either.right(errorResponse);
            }
            log.debug("after lock component {}", componentId);
        }
        try {
            Either<String, ResponseFormat> commentValidationResult = validateComment(changeInfo, transitionEnum);
            if (commentValidationResult.isRight()) {
                errorResponse = commentValidationResult.right().value();
                componentUtils.auditComponent(errorResponse, modifier, component, lifeCycleTransition.getAuditingAction(),
                        new ResourceCommonInfo(componentType.getValue()),
                        ResourceVersionInfo.newBuilder()
                                .state(resourceCurrState.name())
                                .version(resourceCurrVersion)
                                .build(),
                        changeInfo.getUserRemarks());
                return Either.right(errorResponse);
            }
            changeInfo.setUserRemarks(commentValidationResult.left().value());
            log.debug("after validate component");
            Either<Boolean, ResponseFormat> validateHighestVersion = validateHighestVersion(modifier, lifeCycleTransition, component, resourceCurrVersion, componentType);
            if (validateHighestVersion.isRight()) {
                return Either.right(validateHighestVersion.right().value());
            }
            log.debug("after validate Highest Version");
            if (componentType == ComponentTypeEnum.RESOURCE) {
                Either<? extends Component, ResponseFormat> changeResourceResponse = changeResourceState(componentType, modifier, transitionEnum, changeInfo, true, component);
                if (changeResourceResponse.isRight()) {
                    return changeResourceResponse;
                }
                component = changeResourceResponse.left().value();
            }
            return changeState(component, lifeCycleTransition, componentType, modifier, changeInfo, inTransaction);
        } finally {
            component.setUniqueId(componentId);
            if (!inTransaction && needLock) {
                log.info("unlock component {}", componentId);
                NodeTypeEnum nodeType = componentType.getNodeType();
                log.info("During change state, another component {} has been created/updated", componentId);
                graphLockOperation.unlockComponent(componentId, nodeType);

            }
        }

    }

    /*
     * special case for certification of VFCMT - VFCMT can be certified by Designer or Tester right after checkin in case the operation "submit for test" / "start testing" is done to "VFCMT" - please return error 400
     */
    private Either<? extends Component, ResponseFormat> changeResourceState(ComponentTypeEnum componentType, User modifier, LifeCycleTransitionEnum transitionEnum, LifecycleChangeInfoWithAction changeInfo, boolean inTransaction,
            Component component) {
        LifecycleStateEnum oldState = component.getLifecycleState();
        Component updatedComponent = component;
        if (transitionEnum == LifeCycleTransitionEnum.START_CERTIFICATION || transitionEnum == LifeCycleTransitionEnum.CERTIFICATION_REQUEST) {
            //for VFCMT use old error for backward comp. 
            ActionStatus status = isComponentVFCMT(component, componentType) ? ActionStatus.RESOURCE_VFCMT_LIFECYCLE_STATE_NOT_VALID : ActionStatus.RESOURCE_LIFECYCLE_STATE_NOT_VALID;
            return Either.right(componentUtils.getResponseFormat(status, transitionEnum.getDisplayName()));
        } // certify is done directly from checkin
        else if (transitionEnum == LifeCycleTransitionEnum.CERTIFY) {
            log.debug("Certification request for resource {} ", component.getUniqueId());
            if (oldState == LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
                log.debug("Resource {} is in Checkout state perform checkin", component.getUniqueId());
                Either<? extends Component, ResponseFormat> actionResponse = changeState(component, stateTransitions.get(LifeCycleTransitionEnum.CHECKIN.name()), componentType, modifier, changeInfo, inTransaction);
                if (actionResponse.isRight()) {
                    log.debug("Failed to check in Resource {} error {}", component.getUniqueId(), actionResponse.right().value());
                    return actionResponse;
                }
                updatedComponent = actionResponse.left().value();
                oldState = updatedComponent.getLifecycleState();
            }
            if (oldState == LifecycleStateEnum.NOT_CERTIFIED_CHECKIN) {
                // we will call for submit for testing first and then for certify
                Either<? extends Component, ResponseFormat> actionResponse = changeState(updatedComponent, stateTransitions.get(LifeCycleTransitionEnum.CERTIFICATION_REQUEST.name()), componentType, modifier, changeInfo, inTransaction);
                if (actionResponse.isRight()) {
                    return actionResponse;
                }
                updatedComponent = actionResponse.left().value();
                actionResponse = changeState(updatedComponent, stateTransitions.get(LifeCycleTransitionEnum.START_CERTIFICATION.name()), componentType, modifier, changeInfo, inTransaction);
                if (actionResponse.isRight()) {
                    return actionResponse;
                }
                updatedComponent = actionResponse.left().value();
            }
            if(oldState == LifecycleStateEnum.CERTIFIED){
                failOnAlreadyCertifiedResource(component);
            }
        }
        return Either.left(updatedComponent);
    }

    private void failOnAlreadyCertifiedResource(Component component) {
        String firstName = null;
        String lastName = null;
        if(StringUtils.isNotEmpty(component.getLastUpdaterFullName())){
            String[] fullName = component.getLastUpdaterFullName().split(" ");
            if(fullName.length == 2){
                firstName = fullName[0];
                lastName = fullName[1];
            }
        }
        throw new ByActionStatusComponentException(ActionStatus.COMPONENT_ALREADY_CERTIFIED,
                component.getName(),
                component.getComponentType().name().toLowerCase(),
                firstName,
                lastName,
                component.getLastUpdaterUserId());
    }

    private Either<? extends Component, ResponseFormat> changeState(Component component, LifeCycleTransition lifeCycleTransition, ComponentTypeEnum componentType, User modifier, LifecycleChangeInfoWithAction changeInfo, boolean inTransaction) {
        ResponseFormat errorResponse;

        LifecycleStateEnum oldState = component.getLifecycleState();
        String resourceCurrVersion = component.getVersion();
        ComponentBusinessLogic bl = getComponentBL(componentType);

        Either<User, ResponseFormat> ownerResult = lifeCycleTransition.getComponentOwner(component, componentType);
        if (ownerResult.isRight()) {
            return Either.right(ownerResult.right().value());
        }
        User owner = ownerResult.left().value();
        log.info("owner of resource {} is {}", component.getUniqueId(), owner.getUserId());

        Either<Boolean, ResponseFormat> stateValidationResult = lifeCycleTransition.validateBeforeTransition(component, componentType, modifier, owner, oldState, changeInfo);
        if (stateValidationResult.isRight()) {
            log.error("Failed to validateBeforeTransition");
            errorResponse = stateValidationResult.right().value();
            componentUtils.auditComponent(errorResponse, modifier, component, lifeCycleTransition.getAuditingAction(),
                    new ResourceCommonInfo(componentType.getValue()),
                    ResourceVersionInfo.newBuilder()
                            .version(resourceCurrVersion)
                            .state(oldState.name())
                            .build(),
                    changeInfo.getUserRemarks());
            return Either.right(errorResponse);
        }
        
        Either<? extends Component, ResponseFormat> operationResult = lifeCycleTransition.changeState(componentType, component, bl, modifier, owner, false, inTransaction);

        if (operationResult.isRight()) {
            errorResponse = operationResult.right().value();
            log.info("audit before sending error response");
            componentUtils.auditComponentAdmin(errorResponse, modifier, component, lifeCycleTransition.getAuditingAction(), componentType,
                     ResourceVersionInfo.newBuilder()
                             .state(oldState.name())
                             .version(resourceCurrVersion)
                             .build());

            return Either.right(errorResponse);
        }
        Component resourceAfterOperation = operationResult.left().value();
        componentUtils.auditComponent(componentUtils.getResponseFormat(ActionStatus.OK), modifier, resourceAfterOperation,
                lifeCycleTransition.getAuditingAction(), new ResourceCommonInfo(componentType.getValue()),
                ResourceVersionInfo.newBuilder()
                        .state(oldState.name())
                        .version(resourceCurrVersion)
                        .build(),
                changeInfo.getUserRemarks());
        return operationResult;

    }


	private Either<? extends Component, ResponseFormat> getComponentForChange(ComponentTypeEnum componentType, String componentId, User modifier, LifeCycleTransition lifeCycleTransition, LifecycleChangeInfoWithAction changeInfo) {

        Either<? extends Component, StorageOperationStatus> eitherResourceResponse = toscaOperationFacade.getToscaElement(componentId);

        ResponseFormat errorResponse;
        if (eitherResourceResponse.isRight()) {
            ActionStatus actionStatus = componentUtils.convertFromStorageResponse(eitherResourceResponse.right().value(), componentType);
            errorResponse = componentUtils.getResponseFormat(actionStatus, Constants.EMPTY_STRING);
            log.debug("audit before sending response");
            componentUtils.auditComponent(errorResponse, modifier, lifeCycleTransition.getAuditingAction(),
                    new ResourceCommonInfo(componentId, componentType.getValue()), changeInfo.getUserRemarks());

            return Either.right(errorResponse);
        }
        return Either.left(eitherResourceResponse.left().value());
    }

    private Either<Boolean, ResponseFormat> validateHighestVersion(User modifier, LifeCycleTransition lifeCycleTransition, Component component, String resourceCurrVersion, ComponentTypeEnum componentType) {
        ResponseFormat errorResponse;
        if (!component.isHighestVersion()) {
            log.debug("Component version {} is not the last version of component {}", component.getComponentMetadataDefinition().getMetadataDataDefinition().getVersion(),
                    component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
            errorResponse = componentUtils.getResponseFormat(ActionStatus.COMPONENT_HAS_NEWER_VERSION, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName(), componentType.getValue().toLowerCase());
            componentUtils.auditComponentAdmin(errorResponse, modifier, component, lifeCycleTransition.getAuditingAction(), componentType,
                    ResourceVersionInfo.newBuilder()
                            .state(component.getLifecycleState().name())
                            .version(resourceCurrVersion)
                            .build());
            return Either.right(errorResponse);
        }
        return Either.left(true);
    }

    private Either<Boolean, ResponseFormat> lockComponent(ComponentTypeEnum componentType, Component component) {
        NodeTypeEnum nodeType = componentType.getNodeType();
        StorageOperationStatus lockResourceStatus = graphLockOperation.lockComponent(component.getUniqueId(), nodeType);

        if (lockResourceStatus.equals(StorageOperationStatus.OK)) {
            return Either.left(true);
        } else {
            ActionStatus actionStatus = componentUtils.convertFromStorageResponse(lockResourceStatus);
            ResponseFormat responseFormat = componentUtils.getResponseFormat(actionStatus, component.getComponentMetadataDefinition().getMetadataDataDefinition().getName());
            return Either.right(responseFormat);
        }

    }

    private Either<String, ResponseFormat> validateComment(LifecycleChangeInfoWithAction changeInfo, LifeCycleTransitionEnum transitionEnum) {
        String comment = changeInfo.getUserRemarks();
        if (LifeCycleTransitionEnum.CANCEL_CERTIFICATION == transitionEnum || LifeCycleTransitionEnum.CERTIFY == transitionEnum || LifeCycleTransitionEnum.FAIL_CERTIFICATION == transitionEnum || LifeCycleTransitionEnum.CHECKIN == transitionEnum
                || LifeCycleTransitionEnum.CERTIFICATION_REQUEST == transitionEnum
        // import?
        ) {

            if (!ValidationUtils.validateStringNotEmpty(comment)) {
                log.debug("user comment cannot be empty or null.");
                ResponseFormat errorResponse = componentUtils.getResponseFormat(ActionStatus.MISSING_DATA, COMMENT);
                return Either.right(errorResponse);
            }

            comment = ValidationUtils.removeNoneUtf8Chars(comment);
            comment = ValidationUtils.removeHtmlTags(comment);
            comment = ValidationUtils.normaliseWhitespace(comment);
            comment = ValidationUtils.stripOctets(comment);

            if (!ValidationUtils.validateLength(comment, ValidationUtils.COMMENT_MAX_LENGTH)) {
                log.debug("user comment exceeds limit.");
                return Either.right(componentUtils.getResponseFormat(ActionStatus.EXCEEDS_LIMIT, COMMENT, String.valueOf(ValidationUtils.COMMENT_MAX_LENGTH)));
            }
            if (!ValidationUtils.validateIsEnglish(comment)) {
                return Either.right(componentUtils.getResponseFormat(ActionStatus.INVALID_CONTENT));
            }
        }
        return Either.left(comment);
    }

    private ComponentBusinessLogic getComponentBL(ComponentTypeEnum componentTypeEnum) {
        ComponentBusinessLogic businessLogic;
        switch (componentTypeEnum) {
        case RESOURCE:
            businessLogic = this.resourceBusinessLogic;
            break;
        case SERVICE:
            businessLogic = this.serviceBusinessLogic;
            break;
        case PRODUCT:
            businessLogic = this.productBusinessLogic;
            break;
        default:
            throw new IllegalArgumentException("Illegal component type:" + componentTypeEnum.getValue());
        }
        return businessLogic;
    }

    public Either<Component, ResponseFormat> getLatestComponentByUuid(ComponentTypeEnum componentTypeEnum, String uuid) {

        Either<Component, StorageOperationStatus> latestVersionEither = toscaOperationFacade.getLatestComponentByUuid(uuid);

        if (latestVersionEither.isRight()) {

            return Either.right(componentUtils.getResponseFormat(componentUtils.convertFromStorageResponse(latestVersionEither.right().value(), componentTypeEnum), uuid));
        }

        Component latestComponent = latestVersionEither.left().value();

        return Either.left(latestComponent);
    }

    /**
     * Performs Force certification. Note that a Force certification is allowed for the first certification only, as only a state and a version is promoted due a Force certification, skipping other actions required if a previous certified version
     * exists.
     * 
     * @param resource
     * @param user
     * @param lifecycleChangeInfo
     * @param inTransaction
     * @param needLock
     * @return
     */
    public Either<Resource, ResponseFormat> forceResourceCertification(Resource resource, User user, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean inTransaction, boolean needLock) {
        Either<Resource, ResponseFormat> result = null;
        Either<ToscaElement, StorageOperationStatus> certifyResourceRes = null;
        if (lifecycleChangeInfo.getAction() != LifecycleChanceActionEnum.CREATE_FROM_CSAR) {
            log.debug("Force certification is not allowed for the action {}. ", lifecycleChangeInfo.getAction());
            result = Either.right(componentUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
        }
        if (!isFirstCertification(resource.getVersion())) {
            log.debug("Failed to perform a force certification of resource{}. Force certification is allowed for the first certification only. ", resource.getName());
            result = Either.right(componentUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
        }
        // lock resource
        if (result == null && !inTransaction && needLock) {
            log.info("lock component {}", resource.getUniqueId());
            Either<Boolean, ResponseFormat> eitherLockResource = lockComponent(resource.getComponentType(), resource);
            if (eitherLockResource.isRight()) {
                log.error("lock component {} failed", resource.getUniqueId());
                result = Either.right(eitherLockResource.right().value());
            }
            log.info("after lock component {}", resource.getUniqueId());
        }
        try {
            if (result == null) {
                certifyResourceRes = lifecycleOperation.forceCerificationOfToscaElement(resource.getUniqueId(), user.getUserId(), user.getUserId(), resource.getVersion());
                if (certifyResourceRes.isRight()) {
                    StorageOperationStatus status = certifyResourceRes.right().value();
                    log.debug("Failed to perform a force certification of resource {}. The status is {}. ", resource.getName(), status);
                    result = Either.right(componentUtils.getResponseFormatByResource(componentUtils.convertFromStorageResponse(status), resource));
                }
            }
            if (result == null) {
                result = Either.left(ModelConverter.convertFromToscaElement(certifyResourceRes.left().value()));
            }
        } finally {
            log.info("unlock component {}", resource.getUniqueId());
            if (!inTransaction) {
                if (result.isLeft()) {
                    janusGraphDao.commit();
                } else {
                    janusGraphDao.rollback();
                }
                if (needLock) {
                    NodeTypeEnum nodeType = resource.getComponentType().getNodeType();
                    log.info("During change state, another component {} has been created/updated", resource.getUniqueId());
                    graphLockOperation.unlockComponent(resource.getUniqueId(), nodeType);
                }
            }
        }
        return result;
    }

    public boolean isFirstCertification(String previousVersion) {
        return previousVersion.split("\\.")[0].equals("0");
    }

}