Statistics
| Branch: | Tag: | Revision:

one / src / ipamm / IPAMManager.cc @ 1772a358

History | View | Annotate | Download (7.12 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2015, 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
#include "IPAMManager.h"
18
#include "IPAMRequest.h"
19
#include "NebulaLog.h"
20

    
21
/* -------------------------------------------------------------------------- */
22
/* -------------------------------------------------------------------------- */
23

    
24
const char * IPAMManager::ipam_driver_name = "ipam_exe";
25

    
26
/* -------------------------------------------------------------------------- */
27
/* -------------------------------------------------------------------------- */
28

    
29
extern "C" void * ipamm_action_loop(void *arg)
30
{
31
    IPAMManager * ipamm;
32

    
33
    if ( arg == 0 )
34
    {
35
        return 0;
36
    }
37

    
38
    ipamm = static_cast<IPAMManager *>(arg);
39

    
40
    NebulaLog::log("IpamM",Log::INFO,"IPAM Manager started.");
41

    
42
    ipamm->am.loop(ipamm->timer_period, 0);
43

    
44
    NebulaLog::log("IpamM",Log::INFO,"IPAM Manager stopped.");
45

    
46
    return 0;
47
}
48

    
49
/* -------------------------------------------------------------------------- */
50

    
51
int IPAMManager::start()
52
{
53
    int            rc;
54
    pthread_attr_t pattr;
55

    
56
    rc = MadManager::start();
57

    
58
    if ( rc != 0 )
59
    {
60
        return -1;
61
    }
62

    
63
    NebulaLog::log("IPM",Log::INFO,"Starting IPAM Manager...");
64

    
65
    pthread_attr_init(&pattr);
66
    pthread_attr_setdetachstate(&pattr, PTHREAD_CREATE_JOINABLE);
67

    
68
    rc = pthread_create(&ipamm_thread, &pattr, ipamm_action_loop, (void *)this);
69

    
70
    return rc;
71
}
72

    
73
/* -------------------------------------------------------------------------- */
74
/* -------------------------------------------------------------------------- */
75

    
76
void IPAMManager::trigger(Actions action, IPAMRequest * request)
77
{
78
    string  aname;
79

    
80
    switch (action)
81
    {
82
    case REGISTER_ADDRESS_RANGE:
83
        aname = "REGISTER_ADDRESS_RANGE";
84
        break;
85

    
86
    case ALLOCATE_ADDRESS:
87
        aname = "ALLOCATE_ADDRESS";
88
        break;
89

    
90
    case GET_ADDRESS:
91
        aname = "GET_ADDRESS";
92
        break;
93

    
94
    case FREE_ADDRESS:
95
        aname = "FREE_ADDRESS";
96
        break;
97

    
98
    case FINALIZE:
99
        aname = ACTION_FINALIZE;
100
        break;
101

    
102
    default:
103
        return;
104
    }
105

    
106
    am.trigger(aname,request);
107
}
108

    
109
/* -------------------------------------------------------------------------- */
110
/* -------------------------------------------------------------------------- */
111

    
112
void IPAMManager::do_action(const string &action, void * arg)
113
{
114
    IPAMRequest * request = static_cast<IPAMRequest *>(arg);
115

    
116
    if (action == "REGISTER_ADDRESS_RANGE" && request != 0)
117
    {
118
        register_address_range_action(request);
119
    }
120
    else if (action == "ALLOCATE_ADDRESS" && request != 0)
121
    {
122
        allocate_address_action(request);
123
    }
124
    else if (action == "GET_ADDRESS" && request != 0)
125
    {
126
        get_address_action(request);
127
    }
128
    else if (action == "FREE_ADDRESS" && request != 0)
129
    {
130
        free_address_action(request);
131
    }
132
    else if (action == ACTION_TIMER)
133
    {
134
        check_time_outs_action();
135
    }
136
    else if (action == ACTION_FINALIZE)
137
    {
138
        NebulaLog::log("IPM",Log::INFO,"Stopping IPAM Manager...");
139

    
140
        MadManager::stop();
141
    }
142
    else
143
    {
144
        ostringstream oss;
145
        oss << "Unknown action name: " << action;
146

    
147
        NebulaLog::log("IPM", Log::ERROR, oss);
148
    }
149
}
150

    
151
/* -------------------------------------------------------------------------- */
152
/* -------------------------------------------------------------------------- */
153

    
154
const IPAMManagerDriver * IPAMManager::setup_request(IPAMRequest * ir)
155
{
156
    const IPAMManagerDriver * ipammd = get();
157

    
158
    if (ipammd == 0)
159
    {
160
        ir->result  = false;
161
        ir->message = "Could not find the IPAM driver";
162

    
163
        ir->notify();
164

    
165
        return 0;
166
    }
167

    
168
    add_request(ir);
169

    
170
    return ipammd;
171
}
172

    
173
/* -------------------------------------------------------------------------- */
174

    
175
void IPAMManager::register_address_range_action(IPAMRequest * ir)
176
{
177
    std::string action_data;
178
    const IPAMManagerDriver * ipammd = setup_request(ir);
179

    
180
    if (ipammd == 0)
181
    {
182
        return;
183
    }
184

    
185
    ipammd->register_address_range(ir->id, ir->to_xml64(action_data));
186
}
187

    
188
/* -------------------------------------------------------------------------- */
189

    
190
void IPAMManager::get_address_action(IPAMRequest * ir)
191
{
192
    std::string action_data;
193
    const IPAMManagerDriver * ipammd = setup_request(ir);
194

    
195
    if (ipammd == 0)
196
    {
197
        return;
198
    }
199

    
200
    ipammd->get_address(ir->id, ir->to_xml64(action_data));
201
}
202

    
203
/* -------------------------------------------------------------------------- */
204

    
205
void IPAMManager::allocate_address_action(IPAMRequest * ir)
206
{
207
    std::string action_data;
208
    const IPAMManagerDriver * ipammd = setup_request(ir);
209

    
210
    if (ipammd == 0)
211
    {
212
        return;
213
    }
214

    
215
    ipammd->allocate_address(ir->id, ir->to_xml64(action_data));
216
}
217

    
218
/* -------------------------------------------------------------------------- */
219

    
220
void IPAMManager::free_address_action(IPAMRequest * ir)
221
{
222
    std::string action_data;
223
    const IPAMManagerDriver * ipammd = setup_request(ir);
224

    
225
    if (ipammd == 0)
226
    {
227
        return;
228
    }
229

    
230
    ipammd->free_address(ir->id, ir->to_xml64(action_data));
231
}
232

    
233
/* ************************************************************************** */
234
/* MAD Loading                                                                */
235
/* ************************************************************************** */
236

    
237
int IPAMManager::load_mads(int uid)
238
{
239
    const VectorAttribute * vattr;
240
    IPAMManagerDriver *     ipamm_driver;
241

    
242
    if ( mad_conf.size() == 0 )
243
    {
244
        return 0;
245
    }
246

    
247
    NebulaLog::log("IPM", Log::INFO, "Loading IPAM Manager driver.");
248

    
249
    vattr = dynamic_cast<const VectorAttribute *>(mad_conf[0]);
250

    
251
    if ( vattr == 0 )
252
    {
253
        NebulaLog::log("IPM", Log::ERROR, "Failed to load IPAM Manager driver.");
254
        return -1;
255
    }
256

    
257
    VectorAttribute ipam_conf("IPAM_MAD", vattr->value());
258

    
259
    ipam_conf.replace("NAME", ipam_driver_name);
260

    
261
    ipamm_driver = new IPAMManagerDriver(uid, ipam_conf.value(), false, this);
262

    
263
    if ( add(ipamm_driver) != 0 )
264
    {
265
        return -1;
266
    }
267

    
268
    NebulaLog::log("IPM", Log::INFO, "\tIPAM Manager loaded");
269

    
270
    return 0;
271
}