Statistics
| Branch: | Tag: | Revision:

one / include / VMGroupRole.h @ 202b47e9

History | View | Annotate | Download (5.76 KB)

1
/* ------------------------------------------------------------------------ */
2
/* Copyright 2002-2016, OpenNebula Project, OpenNebula Systems              */
3
/*                                                                          */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may  */
5
/* not use this file except in compliance with the License. You may obtain  */
6
/* 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

    
17
#ifndef VMGROUP_ROLE_H_
18
#define VMGROUP_ROLE_H_
19

    
20
#include "PoolObjectSQL.h"
21
#include "NebulaUtil.h"
22

    
23
class VMGroupPool;
24

    
25
/**
26
 *  A VMGroupRole defines a VM type that typically implements a role in a
27
 *  multi-vm application.
28
 *
29
 *  ROLE = [
30
 *    NAME = "Web application servers",
31
 *    ID   = 12,
32
 *    VMS  = "1,2,45,21"
33
 *  ]
34
 *
35
 */
36
class VMGroupRole
37
{
38
public:
39
    VMGroupRole(VectorAttribute *_va);
40

    
41
    virtual ~VMGroupRole(){};
42

    
43
    /* ---------------------------------------------------------------------- */
44
    /* VMS set Interface                                                      */
45
    /* ---------------------------------------------------------------------- */
46
    const std::set<int>& get_vms()
47
    {
48
        return vms;
49
    };
50

    
51
    void add_vm(int vm_id);
52

    
53
    void del_vm(int vm_id);
54

    
55
private:
56
    /**
57
     *  The set of vms in the role
58
     */
59
    std::set<int> vms;
60

    
61
    /**
62
     *  The associated vector attribute
63
     */
64
    VectorAttribute * va;
65

    
66
    /**
67
     *  Set the VMS attribute for the role (list of VM IDs)
68
     */
69
    void set_vms();
70
};
71

    
72
/**
73
 *  The VMGroupRoles class represents a set of ROLES stored in a Template
74
 */
75
class VMGroupRoles
76
{
77
public:
78
    VMGroupRoles():roles_template(false,'=',"ROLES"), next_role(0){};
79

    
80
    ~VMGroupRoles()
81
    {
82
        by_id.delete_roles();
83
    };
84

    
85
    /**
86
     * Function to print the VMGroupRoles into a string in XML format
87
     *   @param xml the resulting XML string
88
     *   @return a reference to the generated string
89
     */
90
    std::string& to_xml(std::string& xml_str) const
91
    {
92
        return roles_template.to_xml(xml_str);
93
    }
94

    
95
    /**
96
     *  Builds the object from an xml node
97
     *    @param node for the ROLES template
98
     *    @return 0 on success, -1 otherwise
99
     */
100
    int from_xml_node(const xmlNodePtr node);
101

    
102
    /**
103
     *  Adds a new role to the set
104
     *    @param vrole VectorAttribute of the role
105
     *    @param error string if any
106
     *
107
     *    @return 0 on success
108
     */
109
    int add_role(VectorAttribute * vrole, string& error);
110

    
111
    /**
112
     *  Check that a key list is defined in the name map
113
     *    @param key_str separated list of keys
114
     *    @param true if the keys are in the map
115
     */
116
    bool in_map(const std::string& key_str)
117
    {
118
        return by_name.in_map(key_str);
119
    }
120

    
121
private:
122
    /**
123
     *  A role map indexed by different key types
124
     */
125
    template<class T>
126
    class RoleMap
127
    {
128
    public:
129
        /**
130
         *  Inserts a new role in the map
131
         *    @param k the key
132
         *    @param r pointer to yhe VMGroupRole
133
         *    @return true if the role was successfully inserted
134
         */
135
        bool insert(const T& k, VMGroupRole * r)
136
        {
137
            std::pair<T, VMGroupRole *> rpair(k, r);
138
            std::pair<roles_it, bool> rc;
139

    
140
            rc = roles.insert(rpair);
141

    
142
            return rc.second;
143
        }
144

    
145
        /**
146
         *  Frees the memory associated to the map and clears it
147
         */
148
        void delete_roles()
149
        {
150
            for (roles_it it = roles.begin() ; it != roles.end() ; ++it )
151
            {
152
                delete it->second;
153
            }
154

    
155
            clear();
156
        }
157

    
158
        /**
159
         *  Clears the contents of the map
160
         */
161
        void clear()
162
        {
163
            roles.clear();
164
        }
165

    
166
        size_t erase(const T& k)
167
        {
168
            return roles.erase(k);
169
        }
170

    
171
        /**
172
         *  Check id a set of keys are in the map.
173
         *    @param key_str a comma separated list of keys
174
         *    @return true if all the keys are in the map
175
         */
176
        bool in_map(const string& key_str)
177
        {
178
            std::set<T> key_set;
179
            typename std::set<T>::iterator it;
180

    
181
            one_util::split_unique(key_str, ',', key_set);
182

    
183
            if ( key_set.empty() )
184
            {
185
                return true;
186
            }
187

    
188
            for ( it = key_set.begin(); it != key_set.end() ; ++it )
189
            {
190
                string rname = one_util::trim(*it);
191

    
192
                if ( roles.find(rname) == roles.end() )
193
                {
194
                    return false;
195
                }
196
            }
197

    
198
            return true;
199
        }
200

    
201
    private:
202
        typedef typename std::map<T, VMGroupRole *>::iterator roles_it;
203

    
204
        std::map<T, VMGroupRole *> roles;
205
    };
206

    
207
    /**
208
     *  The role template to store the VMGroupRole
209
     */
210
        Template roles_template;
211

    
212
    /**
213
     *  The next role id
214
     */
215
    int next_role;
216

    
217
    /**
218
     *  Map to access the roles by their name
219
     */
220
    RoleMap<std::string> by_name;
221

    
222
    /**
223
     *  Map to access the roles by their id
224
     */
225
    RoleMap<int> by_id;
226
};
227

    
228
#endif /*VMGROUP_ROLE_H*/
229