vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / modeling / context.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import itertools
17
18 from ...utils.collections import StrictDict, prune
19 from ...utils.uuid import generate_uuid
20
21
22 # See: http://www.faqs.org/rfcs/rfc1035.html
23 ID_MAX_LENGTH = 63
24
25
26 class IdType(object):
27     LOCAL_SERIAL = 0
28     """
29     Locally unique serial ID: a running integer.
30     """
31
32     LOCAL_RANDOM = 1
33     """
34     Locally unique ID: 6 random safe characters.
35     """
36
37     UNIVERSAL_RANDOM = 2
38     """
39     Universally unique ID (UUID): 22 random safe characters.
40     """
41
42
43 class ModelingContext(object):
44     """
45     Modeling context.
46
47     :ivar template: generated service template
48     :vartype template: aria.modeling.models.ServiceTemplate
49     :ivar instance: generated service instance
50     :vartype instance: aria.modeling.models.Service
51     :ivar node_id_format: format for node instance IDs
52     :vartype node_id_format: basestring
53     :ivar id_type: type of IDs to use for instances
54     :vartype id_type: basestring
55     :ivar id_max_length: maximum allowed instance ID length
56     :vartype id_max_length: int
57     :ivar inputs: inputs values
58     :vartype inputs: {:obj:`basestring`, object}
59     """
60
61     def __init__(self):
62         self.template = None
63         self.instance = None
64         self.node_id_format = '{template}_{id}'
65         #self.id_type = IdType.LOCAL_SERIAL
66         #self.id_type = IdType.LOCAL_RANDOM
67         self.id_type = IdType.UNIVERSAL_RANDOM
68         self.id_max_length = ID_MAX_LENGTH
69         self.inputs = StrictDict(key_class=basestring)
70
71         self._serial_id_counter = itertools.count(1)
72         self._locally_unique_ids = set()
73
74     def store(self, model_storage):
75         if self.template is not None:
76             model_storage.service_template.put(self.template)
77         if self.instance is not None:
78             model_storage.service.put(self.instance)
79
80     def generate_id(self):
81         if self.id_type == IdType.LOCAL_SERIAL:
82             return self._serial_id_counter.next()
83
84         elif self.id_type == IdType.LOCAL_RANDOM:
85             the_id = generate_uuid(6)
86             while the_id in self._locally_unique_ids:
87                 the_id = generate_uuid(6)
88             self._locally_unique_ids.add(the_id)
89             return the_id
90
91         return generate_uuid()
92
93     def set_input(self, name, value):
94         self.inputs[name] = value
95         # TODO: coerce to validate type
96
97     @property
98     def template_as_raw(self):
99         raw = self.template.as_raw
100         prune(raw)
101         return raw
102
103     @property
104     def instance_as_raw(self):
105         raw = self.instance.as_raw
106         prune(raw)
107         return raw