summaryrefslogtreecommitdiff
path: root/src/PVE/API2/Network/SDN/Controllers.pm
blob: bc3ec098222c055df64ab4d588b09fac2cb55902 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package PVE::API2::Network::SDN::Controllers;

use strict;
use warnings;

use PVE::SafeSyslog;
use PVE::Tools qw(extract_param);
use PVE::Cluster qw(cfs_read_file cfs_write_file);
use PVE::Network::SDN;
use PVE::Network::SDN::Zones;
use PVE::Network::SDN::Controllers;
use PVE::Network::SDN::Controllers::Plugin;
use PVE::Network::SDN::Controllers::EvpnPlugin;
use PVE::Network::SDN::Controllers::BgpPlugin;
use PVE::Network::SDN::Controllers::FaucetPlugin;

use Storable qw(dclone);
use PVE::JSONSchema qw(get_standard_option);
use PVE::RPCEnvironment;

use PVE::RESTHandler;

use base qw(PVE::RESTHandler);

my $sdn_controllers_type_enum = PVE::Network::SDN::Controllers::Plugin->lookup_types();

my $api_sdn_controllers_config = sub {
    my ($cfg, $id) = @_;

    my $scfg = dclone(PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id));
    $scfg->{controller} = $id;
    $scfg->{digest} = $cfg->{digest};

    return $scfg;
};

my $CONTROLLER_PROPERTIES = {
    asn => {
        type => 'integer',
        description => 'The local ASN of the controller. BGP & EVPN only.',
        optional => 1,
        minimum => 0,
        maximum => 4294967295,
    },
    node => {
        type => 'string',
        optional => 1,
        description => 'Node(s) where this controller is active.',
    },
    peers => {
        type => 'string',
        optional => 1,
        description => 'Comma-separated list of the peers IP addresses.',
    },
    'bgp-multipath-as-relax' => {
        type => 'boolean',
        optional => 1,
        description =>
            'Consider different AS paths of equal length for multipath computation. BGP only.',
    },
    ebgp => {
        type => 'boolean',
        optional => 1,
        description => "Enable eBGP (remote-as external). BGP only.",
    },
    'ebgp-multihop' => {
        type => 'integer',
        optional => 1,
        description =>
            "Set maximum amount of hops for eBGP peers. Needs ebgp set to 1. BGP only.",
    },
    loopback => {
        description =>
            "Name of the loopback/dummy interface that provides the Router-IP. BGP only.",
        optional => 1,
        type => 'string',
    },
    'isis-domain' => {
        description => "Name of the IS-IS domain. IS-IS only.",
        optional => 1,
        type => 'string',
    },
    'isis-ifaces' => {
        description =>
            "Comma-separated list of interfaces where IS-IS should be active. IS-IS only.",
        optional => 1,
        type => 'string',
        format => 'pve-iface-list',
    },
    'isis-net' => {
        description => "Network Entity title for this node in the IS-IS network. IS-IS only.",
        optional => 1,
        type => 'string',
        format => 'pve-sdn-isis-net',
    },
};

