765ccb7bca9009aa739607386215b24c9e140e4a
[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.type  
48     
49     def isJyRequired(self):
50         return self.required  
51
52     def getJyDescription(self):
53         return self.description 
54     
55     def getJyDefault(self):
56         return self.default   
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 status(self):
80         return self.schema.status
81
82     def validate(self, value=None):
83         if value is not None:
84             self._validate_value(value)
85
86     def _validate_field(self):
87         for name in self.schema.schema:
88             if name not in self.INPUTFIELD:
89                 ExceptionCollector.appendException(
90                     UnknownFieldError(what='Input "%s"' % self.name,
91                                       field=name))
92
93     def validate_type(self, input_type):
94         if input_type not in Schema.PROPERTY_TYPES:
95             ExceptionCollector.appendException(
96                 #ValueError(_('Invalid type "%s".') % type))
97                 ValueError(_('Invalid type "%s".') % input_type))
98
99     # TODO(anyone) Need to test for any built-in datatype not just network
100     # that is, tosca.datatypes.* and not assume tosca.datatypes.network.*
101     # TODO(anyone) Add support for tosca.datatypes.Credential
102     def _validate_value(self, value):
103         tosca = EntityType.TOSCA_DEF
104         datatype = None
105         if self.type in tosca:
106             datatype = tosca[self.type]
107         elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca:
108             datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type]
109
110         DataEntity.validate_datatype(self.type, value, None, datatype)
111
112
113 class Output(object):
114
115     OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
116
117     def __init__(self, name, attrs):
118         self.name = name
119         self.attrs = attrs
120
121     @property
122     def description(self):
123         return self.attrs.get(self.DESCRIPTION)
124
125     @property
126     def value(self):
127         return self.attrs.get(self.VALUE)
128
129     def validate(self):
130         self._validate_field()
131
132     def _validate_field(self):
133         if not isinstance(self.attrs, dict):
134             ExceptionCollector.appendException(
135                 MissingRequiredFieldError(what='Output "%s"' % self.name,
136                                           required=self.VALUE))
137         if self.value is None:
138             ExceptionCollector.appendException(
139                 MissingRequiredFieldError(what='Output "%s"' % self.name,
140                                           required=self.VALUE))
141         for name in self.attrs:
142             if name not in self.OUTPUTFIELD:
143                 ExceptionCollector.appendException(
144                     UnknownFieldError(what='Output "%s"' % self.name,
145                                       field=name))