]> git.proxmox.com Git - pve-network.git/blob - PVE/Network/SDN/Controllers/Plugin.pm
ce35574a025717cfa98080369f4609940a3f36cd
[pve-network.git] / PVE / Network / SDN / Controllers / Plugin.pm
1 package PVE::Network::SDN::Controllers::Plugin;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools;
7 use PVE::JSONSchema;
8 use PVE::Cluster;
9
10 use Data::Dumper;
11 use PVE::JSONSchema qw(get_standard_option);
12 use base qw(PVE::SectionConfig);
13
14 PVE::Cluster::cfs_register_file('sdn/controllers.cfg',
15 sub { __PACKAGE__->parse_config(@_); });
16
17 PVE::Cluster::cfs_register_file('sdn/controllers.cfg.new',
18 sub { __PACKAGE__->parse_config(@_); },
19 sub { __PACKAGE__->write_config(@_); });
20
21 PVE::JSONSchema::register_standard_option('pve-sdn-controller-id', {
22 description => "The SDN controller object identifier.",
23 type => 'string', format => 'pve-sdn-controller-id',
24 });
25
26 PVE::JSONSchema::register_format('pve-sdn-controller-id', \&parse_sdn_controller_id);
27 sub parse_sdn_controller_id {
28 my ($id, $noerr) = @_;
29
30 if ($id !~ m/^[a-z][a-z0-9]*[a-z0-9]$/i) {
31 return undef if $noerr;
32 die "controller ID '$id' contains illegal characters\n";
33 }
34 die "controller ID '$id' can't be more length than 10 characters\n" if length($id) > 10;
35 return $id;
36 }
37
38 my $defaultData = {
39
40 propertyList => {
41 type => {
42 description => "Plugin type.",
43 type => 'string', format => 'pve-configid',
44 type => 'string',
45 },
46 controller => get_standard_option('pve-sdn-controller-id',
47 { completion => \&PVE::Network::SDN::complete_sdn_controller }),
48 },
49 };
50
51 sub private {
52 return $defaultData;
53 }
54
55 sub parse_section_header {
56 my ($class, $line) = @_;
57
58 if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
59 my ($type, $id) = (lc($1), $2);
60 my $errmsg = undef; # set if you want to skip whole section
61 eval { PVE::JSONSchema::pve_verify_configid($type); };
62 $errmsg = $@ if $@;
63 my $config = {}; # to return additional attributes
64 return ($type, $id, $errmsg, $config);
65 }
66 return undef;
67 }
68
69 sub generate_sdn_config {
70 my ($class, $plugin_config, $node, $data, $ctime) = @_;
71
72 die "please implement inside plugin";
73 }
74
75 sub generate_controller_config {
76 my ($class, $plugin_config, $controller, $id, $uplinks, $config) = @_;
77
78 die "please implement inside plugin";
79 }
80
81 sub generate_controller_vnet_config {
82 my ($class, $plugin_config, $controller, $zoneid, $vnetid, $config) = @_;
83
84 }
85
86 sub write_controller_config {
87 my ($class, $plugin_config, $config) = @_;
88
89 die "please implement inside plugin";
90 }
91
92 sub controller_reload {
93 my ($class) = @_;
94
95 die "please implement inside plugin";
96 }
97
98 sub on_delete_hook {
99 my ($class, $controllerid, $zone_cfg) = @_;
100
101 # do nothing by default
102 }
103
104 sub on_update_hook {
105 my ($class, $controllerid, $controller_cfg) = @_;
106
107 # do nothing by default
108 }
109
110 #helpers
111
112 #to be move to Network.pm helper
113 sub get_first_local_ipv4_from_interface {
114 my ($interface) = @_;
115
116 my $cmd = ['/sbin/ip', 'address', 'show', 'dev', $interface];
117
118 my $IP = "";
119
120 my $code = sub {
121 my $line = shift;
122
123 if ($line =~ m!^\s*inet\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
124 $IP = $1;
125 return;
126 }
127 };
128
129 PVE::Tools::run_command($cmd, outfunc => $code);
130
131 return $IP;
132 }
133
134 1;