__PACKAGE__->register_method({
    name => 'index',
    path => '',
    method => 'GET',
    description => "SDN controllers index.",
    permissions => {
        description =>
            "Only list entries where you have 'SDN.Audit' or 'SDN.Allocate' permissions on '/sdn/controllers/<controller>'",
        user => 'all',
    },
    parameters => {
        additionalProperties => 0,
        properties => {
            type => {
                description => "Only list sdn controllers of specific type",
                type => 'string',
                enum => $sdn_controllers_type_enum,
                optional => 1,
            },
            running => {
                type => 'boolean',
                optional => 1,
                description => "Display running config.",
            },
            pending => {
                type => 'boolean',
                optional => 1,
                description => "Display pending config.",
            },
        },
    },
    returns => {
        type => 'array',
        items => {
            type => "object",
            properties => {
                digest => {
                    type => 'string',
                    description => 'Digest of the controller section.',
                    optional => 1,
                },
                state => get_standard_option('pve-sdn-config-state'),
                controller => {
                    type => 'string',
                    description => 'Name of the controller.',
                },
                type => {
                    type => 'string',
                    description => 'Type of the controller',
                    enum => PVE::Network::SDN::Controllers::Plugin->lookup_types(),
                },
                pending => {
                    type => 'object',
                    description =>
                        'Changes that have not yet been applied to the running configuration.',
                    optional => 1,
                    properties => $CONTROLLER_PROPERTIES,
                },
                %$CONTROLLER_PROPERTIES,
            },
        },
        links => [{ rel => 'child', href => "{controller}" }],
    },
    code => sub {
        my ($param) = @_;

        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();

        my $cfg = {};
        if ($param->{pending}) {
            my $running_cfg = PVE::Network::SDN::running_config();
            my $config = PVE::Network::SDN::Controllers::config();
            $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'controllers');
        } elsif ($param->{running}) {
            my $running_cfg = PVE::Network::SDN::running_config();
            $cfg = $running_cfg->{controllers};
        } else {
            $cfg = PVE::Network::SDN::Controllers::config();
        }

        my @sids = PVE::Network::SDN::Controllers::sdn_controllers_ids($cfg);
        my $res = [];
        foreach my $id (@sids) {
            my $privs = ['SDN.Audit', 'SDN.Allocate'];
            next if !$rpcenv->check_any($authuser, "/sdn/controllers/$id", $privs, 1);

            my $scfg = &$api_sdn_controllers_config($cfg, $id);
            next if $param->{type} && $param->{type} ne $scfg->{type};

            my $plugin_config = $cfg->{ids}->{$id};
            my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($plugin_config->{type});
            push @$res, $scfg;
        }

        return $res;
    },
});

__PACKAGE__->register_method({
    name => 'read',
    path => '{controller}',
    method => 'GET',
    description => "Read sdn controller configuration.",
    permissions => {
        check => ['perm', '/sdn/controllers/{controller}', ['SDN.Allocate']],
    },

    parameters => {
        additionalProperties => 0,
        properties => {
            controller => get_standard_option('pve-sdn-controller-id'),
            running => {
                type => 'boolean',
                optional => 1,
                description => "Display running config.",
            },
            pending => {
                type => 'boolean',
                optional => 1,
                description => "Display pending config.",
            },
        },
    },
    returns => {
        properties => {
            digest => {
                type => 'string',
                description => 'Digest of the controller section.',
                optional => 1,
            },
            state => get_standard_option('pve-sdn-config-state'),
            controller => {
                type => 'string',
                description => 'Name of the controller.',
            },
            type => {
                type => 'string',
                description => 'Type of the controller',
                enum => PVE::Network::SDN::Controllers::Plugin->lookup_types(),
            },
            pending => {
                type => 'object',
                description =>
                    'Changes that have not yet been applied to the running configuration.',
                optional => 1,
                properties => $CONTROLLER_PROPERTIES,
            },
            %$CONTROLLER_PROPERTIES,
        },
    },
    code => sub {
        my ($param) = @_;

        my $cfg = {};
        if ($param->{pending}) {
            my $running_cfg = PVE::Network::SDN::running_config();
            my $config = PVE::Network::SDN::Controllers::config();
            $cfg = PVE::Network::SDN::pending_config($running_cfg, $config, 'controllers');
        } elsif ($param->{running}) {
            my $running_cfg = PVE::Network::SDN::running_config();
            $cfg = $running_cfg->{controllers};
        } else {
            $cfg = PVE::Network::SDN::Controllers::config();
        }

        return &$api_sdn_controllers_config($cfg, $param->{controller});
    },
});

