deb build system
[opencloud-plugin.git] / opencloud_neutron_plugin.py
1 from neutron.plugins.openvswitch.ovs_neutron_plugin import *
2 from neutron.extensions import nat
3 import opencloud_db_v2
4
5 """
6 mkdir /usr/lib/python2.7/dist-packages/neutron/plugins/opencloud
7 ln -s /usr/share/pyshared/neutron/plugins/opencloud/opencloud_neutron_plugin.py /usr/lib/python2.7/dist-packages/neutron/plugins/opencloud/opencloud_neutron_plugin.py
8 ln -s /usr/share/pyshared/neutron/plugins/opencloud/opencloud_models_v2.py /usr/lib/python2.7/dist-packages/neutron/plugins/opencloud/opencloud_models_v2.py\r
9 ln -s /usr/share/pyshared/neutron/plugins/opencloud/opencloud_db_v2.py /usr/lib/python2.7/dist-packages/neutron/plugins/opencloud/opencloud_db_v2.py
10 ln -s /usr/share/pyshared/neutron/plugins/opencloud/__init__.py /usr/lib/python2.7/dist-packages/neutron/plugins/opencloud/__init__.py
11 """
12
13 class OpenCloudPluginV2(OVSNeutronPluginV2):
14     _supported_extension_aliases = OVSNeutronPluginV2._supported_extension_aliases + ["nat"]
15
16     def _extend_port_dict_nat(self, context, port):
17         forward = opencloud_db_v2.get_port_forwarding(context.session, port['id'])
18         if forward:
19             port[nat.FORWARD_PORTS] = forward
20         else:
21             port[nat.FORWARD_PORTS] = None
22
23     def _process_nat_update(self, context, attrs, id):
24         forward_ports = attrs.get(nat.FORWARD_PORTS)
25         forward_ports_set = attributes.is_attr_set(forward_ports)
26
27         if not forward_ports_set:
28             return None
29
30         # LOG.info("forward ports %s" % forward_ports)
31         valid_protocols = ["tcp", "udp"]
32         for entry in forward_ports:
33             if not isinstance(entry, dict):
34                 msg = _("nat:forward_ports: must specify a list of dicts (ex: 'l4_protocol=tcp,l4_port=80')")
35                 raise q_exc.InvalidInput(error_message=msg)
36             if not ("l4_protocol" in entry and "l4_port" in entry):
37                 msg = _("nat:forward_ports: dict is missing l4_protocol and l4_port (ex: 'l4_protocol=tcp,l4_port=80')")
38                 raise q_exc.InvalidInput(error_message=msg)
39             if entry['l4_protocol'] not in valid_protocols:
40                 msg = _("nat:forward_ports: invalid protocol (only tcp and udp allowed)")
41                 raise q_exc.InvalidInput(error_message=msg)
42
43             l4_port = entry['l4_port']
44             if ":" in l4_port:
45                 try:
46                     (first, last) = l4_port.split(":")
47                     first = int(first)
48                     last = int(last)
49                 except:
50                     msg = _("nat:forward_ports: l4_port range must be integer:integer")
51                     raise q_exc.InvalidInput(error_message=msg)
52             else:
53                 try:
54                     l4_port = int(l4_port)
55                 except:
56                     msg = _("nat:forward_ports: l4_port must be an integer")
57                     raise q_exc.InvalidInput(error_message=msg)
58
59         return forward_ports
60
61     def get_port(self, context, id, fields=None):
62         session = context.session
63         with session.begin(subtransactions=True):
64             port = super(OpenCloudPluginV2, self).get_port(context, id, None)
65             self._extend_port_dict_nat(context, port)
66         return self._fields(port, fields)
67
68     def get_ports(self, context, filters=None, fields=None):
69         session = context.session
70         with session.begin(subtransactions=True):
71             ports = super(OpenCloudPluginV2, self).get_ports(context, filters,
72                                                           None)
73             for port in ports:
74                 self._extend_port_dict_nat(context, port)
75
76         return [self._fields(port, fields) for port in ports]
77
78     def update_port(self, context, id, port):
79         forward_ports = self._process_nat_update(context, port['port'], id)
80         session = context.session
81         with session.begin(subtransactions=True):
82             updated_port = super(OpenCloudPluginV2, self).update_port(context, id, port)
83             if forward_ports:
84                 opencloud_db_v2.clear_port_forwarding(session, updated_port['id'])\r
85                 opencloud_db_v2.add_port_forwarding(session, updated_port['id'], forward_ports)\r
86                 self._extend_port_dict_nat(context, updated_port)
87
88         return updated_port