aboutsummaryrefslogtreecommitdiffstats
path: root/jtosca/src/main/java/org/openecomp/sdc/toscaparser/api/elements/PortSpec.java
blob: dcb14f91cb1f37f1544805276580fde336c9e7b7 (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
package org.openecomp.sdc.toscaparser.api.elements;

import java.util.LinkedHashMap;

import org.openecomp.sdc.toscaparser.api.DataEntity;
import org.openecomp.sdc.toscaparser.api.common.ExceptionCollector;
import org.openecomp.sdc.toscaparser.api.utils.ValidateUtils;

public class PortSpec {
    // Parent class for tosca.datatypes.network.PortSpec type

    private static final String SHORTNAME = "PortSpec";
    private static final String TYPE_URI = "tosca.datatypes.network." + SHORTNAME;

    private static final String PROTOCOL = "protocol";
    private static final String SOURCE = "source";
    private static final String SOURCE_RANGE = "source_range";
    private static final String TARGET = "target";
    private static final String TARGET_RANGE = "target_range";
    
    private static final String PROPERTY_NAMES[] = {
        PROTOCOL, SOURCE, SOURCE_RANGE,
        TARGET, TARGET_RANGE
    };
	
    // todo(TBD) May want to make this a subclass of DataType
    // and change init method to set PortSpec's properties
    public PortSpec() {
    	
    }

    // The following additional requirements MUST be tested:
    // 1) A valid PortSpec MUST have at least one of the following properties:
    //   target, target_range, source or source_range.
    // 2) A valid PortSpec MUST have a value for the source property that
    //    is within the numeric range specified by the property source_range
    //    when source_range is specified.
    // 3) A valid PortSpec MUST have a value for the target property that is
    //    within the numeric range specified by the property target_range
    //    when target_range is specified.
	public static void  validateAdditionalReq(Object _properties,
											  String propName,
											  LinkedHashMap<String,Object> custom_def) {
		
        try {
        	LinkedHashMap<String,Object> properties = (LinkedHashMap<String,Object>)_properties;
            Object source = properties.get(PortSpec.SOURCE);
            Object sourceRange = properties.get(PortSpec.SOURCE_RANGE);
            Object target = properties.get(PortSpec.TARGET);
            Object targetRange = properties.get(PortSpec.TARGET_RANGE);

            // verify one of the specified values is set
            if(source == null && sourceRange == null && 
                    target == null && targetRange == null) { 
                ExceptionCollector.appendException(String.format(
                    "InvalidTypeAdditionalRequirementsError: Additional requirements for type \"%s\" not met",
                    TYPE_URI));
            }
            // Validate source value is in specified range
            if(source != null &&  sourceRange != null) {
                ValidateUtils.validateValueInRange(source,sourceRange,SOURCE);
            }
            else {
                DataEntity portdef = new DataEntity("PortDef", source, null, SOURCE);
                portdef.validate();
            }
            // Validate target value is in specified range
            if(target != null &&  targetRange != null) {
                ValidateUtils.validateValueInRange(target,targetRange,SOURCE);
            }
            else {
                DataEntity portdef = new DataEntity("PortDef", source, null, TARGET);
                portdef.validate();
            }
        }
        catch(Exception e) {
            ExceptionCollector.appendException(String.format(
                "ValueError: \"%s\" do not meet requirements for type \"%s\"", 
                _properties.toString(),SHORTNAME));
        }
 	}

}

/*python

from toscaparser.common.exception import ExceptionCollector
from toscaparser.common.exception import InvalidTypeAdditionalRequirementsError
from toscaparser.utils.gettextutils import _
import org.openecomp.sdc.toscaparser.api.utils.validateutils as validateutils

log = logging.getLogger('tosca')


class PortSpec(object):
    '''Parent class for tosca.datatypes.network.PortSpec type.'''

    SHORTNAME = 'PortSpec'
    TYPE_URI = 'tosca.datatypes.network.' + SHORTNAME

    PROPERTY_NAMES = (
        PROTOCOL, SOURCE, SOURCE_RANGE,
        TARGET, TARGET_RANGE
    ) = (
        'protocol', 'source', 'source_range',
        'target', 'target_range'
    )

    # TODO(TBD) May want to make this a subclass of DataType
    # and change init method to set PortSpec's properties
    def __init__(self):
        pass

    # The following additional requirements MUST be tested:
    # 1) A valid PortSpec MUST have at least one of the following properties:
    #   target, target_range, source or source_range.
    # 2) A valid PortSpec MUST have a value for the source property that
    #    is within the numeric range specified by the property source_range
    #    when source_range is specified.
    # 3) A valid PortSpec MUST have a value for the target property that is
    #    within the numeric range specified by the property target_range
    #    when target_range is specified.
    @staticmethod
    def validate_additional_req(properties, prop_name, custom_def=None, ):
        try:
            source = properties.get(PortSpec.SOURCE)
            source_range = properties.get(PortSpec.SOURCE_RANGE)
            target = properties.get(PortSpec.TARGET)
            target_range = properties.get(PortSpec.TARGET_RANGE)

            # verify one of the specified values is set
            if source is None and source_range is None and \
                    target is None and target_range is None:
                ExceptionCollector.appendException(
                    InvalidTypeAdditionalRequirementsError(
                        type=PortSpec.TYPE_URI))
            # Validate source value is in specified range
            if source and source_range:
                validateutils.validate_value_in_range(source, source_range,
                                                      PortSpec.SOURCE)
            else:
                from toscaparser.dataentity import DataEntity
                portdef = DataEntity('PortDef', source, None, PortSpec.SOURCE)
                portdef.validate()
            # Validate target value is in specified range
            if target and target_range:
                validateutils.validate_value_in_range(target, target_range,
                                                      PortSpec.TARGET)
            else:
                from toscaparser.dataentity import DataEntity
                portdef = DataEntity('PortDef', source, None, PortSpec.TARGET)
                portdef.validate()
        except Exception:
            msg = _('"%(value)s" do not meet requirements '
                    'for type "%(type)s".') \
                % {'value': properties, 'type': PortSpec.SHORTNAME}
            ExceptionCollector.appendException(
                ValueError(msg))
*/