710da1bb09f4dfe59ea2ab5dc6d1c5eda672b067
[sdc/sdc-distribution-client.git] /
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13 from toscaparser.dataentity import DataEntity
14 from toscaparser.elements.constraints import Schema
15 from toscaparser.functions import is_function
16 from org.openecomp.sdc.toscaparser.jython import JyProperty
17
18
19 class Property(JyProperty):
20     '''TOSCA built-in Property type.'''
21
22     PROPERTY_KEYS = (
23         TYPE, REQUIRED, DESCRIPTION, DEFAULT, CONSTRAINTS
24     ) = (
25         'type', 'required', 'description', 'default', 'constraints'
26     )
27
28     ENTRY_SCHEMA_KEYS = (
29         ENTRYTYPE, ENTRYPROPERTIES
30     ) = (
31         'type', 'properties'
32     )
33
34     def __init__(self, property_name, value, schema_dict, custom_def=None):
35         self.name = property_name
36         self.value = value
37         self.custom_def = custom_def
38         self.schema = Schema(property_name, schema_dict)
39         
40     def getJyName(self):
41         return self.name       
42     
43     def getJyValue(self):
44         return self.value
45     
46     def getJyValueClassName(self):
47         return self.value.__class__.__name__
48     
49     def getJyType(self):
50         return self.type       
51         
52     def isJyRequired(self):
53         return self.required   
54             
55     def getJyDescription(self):
56         return self.description  
57
58     @property
59     def type(self):
60         return self.schema.type
61
62     @property
63     def required(self):
64         return self.schema.required
65
66     @property
67     def description(self):
68         return self.schema.description
69
70     @property
71     def default(self):
72         return self.schema.default
73
74     @property
75     def constraints(self):
76         return self.schema.constraints
77
78     @property
79     def entry_schema(self):
80         return self.schema.entry_schema
81
82     def validate(self):
83         '''Validate if not a reference property.'''
84         if not is_function(self.value):
85             if self.type == Schema.STRING:
86                 self.value = str(self.value)
87             self.value = DataEntity.validate_datatype(self.type, self.value,
88                                                       self.entry_schema,
89                                                       self.custom_def,
90                                                       self.name)
91             self._validate_constraints()
92
93     def _validate_constraints(self):
94         if self.constraints:
95             for constraint in self.constraints:
96                 constraint.validate(self.value)