Statistics
| Branch: | Tag: | Revision:

one / include / ClusterPool.h @ 6329d8b3

History | View | Annotate | Download (4.48 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             */
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 CLUSTER_POOL_H_
18
#define CLUSTER_POOL_H_
19

    
20
#include "Cluster.h"
21
#include "Host.h"
22
#include "SqlDB.h"
23

    
24
using namespace std;
25

    
26
/**
27
 *  A cluster helper class. It is not a normal PoolSQL,
28
 *  but a series of static methods related to clusters.
29
 */
30
class ClusterPool : public PoolSQL
31
{
32
public:
33
    ClusterPool(SqlDB * db);
34

    
35
    ~ClusterPool(){};
36

    
37
    /**
38
     *  Removes the host from the given cluster setting the default one.
39
     *    @param host The host to assign
40
     *
41
     *    @return 0 on success
42
     */
43
    int set_default_cluster(Host * host)
44
    {
45
        return host->set_cluster(ClusterPool::DEFAULT_CLUSTER_ID);
46
    };
47

    
48
    /**
49
     *  Cluster name for the default cluster
50
     */
51
    static const string DEFAULT_CLUSTER_NAME;
52

    
53
    /**
54
     *  Cluster id for the default cluster
55
     */
56
    static const int DEFAULT_CLUSTER_ID;
57

    
58
    /* ---------------------------------------------------------------------- */
59
    /* Methods for DB management                                              */
60
    /* ---------------------------------------------------------------------- */
61

    
62
    /**
63
     *  Allocates a new cluster in the pool
64
     *    @param clid the id assigned to the cluster
65
     *    @return the id assigned to the cluster or -1 in case of failure
66
     */
67
    int allocate(int * oid, string name, string& error_str);
68

    
69
    /**
70
     *  Function to get a Cluster from the pool, if the object is not in memory
71
     *  it is loaded from the DB
72
     *    @param oid Cluster unique id
73
     *    @param lock locks the Cluster mutex
74
     *    @return a pointer to the Cluster, 0 if the Cluster could not be loaded
75
     */
76
    Cluster * get(int oid, bool lock)
77
    {
78
        return static_cast<Cluster *>(PoolSQL::get(oid,lock));
79
    };
80

    
81
    /**
82
     *  Gets an object from the pool (if needed the object is loaded from the
83
     *  database).
84
     *   @param name of the object
85
     *   @param lock locks the object if true
86
     *
87
     *   @return a pointer to the object, 0 in case of failure
88
     */
89
    Cluster * get(const string& name, bool lock)
90
    {
91
        return static_cast<Cluster *>(PoolSQL::get(name,-1,lock));
92
    };
93

    
94
    /** Update a particular Cluster
95
     *    @param user pointer to Cluster
96
     *    @return 0 on success
97
     */
98
    int update(Cluster * cluster)
99
    {
100
        return cluster->update(db);
101
    };
102

    
103
    /**
104
     *  Drops the Cluster from the data base. The object mutex SHOULD be
105
     *  locked.
106
     *    @param cluster a pointer to the object
107
     *    @return 0 on success.
108
     */
109
    int drop(Cluster * cluster);
110

    
111
    /**
112
     *  Bootstraps the database table(s) associated to the Cluster pool
113
     */
114
    static void bootstrap(SqlDB * _db)
115
    {
116
        Cluster::bootstrap(_db);
117
    };
118

    
119
    /**
120
     *  Dumps the Cluster pool in XML format. A filter can be also added to the
121
     *  query
122
     *  @param oss the output stream to dump the pool contents
123
     *  @param where filter for the objects, defaults to all
124
     *
125
     *  @return 0 on success
126
     */
127
    int dump(ostringstream& oss, const string& where)
128
    {
129
        return PoolSQL::dump(oss, "CLUSTER_POOL", Cluster::table, where);
130
    };
131

    
132
private:
133

    
134
    /**
135
     *  Factory method to produce objects
136
     *    @return a pointer to the new object
137
     */
138
    PoolObjectSQL * create()
139
    {
140
        return new Cluster(-1,"");
141
    };
142
};
143

    
144
#endif /*CLUSTER_POOL_H_*/