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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from ...utils.collections import StrictDict, prune
19 from ...utils.uuid import generate_uuid
22 # See: http://www.faqs.org/rfcs/rfc1035.html
29 Locally unique serial ID: a running integer.
34 Locally unique ID: 6 random safe characters.
39 Universally unique ID (UUID): 22 random safe characters.
43 class ModelingContext(object):
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}
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)
71 self._serial_id_counter = itertools.count(1)
72 self._locally_unique_ids = set()
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)
80 def generate_id(self):
81 if self.id_type == IdType.LOCAL_SERIAL:
82 return self._serial_id_counter.next()
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)
91 return generate_uuid()
93 def set_input(self, name, value):
94 self.inputs[name] = value
95 # TODO: coerce to validate type
98 def template_as_raw(self):
99 raw = self.template.as_raw
104 def instance_as_raw(self):
105 raw = self.instance.as_raw