aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/BaseBusinessLogic.java
blob: d1df40c091b5014578e8d9e08eb159cac6ecfd08 (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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
/*-
 * ============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.impl;

import static org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR;

import com.google.gson.JsonElement;
import fj.data.Either;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.config.BeEcompErrorManager;
import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
import org.openecomp.sdc.be.datatypes.elements.PropertyRule;
import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstInputsMap;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.IComplexDefaultValue;
import org.openecomp.sdc.be.model.IPropertyInputCommon;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.PolicyDefinition;
import org.openecomp.sdc.be.model.PropertyConstraint;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.StorageException;
import org.openecomp.sdc.be.model.operations.api.IElementOperation;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.AttributeOperation;
import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
import org.openecomp.sdc.be.model.tosca.ToscaType;
import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
import org.openecomp.sdc.be.model.tosca.validators.PropertyTypeValidator;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.ArtifactTypeEnum;
import org.openecomp.sdc.common.datastructure.Wrapper;
import org.openecomp.sdc.common.log.wrappers.Logger;
import org.openecomp.sdc.exception.ResponseFormat;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseBusinessLogic {

    private static final String FAILED_TO_LOCK_COMPONENT_ERROR = "Failed to lock component {} error - {}";

    private static final Logger log = Logger.getLogger(BaseBusinessLogic.class);
    private static final String EMPTY_VALUE = null;
    private static final String SCHEMA_DOESN_T_EXISTS_FOR_PROPERTY_OF_TYPE = "Schema doesn't exists for property of type {}";
    private static final String PROPERTY_IN_SCHEMA_DEFINITION_INSIDE_PROPERTY_OF_TYPE_DOESN_T_EXIST = "Property in Schema Definition inside property of type {} doesn't exist";
    private static final String ADD_PROPERTY_VALUE = "Add property value";
    private static final String THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID = "The value {} of property from type {} is invalid";
    protected IGroupTypeOperation groupTypeOperation;
    protected InterfaceOperation interfaceOperation;
    protected IElementOperation elementDao;
    protected ComponentsUtils componentsUtils;
    protected UserBusinessLogic userAdmin;
    protected IGraphLockOperation graphLockOperation;
    protected JanusGraphDao janusGraphDao;
    protected JanusGraphGenericDao janusGraphGenericDao;
    protected PropertyOperation propertyOperation;
    protected AttributeOperation attributeOperation;
    protected ApplicationDataTypeCache applicationDataTypeCache;
    protected ToscaOperationFacade toscaOperationFacade;
    protected ApplicationDataTypeCache dataTypeCache;
    protected IGroupOperation groupOperation;
    protected IGroupInstanceOperation groupInstanceOperation;
    protected InterfaceLifecycleOperation interfaceLifecycleTypeOperation;
    protected PolicyTypeOperation policyTypeOperation;
    protected ArtifactsOperations artifactToscaOperation;
    protected UserValidations userValidations;

    DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();

    public BaseBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation,
        IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation,
        InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) {
        this.elementDao = elementDao;
        this.groupOperation = groupOperation;
        this.groupInstanceOperation = groupInstanceOperation;
        this.groupTypeOperation = groupTypeOperation;
        this.interfaceOperation = interfaceOperation;
        this.interfaceLifecycleTypeOperation = interfaceLifecycleTypeOperation;
        this.artifactToscaOperation = artifactToscaOperation;
    }

    @Autowired
    public void setUserAdmin(UserBusinessLogic userAdmin) {
        this.userAdmin = userAdmin;
    }

    @Autowired
    public void setUserValidations(UserValidations userValidations) {
        this.userValidations = userValidations;
    }

    @Autowired
    public void setComponentsUtils(ComponentsUtils componentsUtils) {
        this.componentsUtils = componentsUtils;
    }

    @Autowired
    public void setJanusGraphDao(JanusGraphDao janusGraphDao) {
        this.janusGraphDao = janusGraphDao;
    }

    @Autowired
    public void setApplicationDataTypeCache(ApplicationDataTypeCache applicationDataTypeCache) {
        this.applicationDataTypeCache = applicationDataTypeCache;
    }

    @Autowired
    public void setJanusGraphGenericDao(JanusGraphGenericDao janusGraphGenericDao) {
        this.janusGraphGenericDao = janusGraphGenericDao;
    }

    @Autowired
    public void setGraphLockOperation(IGraphLockOperation graphLockOperation) {
        this.graphLockOperation = graphLockOperation;
    }

    @Autowired
    public void setToscaOperationFacade(ToscaOperationFacade toscaOperationFacade) {
        this.toscaOperationFacade = toscaOperationFacade;
    }

    @Autowired
    void setPolicyTypeOperation(PolicyTypeOperation policyTypeOperation) {
        this.policyTypeOperation = policyTypeOperation;
    }

    @Autowired
    public void setDataTypeCache(ApplicationDataTypeCache dataTypeCache) {
        this.dataTypeCache = dataTypeCache;
    }

    @Autowired
    public void setPropertyOperation(PropertyOperation propertyOperation) {
        this.propertyOperation = propertyOperation;
    }

    @Autowired
    public void setAttributeOperation(AttributeOperation attributeOperation) {
        this.attributeOperation = attributeOperation;
    }

    User validateUserNotEmpty(User user, String ecompErrorContext) {
        return userValidations.validateUserNotEmpty(user, ecompErrorContext);
    }

    protected User validateUserExists(String userId) {
        return userValidations.validateUserExists(userId);
    }

    public User validateUserExists(User user) {
        return userValidations.validateUserExists(user);
    }

    ActionStatus validateUserExistsActionStatus(String userId) {
        return userValidations.validateUserExistsActionStatus(userId);
    }

    protected void validateUserRole(User user, List<Role> roles) {
        userValidations.validateUserRole(user, roles);
    }

    protected void lockComponent(Component component, String ecompErrorContext) {
        lockComponent(component.getUniqueId(), component, ecompErrorContext);
    }

    protected boolean isVolumeGroup(List<String> artifactsInGroup,List <ArtifactDefinition> deploymentArtifacts) {
        for (String artifactId : artifactsInGroup) {
            ArtifactDefinition artifactDef = ArtifactUtils.findArtifactInList(deploymentArtifacts, artifactId);
            if (artifactDef != null
                    && artifactDef.getArtifactType().equalsIgnoreCase(ArtifactTypeEnum.HEAT_VOL.getType())) {
                return true;
            }
        }
        return false;
    }

    protected void lockComponent(Component component, boolean shouldLock, String ecompErrorContext) {
        if(shouldLock){
            lockComponent(component.getUniqueId(), component, ecompErrorContext);
        }
    }

    protected void lockComponent(String componentId, Component component, String ecompErrorContext) {
        ActionStatus lock = lockElement(componentId, component, ecompErrorContext);
        if (lock!= ActionStatus.OK) {
            logAndThrowComponentException(lock, component.getUniqueId(), component.getName());
        }
    }

    protected void lockComponent(String componentId, Component component, boolean needLock, String ecompErrorContext) {
        if (needLock) {
            lockComponent(componentId, component, ecompErrorContext);
        }
    }

    private ResponseFormat logAndThrowComponentException(ActionStatus status, String componentId, String name){
        log.debug(FAILED_TO_LOCK_COMPONENT_ERROR, componentId, status);
        throw new ByActionStatusComponentException(status, name);
    }

    private ActionStatus lockElement(String componentId, Component component, String ecompErrorContext) {
        ComponentTypeEnum componentType = component.getComponentType();
        NodeTypeEnum nodeType = componentType.getNodeType();
        StorageOperationStatus lockResourceStatus = graphLockOperation.lockComponent(componentId, nodeType);

        if (lockResourceStatus == StorageOperationStatus.OK) {
            return ActionStatus.OK;
        } else {
            BeEcompErrorManager.getInstance().logBeFailedLockObjectError(ecompErrorContext, nodeType.getName(), componentId);
            return componentsUtils.convertFromStorageResponse(lockResourceStatus, componentType);
        }
    }

    protected void unlockComponent(boolean failed, Component component, boolean inTransaction) {
        if (component != null) {
            ComponentTypeEnum componentType = component.getComponentType();
            NodeTypeEnum nodeType = componentType.getNodeType();
            if (!inTransaction) {
                if (failed) {
                    janusGraphDao.rollback();
                } else {
                    janusGraphDao.commit();
                }
            }
            // unlock resource
            graphLockOperation.unlockComponent(component.getUniqueId(), nodeType);
        }
        else log.debug("component is NULL");
    }

    protected void unlockComponent(boolean failed, Component component) {
        unlockComponent(failed, component, false);
    }
    void unlockComponentById(boolean failed, String componentId) {
        Either<Component, StorageOperationStatus> component = toscaOperationFacade.getToscaElement(componentId);
        if(component.isLeft()) {
            unlockComponent(failed, component.left().value(), false);
        }
    }

    <T> Boolean validateJsonBody(T bodyObject, Class<T> clazz) {
        if (bodyObject == null) {
            log.debug("Invalid JSON received for object of type {}", clazz.getSimpleName());
            throw new ByActionStatusComponentException(ActionStatus.INVALID_CONTENT);
        } else {
            return true;
        }
    }


    ComponentTypeEnum validateComponentType(String componentType) {
        ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
        if (componentTypeEnum == null) {
            log.debug("Invalid component type {}", componentType);
            throw new ByActionStatusComponentException(ActionStatus.UNSUPPORTED_ERROR, componentType);
        } else {
            return componentTypeEnum;
        }
    }

    Component validateComponentExists(String componentId, ComponentTypeEnum componentType, ComponentParametersView filter) {

        Either<Component, StorageOperationStatus> toscaElement = toscaOperationFacade.getToscaElement(componentId, filter == null ? new ComponentParametersView() : filter);
        if(toscaElement.isRight()){
            handleGetComponentError(componentId, componentType, toscaElement.right().value());
        }
        return validateComponentType(toscaElement.left().value(), componentType);
    }

    private Component validateComponentType(Component cmpt, ComponentTypeEnum componentType) {
        if (componentType != cmpt.getComponentType()) {
            log.debug("component {} is not of requested type {}", cmpt.getUniqueId(), componentType);
            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, componentType));
        }
        return cmpt;
    }

    <T extends PropertyDataDefinition> String updateInputPropertyObjectValue(T property) {
        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypesEither = dataTypeCache.getAll();
        if (allDataTypesEither.isRight()) {
            JanusGraphOperationStatus status = allDataTypesEither.right().value();
            BeEcompErrorManager.getInstance().logInternalFlowError("UpdatePropertyValueOnComponentInstance", "Failed to update property value on instance. Status is " + status, ErrorSeverity.ERROR);
            throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status)));
        }
        Map<String, DataTypeDefinition> allDataTypes = allDataTypesEither.left().value();
        String propertyType = property.getType();
        String innerType = getInnerType(property);
        // Specific Update Logic
        Either<Object, Boolean> isValid =
                propertyOperation.validateAndUpdatePropertyValue(propertyType, (String) property.getValue(), true,
                        innerType, allDataTypes);
        String newValue = property.getValue();
        if (isValid.isRight()) {
            Boolean res = isValid.right().value();
            if (Boolean.FALSE.equals(res)) {
                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
            }
        } else {
            Object object = isValid.left().value();
            if (object != null) {
                newValue = object.toString();
            }
        }
        return newValue;
    }

    <T extends PropertyDataDefinition> String getInnerType(T property){
        ToscaPropertyType type = ToscaPropertyType.isValidType(property.getType());
        log.debug("#getInnerType - The type of the property {} is {}", property.getUniqueId(), property.getType());
        String innerType = null;
        if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
            if (property.getSchema() == null) {
                log.debug(SCHEMA_DOESN_T_EXISTS_FOR_PROPERTY_OF_TYPE, type);
                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE));
            }
            PropertyDataDefinition innerProperty = property.getSchema().getProperty();
            if (innerProperty == null) {
                log.debug(PROPERTY_IN_SCHEMA_DEFINITION_INSIDE_PROPERTY_OF_TYPE_DOESN_T_EXIST, type);
                throw new ByActionStatusComponentException(componentsUtils.convertFromStorageResponse(StorageOperationStatus.INVALID_VALUE));
            }
            innerType = innerProperty.getType();
        }
        return innerType;
    }

    public void validateCanWorkOnComponent(Component component, String userId) {
        ActionStatus actionStatus = ActionStatus.RESTRICTED_OPERATION;
        // verify resource is not archived
        if (component.isArchived() == true){
            actionStatus = ActionStatus.COMPONENT_IS_ARCHIVED;
            throw new ByActionStatusComponentException(actionStatus, component.getName());
        }

        if (component.getLifecycleState() != LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT) {
            log.debug("Component {} is not checked-out", component.getName());
            throw new ByActionStatusComponentException(actionStatus);
        }

        // verify userId is not null
        if (userId == null) {
            log.debug("Current user userId is null");
            throw new ByActionStatusComponentException(actionStatus);
        }

        // verify component last update user is the current user
        String lastUpdaterUserId = component.getLastUpdaterUserId();
        if (!userId.equals(lastUpdaterUserId)) {
            log.debug("Current user is not last updater, last updater userId: {}, current user userId: {}", lastUpdaterUserId, userId);
            throw new ByActionStatusComponentException(actionStatus);
        }

        // verify resource is not deleted
        if (Boolean.TRUE.equals(component.getIsDeleted())) {
            log.debug("Component {} is marked as deleted", component.getUniqueId());
            throw new ByActionStatusComponentException(actionStatus);
        }

    }


    ComponentTypeEnum getComponentTypeByParentComponentType(ComponentTypeEnum parentComponentType) {
        switch (parentComponentType) {
            case SERVICE:
            case RESOURCE:
                return ComponentTypeEnum.RESOURCE;
            case PRODUCT:
                return ComponentTypeEnum.SERVICE;
            default:
                break;
        }
        return null;
    }



    protected Map<String, DataTypeDefinition> getAllDataTypes(ApplicationDataTypeCache applicationDataTypeCache) {
        Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> allDataTypes = applicationDataTypeCache.getAll();
        if (allDataTypes.isRight()) {
            JanusGraphOperationStatus operationStatus = allDataTypes.right().value();
            if (operationStatus == JanusGraphOperationStatus.NOT_FOUND) {
                BeEcompErrorManager.getInstance().logInternalDataError("FetchDataTypes", "Data types are not loaded", ErrorSeverity.ERROR);
                throw new ByActionStatusComponentException(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY);
            } else {
                BeEcompErrorManager.getInstance().logInternalFlowError("FetchDataTypes", "Failed to fetch data types", ErrorSeverity.ERROR);
                throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR);
            }
        }
        return allDataTypes.left().value();
    }

    Either<Boolean, ResponseFormat> validatePropertyDefaultValue(IComplexDefaultValue property, Map<String, DataTypeDefinition> dataTypes) {
        String type;
        String innerType = null;
        if (!propertyOperation.isPropertyTypeValid(property)) {
            log.info("Invalid type for property '{}' type '{}'", property.getName(), property.getType());
            ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_TYPE, property.getType(), property.getName());
            return Either.right(responseFormat);
        }
        type = property.getType();
        if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
            ImmutablePair<String, Boolean> propertyInnerTypeValid = propertyOperation.isPropertyInnerTypeValid(property, dataTypes);
            innerType = propertyInnerTypeValid.getLeft();
            if (!propertyInnerTypeValid.getRight()) {
                log.info("Invalid inner type for property '{}' type '{}', dataTypeCount '{}'", property.getName(), property.getType(), dataTypes.size());
                ResponseFormat responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_PROPERTY_INNER_TYPE, innerType, property.getName());
                return Either.right(responseFormat);
            }
        }
        if (!propertyOperation.isPropertyDefaultValueValid(property, dataTypes)) {
            log.info("Invalid default value for property '{}' type '{}'", property.getName(), property.getType());
            ResponseFormat responseFormat;
            if (type.equals(ToscaPropertyType.LIST.getType()) || type.equals(ToscaPropertyType.MAP.getType())) {
                responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_COMPLEX_DEFAULT_VALUE, property.getName(), type, innerType,
                        property.getDefaultValue());
            } else {
                responseFormat = componentsUtils.getResponseFormat(ActionStatus.INVALID_DEFAULT_VALUE, property.getName(), type,
                        property.getDefaultValue());
            }
            return Either.right(responseFormat);

        }
        return Either.left(true);
    }

    void validateComponentTypeEnum(ComponentTypeEnum componentTypeEnum, String errorContext, Wrapper<ResponseFormat> errorWrapper) {
        if (componentTypeEnum == null) {
            BeEcompErrorManager.getInstance().logInvalidInputError(errorContext, "invalid component type", ErrorSeverity.INFO);
            errorWrapper.setInnerElement(componentsUtils.getResponseFormat(ActionStatus.NOT_ALLOWED));
        }

    }

    protected void validateCanWorkOnComponent(String componentId, ComponentTypeEnum componentTypeEnum, String userId, Wrapper<ResponseFormat> errorWrapper) {
        if (!ComponentValidationUtils.canWorkOnComponent(componentId, toscaOperationFacade, userId)) {
            log.info("Restricted operation for user {} on {} {}", userId, componentTypeEnum.getValue(), componentId);
            errorWrapper.setInnerElement(componentsUtils.getResponseFormat(ActionStatus.RESTRICTED_OPERATION));
        }

    }

    void validateComponentLock(String componentId, ComponentTypeEnum componentTypeEnum, Wrapper<ResponseFormat> errorWrapper) {
        StorageOperationStatus lockStatus = graphLockOperation.lockComponent(componentId, componentTypeEnum.getNodeType());
        if (lockStatus != StorageOperationStatus.OK) {
            log.debug("Failed to lock {} {}", componentTypeEnum.getValue(), componentId);
            errorWrapper.setInnerElement(componentsUtils.getResponseFormat(componentsUtils.convertFromStorageResponse(lockStatus)));
        }

    }

    protected ToscaPropertyType getType(String propertyType) {
        return ToscaPropertyType.isValidType(propertyType);
    }

    void commitOrRollback(Either<?, ResponseFormat> result) {
        if (result == null || result.isRight()) {
            log.warn("operation failed. do rollback");
            janusGraphDao.rollback();
        } else {
            log.debug("operation success. do commit");
            janusGraphDao.commit();
        }
    }

    protected Either<Boolean, ResponseFormat> lockComponentByName(String name, Component component, String ecompErrorContext) {
        ComponentTypeEnum componentType = component.getComponentType();
        NodeTypeEnum nodeType = componentType.getNodeType();
        StorageOperationStatus lockResourceStatus = graphLockOperation.lockComponentByName(name, nodeType);

        if (lockResourceStatus == StorageOperationStatus.OK) {
            return Either.left(true);
        } else {
            BeEcompErrorManager.getInstance().logBeFailedLockObjectError(ecompErrorContext, nodeType.getName(), name);
            ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(lockResourceStatus, componentType);
            ResponseFormat responseFormat = componentsUtils.getResponseFormat(actionStatus, component.getName());
            log.debug(FAILED_TO_LOCK_COMPONENT_ERROR, name, actionStatus);
            return Either.right(responseFormat);
        }
    }

    protected Component validateComponentExistsByFilter(String componentId, ComponentTypeEnum componentType, ComponentParametersView componentParametersView) {
        return toscaOperationFacade.getToscaElement(componentId, componentParametersView)
                .left()
                .on(err -> handleGetComponentError(componentId, componentType, err));

    }

    private Component handleGetComponentError(String componentId, ComponentTypeEnum componentType, StorageOperationStatus getComponentError) {
        ActionStatus actionStatus = componentsUtils.convertFromStorageResponse(getComponentError, componentType);
        log.debug("error fetching component with id {}. error status: {}", componentId, getComponentError);
        throw new ByActionStatusComponentException(actionStatus, componentId);
    }

    @SafeVarargs
    static <T extends Enum<T>> boolean enumHasValueFilter(String name, Function<String, T> enumGetter, T... enumValues) {
        T enumFound = enumGetter.apply(name);
        return Arrays.asList(enumValues).contains(enumFound);
    }

    String validatePropValueBeforeCreate(IPropertyInputCommon property, String value, boolean isValidate,
        Map<String, DataTypeDefinition> allDataTypes) {
        String propertyType = property.getType();
        String updatedInnerType = updateInnerType(property);
        Either<Object, Boolean> isValid = validateAndUpdatePropertyValue(propertyType, value, isValidate, updatedInnerType, allDataTypes);
        String newValue = value;
        if (isValid.isRight()) {
            Boolean res = isValid.right().value();
            if (Boolean.FALSE.equals(res)) {
                throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
                        JanusGraphOperationStatus.ILLEGAL_ARGUMENT));
            }
        } else {
            Object object = isValid.left().value();
            if (object != null) {
                newValue = object.toString();
            }
        }
        ImmutablePair<String, Boolean> pair = validateAndUpdateRules(propertyType, property.getRules(), updatedInnerType, allDataTypes, isValidate);
        log.trace("After validateAndUpdateRules. pair = {}", pair);
        if (Boolean.FALSE.equals(pair.getRight())) {
            BeEcompErrorManager.getInstance().logBeInvalidValueError(ADD_PROPERTY_VALUE, pair.getLeft(), property.getName(), propertyType);
            throw new StorageException(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
                    JanusGraphOperationStatus.ILLEGAL_ARGUMENT));
        }
        return newValue;
    }

    private String updateInnerType(IPropertyInputCommon property) {
        ToscaPropertyType type = ToscaPropertyType.isValidType(property.getType());
        if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
            SchemaDefinition def = property.getSchema();
            if (def == null) {
                log.debug(SCHEMA_DOESN_T_EXISTS_FOR_PROPERTY_OF_TYPE, type);
                failOnIllegalArgument();
            }
            PropertyDataDefinition propDef = def.getProperty();
            if (propDef == null) {
                log.debug(PROPERTY_IN_SCHEMA_DEFINITION_INSIDE_PROPERTY_OF_TYPE_DOESN_T_EXIST, type);
                failOnIllegalArgument();
            }
            return propDef.getType();
        }
        return null;
    }

    private void failOnIllegalArgument() {
        throw new ByActionStatusComponentException(
                componentsUtils.convertFromStorageResponse(
                        DaoStatusConverter.convertJanusGraphStatusToStorageStatus(JanusGraphOperationStatus.ILLEGAL_ARGUMENT)));
    }

    public Either<Object, Boolean> validateAndUpdatePropertyValue(String propertyType, String value, boolean isValidate, String innerType, Map<String, DataTypeDefinition> dataTypes) {
        log.trace("Going to validate property value and its type. type = {}, value = {}", propertyType, value);
        ToscaPropertyType type = getType(propertyType);

        if (isValidate) {

            if (type == null) {
                DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
                ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes);
                if (Boolean.FALSE.equals(validateResult.right)) {
                    log.debug(THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID, value, propertyType);
                    return Either.right(false);
                }
                JsonElement jsonElement = validateResult.left;
                String valueFromJsonElement = getValueFromJsonElement(jsonElement);
                return Either.left(valueFromJsonElement);
            }
            log.trace("before validating property type {}", propertyType);
            boolean isValidProperty = isValidValue(type, value, innerType, dataTypes);
            if (!isValidProperty) {
                log.debug(THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID, value, type);
                return Either.right(false);
            }
        }
        Object convertedValue = value;
        if (!isEmptyValue(value) && isValidate) {
            PropertyValueConverter converter = type.getConverter();
            convertedValue = converter.convert(value, innerType, dataTypes);
        }
        return Either.left(convertedValue);
    }

    private ImmutablePair<String, Boolean> validateAndUpdateRules(String propertyType, List<PropertyRule> rules, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean isValidate) {

        if (rules == null || rules.isEmpty()) {
            return ImmutablePair.of(null, true);
        }

        for (PropertyRule rule : rules) {
            String value = rule.getValue();
            Either<Object, Boolean> updateResult = validateAndUpdatePropertyValue(propertyType, value, isValidate, innerType, dataTypes);
            if (updateResult.isRight()) {
                Boolean status = updateResult.right().value();
                if (Boolean.FALSE.equals(status)) {
                    return ImmutablePair.of(value, status);
                }
            } else {
                String newValue = null;
                Object object = updateResult.left().value();
                if (object != null) {
                    newValue = object.toString();
                }
                rule.setValue(newValue);
            }
        }

        return ImmutablePair.of(null, true);
    }

    protected boolean isValidValue(ToscaPropertyType type, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
        if (isEmptyValue(value)) {
            return true;
        }

        PropertyTypeValidator validator = type.getValidator();

        return validator.isValid(value, innerType, dataTypes);
    }

    public boolean isEmptyValue(String value) {
        return value == null;
    }

    protected String getValueFromJsonElement(JsonElement jsonElement) {
        if (jsonElement == null || jsonElement.isJsonNull()) {
            return EMPTY_VALUE;
        }
        if (jsonElement.toString().isEmpty()) {
            return "";
        }
        return jsonElement.toString();
    }

    protected void rollbackWithException(ActionStatus actionStatus, String... params) {
        janusGraphDao.rollback();
        throw new ByActionStatusComponentException(actionStatus, params);
    }

    public  <T extends ToscaDataDefinition> Either<List<T>, ResponseFormat> declareProperties(String userId, String componentId,
                                                                                              ComponentTypeEnum componentTypeEnum, ComponentInstInputsMap componentInstInputsMap) {

        return Either.left(new ArrayList<>());
    }

    public <T extends PropertyDataDefinition> List<PropertyConstraint> setInputConstraint(T inputDefinition) {
        if (StringUtils.isNotBlank(inputDefinition.getParentPropertyType())
                && StringUtils.isNotBlank(inputDefinition.getSubPropertyInputPath())) {
            return setConstraint(inputDefinition);
        }

        return Collections.emptyList();
    }

    private <T extends PropertyDataDefinition> List<PropertyConstraint> setConstraint(T inputDefinition) {
        List<PropertyConstraint> constraints = new ArrayList<>();
        String[] inputPathArr = inputDefinition.getSubPropertyInputPath().split("#");
        if (inputPathArr.length > 1) {
            inputPathArr = ArrayUtils.remove(inputPathArr, 0);
        }

        Map<String, DataTypeDefinition> dataTypeDefinitionMap =
                applicationDataTypeCache.getAll().left().value();

        String propertyType = inputDefinition.getParentPropertyType();

        for (String anInputPathArr : inputPathArr) {
            if (ToscaType.isPrimitiveType(propertyType)) {
                constraints.addAll(
                        dataTypeDefinitionMap.get(propertyType).getConstraints());
            } else if (!ToscaType.isCollectionType(propertyType)) {
                propertyType = setConstraintForComplexType(dataTypeDefinitionMap, propertyType, anInputPathArr,
                        constraints);
            }
        }

        return constraints;
    }

    private String setConstraintForComplexType(Map<String, DataTypeDefinition> dataTypeDefinitionMap,
                                               String propertyType,
                                               String anInputPathArr,
                                               List<PropertyConstraint> constraints) {
        String type = null;
        List<PropertyDefinition> propertyDefinitions =
                dataTypeDefinitionMap.get(propertyType).getProperties();
        for (PropertyDefinition propertyDefinition : propertyDefinitions) {
            if (propertyDefinition.getName().equals(anInputPathArr)) {
                if (ToscaType.isPrimitiveType(propertyDefinition.getType())) {
                    constraints.addAll(propertyDefinition.safeGetConstraints());
                } else {
                    type = propertyDefinition.getType();
                }
                break;
            }
        }

        return type;
    }


    protected void rollbackWithException(StorageException e) {
        janusGraphDao.rollback();
        throw e;
    }

    protected void rollbackWithException(ComponentException e) {
        janusGraphDao.rollback();
        throw e;
    }

    protected void unlockRollbackWithException(Component component, RuntimeException e) {
        janusGraphDao.rollback();
        graphLockOperation.unlockComponent(component.getUniqueId(), component.getComponentType().getNodeType());
        throw e;
    }

    protected void unlockWithCommit(Component component){
        ComponentTypeEnum componentType = component.getComponentType();
        NodeTypeEnum nodeType = componentType.getNodeType();
        janusGraphDao.commit();
        graphLockOperation.unlockComponent(component.getUniqueId(), nodeType);
    }

    protected ComponentInstance componentInstanceException(StorageOperationStatus storageOperationStatus) {
        throw new StorageException(storageOperationStatus);
    }

    protected Component componentException(StorageOperationStatus storageOperationStatus) {
        throw new StorageException(storageOperationStatus);
    }

    protected PolicyDefinition storageExceptionPolicyDefinition(StorageOperationStatus storageOperationStatus) {
        throw new StorageException(storageOperationStatus);
    }

    protected PolicyDefinition componentExceptionPolicyDefinition(ResponseFormat responseFormat) {
        throw new ByResponseFormatComponentException(responseFormat);
    }

    protected Component componentException(ResponseFormat responseFormat) {
        throw new ByResponseFormatComponentException(responseFormat);
    }

    protected List<ComponentInstanceProperty> componentInstancePropertyListException(StorageOperationStatus storageOperationStatus) {
        throw new StorageException(storageOperationStatus);
    }

    protected Component getComponent(final String componentId) throws BusinessLogicException {

        final Either<Component, StorageOperationStatus> result = toscaOperationFacade.getToscaElement(componentId);

        if (result.isRight()) {
            final StorageOperationStatus errorStatus = result.right().value();
            log.error(BUSINESS_PROCESS_ERROR, this.getClass().getName(),
                "Failed to fetch component information by component id, error {}", errorStatus);
            throw new BusinessLogicException(componentsUtils.getResponseFormat(
                componentsUtils.convertFromStorageResponse(errorStatus)));
        }
        return result.left().value();
    }

}