summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/parser/modeling/context.py
blob: d8a1f7ad6188bbc026c66dea4c3a1765a246c5c8 (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
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import itertools

from ...utils.collections import StrictDict, prune
from ...utils.uuid import generate_uuid


# See: http://www.faqs.org/rfcs/rfc1035.html
ID_MAX_LENGTH = 63


class IdType(object):
    LOCAL_SERIAL = 0
    """
    Locally unique serial ID: a running integer.
    """

    LOCAL_RANDOM = 1
    """
    Locally unique ID: 6 random safe characters.
    """

    UNIVERSAL_RANDOM = 2
    """
    Universally unique ID (UUID): 22 random safe characters.
    """


class ModelingContext(object):
    """
    Modeling context.

    :ivar template: generated service template
    :vartype template: aria.modeling.models.ServiceTemplate
    :ivar instance: generated service instance
    :vartype instance: aria.modeling.models.Service
    :ivar node_id_format: format for node instance IDs
    :vartype node_id_format: basestring
    :ivar id_type: type of IDs to use for instances
    :vartype id_type: basestring
    :ivar id_max_length: maximum allowed instance ID length
    :vartype id_max_length: int
    :ivar inputs: inputs values
    :vartype inputs: {:obj:`basestring`, object}
    """

    def __init__(self):
        self.template = None
        self.instance = None
        self.node_id_format = '{template}_{id}'
        #self.id_type = IdType.LOCAL_SERIAL
        #self.id_type = IdType.LOCAL_RANDOM
        self.id_type = IdType.UNIVERSAL_RANDOM
        self.id_max_length = ID_MAX_LENGTH
        self.inputs = StrictDict(key_class=basestring)

        self._serial_id_counter = itertools.count(1)
        self._locally_unique_ids = set()

    def store(self, model_storage):
        if self.template is not None:
            model_storage.service_template.put(self.template)
        if self.instance is not None:
            model_storage.service.put(self.instance)

    def generate_id(self):
        if self.id_type == IdType.LOCAL_SERIAL:
            return self._serial_id_counter.next()

        elif self.id_type == IdType.LOCAL_RANDOM:
            the_id = generate_uuid(6)
            while the_id in self._locally_unique_ids:
                the_id = generate_uuid(6)
            self._locally_unique_ids.add(the_id)
            return the_id

        return generate_uuid()

    def set_input(self, name, value):
        self.inputs[name] = value
        # TODO: coerce to validate type

    @property
    def template_as_raw(self):
        raw = self.template.as_raw
        prune(raw)
        return raw

    @property
    def instance_as_raw(self):
        raw = self.instance.as_raw
        prune(raw)
        return raw