Catalog alignment
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / ToscaPropertyType.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
21 package org.openecomp.sdc.be.model.tosca;
22
23 import org.openecomp.sdc.be.model.tosca.converters.*;
24 import org.openecomp.sdc.be.model.tosca.validators.*;
25
26 /**
27  * The primitive type that TOSCA YAML supports.
28  * 
29  * @author esofer
30  */
31 public enum ToscaPropertyType {
32
33     ROOT("tosca.datatypes.Root", null, null, null, true),
34
35     STRING("string", StringValidator.getInstance(), StringConvertor.getInstance(), ToscaStringConvertor.getInstance()),
36
37     BOOLEAN("boolean", BooleanValidator.getInstance(), ToscaBooleanConverter.getInstance(), BooleanConverter.getInstance()),
38
39     FLOAT("float", FloatValidator.getInstance(), ToscaFloatConverter.getInstance(), FloatConverter.getInstance()),
40
41     INTEGER("integer", IntegerValidator.getInstance(), DefaultConverter.getInstance(), IntegerConverter.getInstance()),
42
43     SCALAR_UNIT("scalar-unit", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
44
45     SCALAR_UNIT_SIZE("scalar-unit.size", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
46
47     SCALAR_UNIT_TIME("scalar-unit.time", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
48
49     SCALAR_UNIT_FREQUENCY("scalar-unit.frequency", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
50
51     RANGE("range", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
52
53     TIMESTAMP("timestamp", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
54
55     MAP("map", MapValidator.getInstance(), MapConverter.getInstance(), ToscaMapValueConverter.getInstance()),
56
57     LIST("list", ListValidator.getInstance(), ListConverter.getInstance(), ToscaListValueConverter.getInstance()),
58
59     VERSION("version", StringValidator.getInstance(), DefaultConverter.getInstance(), ToscaValueDefaultConverter.getInstance()),
60
61     KEY("key", KeyValidator.getInstance(), StringConvertor.getInstance(), ToscaValueDefaultConverter.getInstance()),
62
63     JSON("json", JsonValidator.getInstance(), JsonConverter.getInstance(), ToscaJsonValueConverter.getInstance());
64
65     private String type;
66     private PropertyTypeValidator validator;
67     private PropertyValueConverter converter;
68     private ToscaValueConverter valueConverter;
69     private boolean isAbstract = false;
70
71     ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, ToscaValueConverter valueConverter) {
72         this.type = type;
73         this.validator = validator;
74         this.converter = converter;
75         this.valueConverter = valueConverter;
76     }
77
78     ToscaPropertyType(String type, PropertyTypeValidator validator, PropertyValueConverter converter, ToscaValueConverter valueConverter, boolean isAbstract) {
79         this(type, validator, converter, valueConverter);
80         this.isAbstract = isAbstract;
81     }
82
83     public String getType() {
84         return type;
85     }
86
87     public PropertyTypeValidator getValidator() {
88         return validator;
89     }
90
91     public PropertyValueConverter getConverter() {
92         return converter;
93     }
94
95     public boolean isAbstract() {
96         return isAbstract;
97     }
98
99     public ToscaValueConverter getValueConverter() {
100         return valueConverter;
101     }
102
103     public static ToscaPropertyType isValidType(String typeName) {
104         if (typeName == null) {
105             return null;
106         }
107
108         for (ToscaPropertyType type : ToscaPropertyType.values()) {
109             if (type.getType().equals(typeName)) {
110                 return type;
111             }
112         }
113         return null;
114     }
115
116     public static boolean isScalarType(String dataTypeName) {
117
118         ToscaPropertyType isPrimitiveToscaType = ToscaPropertyType.isValidType(dataTypeName);
119
120         return isPrimitiveToscaType != null && !isPrimitiveToscaType.isAbstract();
121
122     }
123
124     public static boolean isPrimitiveType(String dataTypeName) {
125
126         if (ToscaPropertyType.MAP.getType().equals(dataTypeName) || ToscaPropertyType.LIST.getType().equals(dataTypeName)){
127             return false;
128         }
129         if(isScalarType(dataTypeName)){
130             return true;
131         }
132         return false;
133     }
134
135     public static ToscaPropertyType getTypeIfScalar(String dataTypeName) {
136
137         ToscaPropertyType isPrimitiveToscaType = ToscaPropertyType.isValidType(dataTypeName);
138
139         if (isPrimitiveToscaType != null && !isPrimitiveToscaType.isAbstract()) {
140             return isPrimitiveToscaType;
141         } else {
142             return null;
143         }
144
145     }
146
147     @Override
148     public String toString() {
149         return name().toLowerCase();
150     }
151 }