Merge "[SDC] code sync"
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / tosca / ToscaType.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 java.text.DateFormat;
24 import java.text.ParseException;
25 import java.util.Locale;
26
27 /**
28  * The primitive type that TOSCA YAML supports.
29  * 
30  * @author mkv
31  */
32 public enum ToscaType {
33         STRING, INTEGER, FLOAT, BOOLEAN, TIMESTAMP, VERSION;
34
35         // private static final Pattern TIMESTAMP_REGEX = Pattern
36         // .compile("[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?([Tt]|[
37         // \\t]+)[0-9][0-9]?:[0-9][0-9]:[0-9][0-9](\\.[0-9]*)?(([ \\t]*)Z|([
38         // \\t]*)[-+][0-9][0-9]?(:[0-9][0-9])?)?");
39
40         public static ToscaType fromYamlTypeName(String typeName) {
41                 if (typeName == null) {
42                         return null;
43                 }
44                 try {
45                         return ToscaType.valueOf(typeName.toUpperCase());
46                 } catch (IllegalArgumentException e) {
47                         return null;
48                 }
49         }
50
51         public boolean isValidValue(String value) {
52                 switch (this) {
53                 case BOOLEAN:
54                         return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false");
55                 case FLOAT:
56                         return isFloat(value);
57                 case INTEGER:
58                         return isInteger(value);
59                 case STRING:
60                         return true;
61                 case TIMESTAMP:
62                         try {
63                                 DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
64                                 return true;
65                         } catch (ParseException e) {
66                                 return false;
67                         }
68                 case VERSION:
69                         return VersionUtil.isValid(value);
70                 default:
71                         return false;
72                 }
73         }
74
75         private boolean isFloat(String value) {
76                 try {
77                         Float.valueOf(value);
78                 } catch (NumberFormatException e) {
79                         return false;
80                 }
81                 return true;
82         }
83
84         private boolean isInteger(String value) {
85                 try {
86                         Long.valueOf(value);
87                 } catch (NumberFormatException e) {
88                         return false;
89                 }
90                 return true;
91         }
92
93         public Object convert(String value) {
94                 switch (this) {
95                 case STRING:
96                         return value;
97                 case BOOLEAN:
98                         return Boolean.valueOf(value);
99                 case FLOAT:
100                         return Double.valueOf(value);
101                 case INTEGER:
102                         return Long.valueOf(value);
103                 case TIMESTAMP:
104                         try {
105                                 return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.US).parse(value);
106                         } catch (ParseException e) {
107                                 throw new IllegalArgumentException("Value must be a valid timestamp", e);
108                         }
109                 case VERSION:
110                         return VersionUtil.parseVersion(value);
111                 default:
112                         return null;
113                 }
114         }
115
116         @Override
117         public String toString() {
118                 return name().toLowerCase();
119         }
120 }