7a4acd94549e9a47b8f74ccf337af957eec704c3
[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
14 import logging
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import MissingRequiredFieldError
18 from toscaparser.common.exception import UnknownFieldError
19 from toscaparser.dataentity import DataEntity
20 from toscaparser.elements.constraints import Schema
21 from toscaparser.elements.entity_type import EntityType
22 from toscaparser.utils.gettextutils import _
23 from org.openecomp.sdc.toscaparser.jython.parameters import JyInput 
24
25
26 log = logging.getLogger('tosca')
27
28
29 class Input(JyInput):
30
31     INPUTFIELD = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED, STATUS,
32                   ENTRY_SCHEMA) = ('type', 'description', 'default',
33                                    'constraints', 'required', 'status',
34                                    'entry_schema')
35
36     def __init__(self, name, schema_dict):
37         self.name = name
38         self.schema = Schema(name, schema_dict)
39
40         self._validate_field()
41         self.validate_type(self.type)
42
43     def getJyName(self):
44         return self.name  
45
46     def getJyType(self):
47         return self.name  
48     
49     def isJyRequired(self):
50         return self.required  
51
52     def getJyDescription(self):
53         return self.description  
54     
55     @property
56     def type(self):
57         return self.schema.type
58
59     @property
60     def required(self):
61         return self.schema.required
62
63     @property
64     def description(self):
65         return self.schema.description
66
67     @property
68     def default(self):
69         return self.schema.default
70
71     @property
72     def constraints(self):
73         return self.schema.constraints
74
75     @property
76     def status(self):
77         return self.schema.status
78
79     def validate(self, value=None):
80         if value is not None:
81             self._validate_value(value)
82
83     def _validate_field(self):
84         for name in self.schema.schema:
85             if name not in self.INPUTFIELD:
86                 ExceptionCollector.appendException(
87                     UnknownFieldError(what='Input "%s"' % self.name,
88                                       field=name))
89
90     def validate_type(self, input_type):
91         if input_type not in Schema.PROPERTY_TYPES:
92             ExceptionCollector.appendException(
93                 ValueError(_('Invalid type "%s".') % type))
94
95     # TODO(anyone) Need to test for any built-in datatype not just network
96     # that is, tosca.datatypes.* and not assume tosca.datatypes.network.*
97     # TODO(anyone) Add support for tosca.datatypes.Credential
98     def _validate_value(self, value):
99         tosca = EntityType.TOSCA_DEF
100         datatype = None
101         if self.type in tosca:
102             datatype = tosca[self.type]
103         elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca:
104             datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type]
105
106         DataEntity.validate_datatype(self.type, value, None, datatype)
107
108
109 class Output(object):
110
111     OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
112
113     def __init__(self, name, attrs):
114         self.name = name
115         self.attrs = attrs
116
117     @property
118     def description(self):
119         return self.attrs.get(self.DESCRIPTION)
120
121     @property
122     def value(self):
123         return self.attrs.get(self.VALUE)
124
125     def validate(self):
126         self._validate_field()
127
128     def _validate_field(self):
129         if not isinstance(self.attrs, dict):
130             ExceptionCollector.appendException(
131                 MissingRequiredFieldError(what='Output "%s"' % self.name,
132                                           required=self.VALUE))
133         if self.value is None:
134             ExceptionCollector.appendException(
135                 MissingRequiredFieldError(what='Output "%s"' % self.name,
136                                           required=self.VALUE))
137         for name in self.attrs:
138             if name not in self.OUTPUTFIELD:
139                 ExceptionCollector.appendException(
140                     UnknownFieldError(what='Output "%s"' % self.name,
141                                       field=name))