Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / validation / ComponentValidations.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components.validation;
24
25 import org.apache.commons.collections.CollectionUtils;
26 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
27 import org.openecomp.sdc.be.dao.api.ActionStatus;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
30 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
31 import org.openecomp.sdc.be.model.Component;
32 import org.openecomp.sdc.be.model.ComponentInstance;
33 import org.openecomp.sdc.be.model.ComponentParametersView;
34 import org.openecomp.sdc.be.model.GroupDefinition;
35 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
36 import org.openecomp.sdc.be.model.operations.StorageException;
37 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
38 import org.openecomp.sdc.be.model.operations.utils.ComponentValidationUtils;
39 import org.openecomp.sdc.common.util.ValidationUtils;
40
41 import java.util.HashSet;
42 import java.util.List;
43 import java.util.Optional;
44 import java.util.Set;
45
46 import static java.util.stream.Collectors.toList;
47
48 @org.springframework.stereotype.Component
49 public class ComponentValidations {
50
51     private final ToscaOperationFacade toscaOperationFacade;
52
53     public ComponentValidations(ToscaOperationFacade toscaOperationFacade) {
54         this.toscaOperationFacade = toscaOperationFacade;
55     }
56
57     public Optional<ComponentInstance> getComponentInstance(Component component, String instanceId) {
58         return component.getComponentInstances()
59                 .stream()
60                 .filter(ci -> ci.getUniqueId().equals(instanceId))
61                 .findFirst();
62     }
63
64     public static boolean validateComponentInstanceExist(Component component, String instanceId) {
65         return Optional.ofNullable(component.getComponentInstances())
66                        .map(componentInstances -> componentInstances.stream().map(ComponentInstance::getUniqueId).collect(toList()))
67                        .filter(instancesIds -> instancesIds.contains(instanceId))
68                        .isPresent();
69     }
70
71     public static String getNormalizedName(ToscaDataDefinition toscaDataDefinition) {
72         String name = (String) toscaDataDefinition.getToscaPresentationValue(JsonPresentationFields.NAME);
73         return org.openecomp.sdc.common.util.ValidationUtils.normalizeComponentInstanceName(name);
74     }
75
76     /**
77      * The following logic is applied:
78      * For each name new or existing name we look at the normalized name which is used in Tosca representation
79      * @param currentName
80      * @param newName
81      * @param component
82      * @return True is new name can be used in this component, false otherwise
83      */
84     public static boolean validateNameIsUniqueInComponent(String currentName, String newName, Component component) {
85         String normalizedCurrentName = ValidationUtils.normalizeComponentInstanceName(currentName);
86         String normalizedNewName = ValidationUtils.normalizeComponentInstanceName(newName);
87         
88         if (normalizedCurrentName.equals(normalizedNewName)) {
89             return true;    //As it's same entity, still considered unique
90         }
91         List<GroupDefinition> groups = component.getGroups();
92         List<ComponentInstance> componentInstances = component.getComponentInstances();
93         Set<String> existingNames = new HashSet<>();
94         if (CollectionUtils.isNotEmpty(groups)) {
95             List<String> existingGroupNames = groups
96                     .stream()
97                     .map(ComponentValidations::getNormalizedName)
98                     .collect(toList());
99             existingNames.addAll(existingGroupNames);
100         }
101         if (CollectionUtils.isNotEmpty(componentInstances)) {
102             List<String> existingInstanceNames = componentInstances
103                     .stream()
104                     .map(ComponentValidations::getNormalizedName)
105                     .collect(toList());
106             existingNames.addAll(existingInstanceNames);
107         }
108         return !existingNames.contains(normalizedNewName);
109     }
110
111     void validateComponentIsCheckedOutByUser(Component component, String userId) {
112         if (!ComponentValidationUtils.canWorkOnComponent(component, userId)) {
113             throw new ByActionStatusComponentException(ActionStatus.ILLEGAL_COMPONENT_STATE, component.getComponentType().name(), component.getName(), component.getLifecycleState().name());
114         }
115     }
116     Component validateComponentIsCheckedOutByUser(String componentId, ComponentTypeEnum componentTypeEnum, String userId) {
117         Component component = getComponent(componentId, componentTypeEnum);
118         validateComponentIsCheckedOutByUser(component, userId);
119         return component;
120     }
121
122     Component getComponent(String componentId, ComponentTypeEnum componentType) {
123         Component component = toscaOperationFacade.getToscaElement(componentId, new ComponentParametersView())
124                 .left()
125                 .on(storageOperationStatus -> onToscaOperationError(storageOperationStatus, componentId));
126
127         validateComponentType(component, componentType);
128
129         return component;
130     }
131
132     private void validateComponentType(Component component, ComponentTypeEnum componentType) {
133         if (componentType!=component.getComponentType()) {
134             throw new ByActionStatusComponentException(ActionStatus.INVALID_RESOURCE_TYPE);
135         }
136     }
137
138     private Component onToscaOperationError(StorageOperationStatus storageOperationStatus, String componentId) {
139         throw new StorageException(storageOperationStatus, componentId);
140     }
141
142 }