__PACKAGE__->register_method({
    name => 'create',
    protected => 1,
    path => '',
    method => 'POST',
    description => "Create a new sdn controller object.",
    permissions => {
        check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
    },
    parameters => PVE::Network::SDN::Controllers::Plugin->createSchema(
        undef,
        {
            'lock-token' => get_standard_option('pve-sdn-lock-token'),
        },
    ),
    returns => { type => 'null' },
    code => sub {
        my ($param) = @_;

        my $type = extract_param($param, 'type');
        my $id = extract_param($param, 'controller');
        my $lock_token = extract_param($param, 'lock-token');

        my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($type);
        my $opts = $plugin->check_config($id, $param, 1, 1);

        # create /etc/pve/sdn directory
        PVE::Cluster::check_cfs_quorum();
        mkdir("/etc/pve/sdn");

        PVE::Network::SDN::lock_sdn_config(
            sub {

                my $controller_cfg = PVE::Network::SDN::Controllers::config();

                my $scfg = undef;
                if (
                    $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config(
                        $controller_cfg, $id, 1,
                    )
                ) {
                    die "sdn controller object ID '$id' already defined\n";
                }

                $controller_cfg->{ids}->{$id} = $opts;
                $plugin->on_update_hook($id, $controller_cfg);

                PVE::Network::SDN::Controllers::write_config($controller_cfg);

            },
            "create sdn controller object failed",
            $lock_token,
        );

        return undef;
    },
});

__PACKAGE__->register_method({
    name => 'update',
    protected => 1,
    path => '{controller}',
    method => 'PUT',
    description => "Update sdn controller object configuration.",
    permissions => {
        check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
    },
    parameters => PVE::Network::SDN::Controllers::Plugin->updateSchema(
        undef,
        {
            'lock-token' => get_standard_option('pve-sdn-lock-token'),
        },
    ),
    returns => { type => 'null' },
    code => sub {
        my ($param) = @_;

        my $id = extract_param($param, 'controller');
        my $digest = extract_param($param, 'digest');
        my $delete = extract_param($param, 'delete');
        my $lock_token = extract_param($param, 'lock-token');

        PVE::Network::SDN::lock_sdn_config(
            sub {

                my $controller_cfg = PVE::Network::SDN::Controllers::config();

                PVE::SectionConfig::assert_if_modified($controller_cfg, $digest);

                my $scfg =
                    PVE::Network::SDN::Controllers::sdn_controllers_config($controller_cfg, $id);

                my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});
                my $opts = $plugin->check_config($id, $param, 0, 1);

                if ($delete) {
                    $delete = [PVE::Tools::split_list($delete)];
                    my $options = $plugin->private()->{options}->{ $scfg->{type} };
                    PVE::SectionConfig::delete_from_config($scfg, $options, $opts, $delete);
                }

                for my $k (keys %{$opts}) {
                    $scfg->{$k} = $opts->{$k};
                }

                $plugin->on_update_hook($id, $controller_cfg);

                PVE::Network::SDN::Controllers::write_config($controller_cfg);

            },
            "update sdn controller object failed",
            $lock_token,
        );

        return undef;
    },
});

__PACKAGE__->register_method({
    name => 'delete',
    protected => 1,
    path => '{controller}',
    method => 'DELETE',
    description => "Delete sdn controller object configuration.",
    permissions => {
        check => ['perm', '/sdn/controllers', ['SDN.Allocate']],
    },
    parameters => {
        additionalProperties => 0,
        properties => {
            controller => get_standard_option(
                'pve-sdn-controller-id',
                {
                    completion => \&PVE::Network::SDN::Controllers::complete_sdn_controllers,
                },
            ),
            'lock-token' => get_standard_option('pve-sdn-lock-token'),
        },
    },
    returns => { type => 'null' },
    code => sub {
        my ($param) = @_;

        my $id = extract_param($param, 'controller');
        my $lock_token = extract_param($param, 'lock-token');

        PVE::Network::SDN::lock_sdn_config(
            sub {

                my $cfg = PVE::Network::SDN::Controllers::config();

                my $scfg = PVE::Network::SDN::Controllers::sdn_controllers_config($cfg, $id);

                my $plugin = PVE::Network::SDN::Controllers::Plugin->lookup($scfg->{type});

                my $zone_cfg = PVE::Network::SDN::Zones::config();

                $plugin->on_delete_hook($id, $zone_cfg);

                delete $cfg->{ids}->{$id};
                PVE::Network::SDN::Controllers::write_config($cfg);

            },
            "delete sdn controller object failed",
            $lock_token,
        );

        return undef;
    },
});

1;