Support adding data types to model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / UniqueIdBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.model.operations.impl;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.UUID;
25 import org.apache.commons.lang.StringUtils;
26 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
27 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
28 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
29 import org.openecomp.sdc.be.resources.data.ResourceCategoryData;
30 import org.openecomp.sdc.be.resources.data.ServiceCategoryData;
31 import org.openecomp.sdc.be.resources.data.TagData;
32 import org.openecomp.sdc.be.resources.data.UserData;
33 import org.openecomp.sdc.common.api.Constants;
34 import org.openecomp.sdc.common.util.ValidationUtils;
35
36 public class UniqueIdBuilder {
37
38     private static final String HEAT_PARAM_PREFIX = "heat_";
39     private static String DOT = ".";
40     private static UserData userData = new UserData();
41     private static TagData tagData = new TagData();
42     private static ResourceCategoryData resCategoryData = new ResourceCategoryData();
43     private static ServiceCategoryData serCategoryData = new ServiceCategoryData();
44     private static Map<NodeTypeEnum, String> nodeTypeToUniqueKeyMapper = new HashMap<>();
45
46     static {
47         nodeTypeToUniqueKeyMapper.put(NodeTypeEnum.User, userData.getUniqueIdKey());
48         nodeTypeToUniqueKeyMapper.put(NodeTypeEnum.Tag, tagData.getUniqueIdKey());
49         nodeTypeToUniqueKeyMapper.put(NodeTypeEnum.ResourceCategory, resCategoryData.getUniqueIdKey());
50         nodeTypeToUniqueKeyMapper.put(NodeTypeEnum.ServiceCategory, serCategoryData.getUniqueIdKey());
51     }
52
53     public static String buildPropertyUniqueId(String resourceId, String propertyName) {
54         return resourceId + DOT + propertyName;
55     }
56
57     static String buildHeatParameterUniqueId(String resourceId, String propertyName) {
58         return resourceId + DOT + HEAT_PARAM_PREFIX + propertyName;
59     }
60
61     static String buildHeatParameterValueUniqueId(String resourceId, String artifactLabel, String propertyName) {
62         return buildTypeUid(resourceId, artifactLabel, propertyName);
63     }
64
65     /**
66      * find the unique id key of a node on the graph
67      *
68      * @param nodeTypeEnum
69      * @return
70      */
71     public static String getKeyByNodeType(NodeTypeEnum nodeTypeEnum) {
72         String uniqueID = nodeTypeToUniqueKeyMapper.get(nodeTypeEnum);
73         if (uniqueID == null) {
74             uniqueID = GraphPropertiesDictionary.UNIQUE_ID.getProperty();
75         }
76         return uniqueID;
77     }
78
79     public static String buildResourceUniqueId() {
80         return generateUUID();
81     }
82
83     public static String generateUUID() {
84         UUID uuid = UUID.randomUUID();
85         return uuid.toString();
86     }
87
88     public static String buildComponentUniqueId() {
89         return generateUUID();
90     }
91
92     static String buildCapabilityTypeUid(String type) {
93         return type;
94     }
95
96     public static String buildRelationshipTypeUid(final String modelName, final String type) {
97         return StringUtils.isEmpty(modelName) ? type : modelName + DOT + type;
98     }
99
100     public static String buildAttributeUid(String resourceId, String attName) {
101         return buildTypeUid(NodeTypeEnum.Attribute.getName(), resourceId, attName);
102     }
103
104     public static String buildRequirementUid(String resourceId, String reqName) {
105         return resourceId + DOT + reqName;
106     }
107
108     public static String buildCapabilityUid(String resourceId, String capabilityName) {
109         return buildTypeUid(NodeTypeEnum.Capability.getName(), resourceId, capabilityName);
110     }
111
112     public static String buildArtifactByInterfaceUniqueId(String resourceId, String interfaceName, String operation, String artifactLabel) {
113         return resourceId + DOT + interfaceName + DOT + operation + DOT + artifactLabel;
114     }
115
116     public static String buildInstanceArtifactUniqueId(String parentId, String instanceId, String artifactLabel) {
117         return buildTypeUid(parentId, instanceId, artifactLabel);
118     }
119
120     public static String buildResourceInstanceUniuqeId(String serviceId, String resourceId, String logicalName) {
121         return buildTypeUid(serviceId, resourceId, logicalName);
122     }
123
124     public static String buildRelationsipInstInstanceUid(String resourceInstUid, String requirement) {
125         return generateUUID();
126     }
127
128     /*
129      * TODO Pavel To be removed when new category logic comes in
130      */
131     static String buildResourceCategoryUid(String categoryName, String subcategoryName, NodeTypeEnum type) {
132         return buildTypeUid(type.getName(), categoryName, subcategoryName);
133     }
134
135     /*
136      * TODO Pavel To be removed when new category logic comes in
137      */
138     static String buildServiceCategoryUid(String categoryName, NodeTypeEnum type) {
139         return type.getName() + DOT + categoryName;
140     }
141
142     // New logic
143     public static String buildCategoryUid(String categoryName, NodeTypeEnum type) {
144         return type.getName() + DOT + categoryName;
145     }
146
147     public static String buildComponentCategoryUid(String categoryName, VertexTypeEnum type) {
148         return type.getName() + DOT + ValidationUtils.normalizeCategoryName4Uniqueness(categoryName);
149     }
150
151     public static String buildSubCategoryUid(String categoryUid, String subCategoryName) {
152         return categoryUid + DOT + subCategoryName;
153     }
154
155     public static String buildGroupingUid(String subCategoryUid, String groupingName) {
156         return subCategoryUid + DOT + groupingName;
157     }
158
159     static String buildResourceInstancePropertyValueUid(String resourceInstanceUniqueId, Integer index) {
160         return resourceInstanceUniqueId + DOT + "property" + DOT + index;
161     }
162
163     public static String buildComponentPropertyUniqueId(String resourceId, String propertyName) {
164         return buildTypeUid(NodeTypeEnum.Property.getName(), resourceId, propertyName);
165     }
166
167     static String buildResourceInstanceAttributeValueUid(String resourceInstanceUniqueId, Integer index) {
168         return resourceInstanceUniqueId + DOT + "attribute" + DOT + index;
169     }
170
171     static String buildResourceInstanceInputValueUid(String resourceInstanceUniqueId, Integer index) {
172         return resourceInstanceUniqueId + DOT + "input" + DOT + index;
173     }
174
175     static String buildAdditionalInformationUniqueId(String resourceUniqueId) {
176         return resourceUniqueId + DOT + "additionalinformation";
177     }
178
179     public static String buildDataTypeUid(final String modelName, final String name) {
180         return StringUtils.isEmpty(modelName) ? name + DOT + "datatype" : modelName + DOT + name + DOT + "datatype";
181     }
182
183     public static String buildInvariantUUID() {
184         return generateUUID();
185     }
186
187     static String buildGroupTypeUid(String type, String version, String resourceName) {
188         return buildTypeUid(type, version, resourceName);
189     }
190
191     static String buildPolicyTypeUid(String type, String version, String resourceName) {
192         return buildTypeUid(type, version, resourceName);
193     }
194
195     static String buildTypeUid(String type, String version, String resourceName) {
196         return type + DOT + version + DOT + resourceName;
197     }
198
199     public static String buildPolicyUniqueId(String componentId, String name) {
200         return componentId + DOT + name + Constants.POLICY_UID_POSTFIX;
201     }
202
203     public static String buildGroupPropertyValueUid(String groupUniqueId, Integer index) {
204         return groupUniqueId + DOT + "property" + DOT + index;
205     }
206
207     public static String buildModelUid(final String modelName) {
208         return NodeTypeEnum.Model.getName() + DOT + modelName;
209     }
210 }