summaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/Property.java
blob: 731bc73f323bc2d68eccdce4bf189744f9cdf432 (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
package org.openecomp.sdc.toscaparser.api;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.api.elements.constraints.Constraint;
import org.openecomp.sdc.toscaparser.api.elements.constraints.Schema;
import org.openecomp.sdc.toscaparser.api.functions.Function;

public class Property {
    // TOSCA built-in Property type

	private static final String TYPE = "type";
	private static final String REQUIRED = "required";
	private static final String DESCRIPTION = "description";
	private static final String DEFAULT = "default";
	private static final String CONSTRAINTS = "constraints";
	
	private static final String[] PROPERTY_KEYS = {
			TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS};

	private static final String ENTRYTYPE = "type";
	private static final String ENTRYPROPERTIES = "properties";
	private static final String[] ENTRY_SCHEMA_KEYS = {
        ENTRYTYPE, ENTRYPROPERTIES};
	
	private String name;
	private Object value;
	private Schema schema;
	private LinkedHashMap<String,Object> customDef;

	public Property(String propname,
					Object propvalue,
					LinkedHashMap<String,Object> propschemaDict,
					LinkedHashMap<String,Object> propcustomDef) {
		
        name = propname;
        value = propvalue;
        customDef = propcustomDef;
        schema = new Schema(propname, propschemaDict);
	}
	
	public String getType() {
		return schema.getType();
	}

	public boolean isRequired() {
		return schema.isRequired();
	}
	
	public String getDescription() {
		return schema.getDescription();
	}

	public Object getDefault() {
		return schema.getDefault();
	}

	public ArrayList<Constraint> getConstraints() {
		return schema.getConstraints();
	}

	public LinkedHashMap<String,Object> getEntrySchema() {
		return schema.getEntrySchema();
	}
 

	public String getName() {
		return name;
	}

	public Object getValue() {
		return value;
	}
	
	// setter
	public Object setValue(Object vob) {
		value = vob;
		return value;
	}
	
	public void validate() {
		// Validate if not a reference property
		if(!Function.isFunction(value)) {
			if(getType().equals(Schema.STRING)) {
				value = value.toString();
			}
			value = DataEntity.validateDatatype(getType(),value,
                    							 getEntrySchema(),
                    							 customDef,
                    							 name);
			_validateConstraints();
		}
	}

	private void _validateConstraints() {
		if(getConstraints() != null) {
			for(Constraint constraint: getConstraints()) {
				constraint.validate(value);
			}
		}
	}

	@Override
	public String toString() {
		return "Property{" +
				"name='" + name + '\'' +
				", value=" + value +
				", schema=" + schema +
				", customDef=" + customDef +
				'}';
	}
}

/*python

class Property(object):
    '''TOSCA built-in Property type.'''

    PROPERTY_KEYS = (
        TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS
    ) = (
        'type', 'required', 'description', 'default', 'constraints'
    )

    ENTRY_SCHEMA_KEYS = (
        ENTRYTYPE, ENTRYPROPERTIES
    ) = (
        'type', 'properties'
    )

    def __init__(self, property_name, value, schema_dict, custom_def=None):
        self.name = property_name
        self.value = value
        self.custom_def = custom_def
        self.schema = Schema(property_name, schema_dict)

    @property
    def type(self):
        return self.schema.type

    @property
    def required(self):
        return self.schema.required

    @property
    def description(self):
        return self.schema.description

    @property
    def default(self):
        return self.schema.default

    @property
    def constraints(self):
        return self.schema.constraints

    @property
    def entry_schema(self):
        return self.schema.entry_schema

    def validate(self):
        '''Validate if not a reference property.'''
        if not is_function(self.value):
            if self.type == Schema.STRING:
                self.value = str(self.value)
            self.value = DataEntity.validate_datatype(self.type, self.value,
                                                      self.entry_schema,
                                                      self.custom_def,
                                                      self.name)
            self._validate_constraints()

    def _validate_constraints(self):
        if self.constraints:
            for constraint in self.constraints:
                constraint.validate(self.value)
*/