pin keystoneauth version
[tungstenfabric/tf-neutron-plugin.git] / neutron_plugin_contrail / plugins / opencontrail / services / trunk / plugin.py
1 # Copyright 2018 Juniper Networks.  All rights reserved.
2 #
3 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
4 #    not use this file except in compliance with the License. You may obtain
5 #    a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #    Unless required by applicable law or agreed to in writing, software
10 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 #    License for the specific language governing permissions and limitations
13 #    under the License.
14
15 from oslo_log import log as logging
16
17 from neutron_lib.services import base as service_base
18 from neutron_lib.api.definitions import trunk as trunk_apidef
19 from neutron_lib.plugins import directory
20
21 LOG = logging.getLogger(__name__)
22
23
24 class TrunkPlugin(service_base.ServicePluginBase):
25     """Implements Contrail Neutron Trunk Service plugin."""
26
27     supported_extension_aliases = [trunk_apidef.ALIAS]
28
29     def __init__(self):
30         super(TrunkPlugin, self).__init__()
31
32     @property
33     def _core_plugin(self):
34         return directory.get_plugin()
35
36     @classmethod
37     def get_plugin_type(cls):
38         return 'trunk'
39
40     def get_plugin_description(self):
41         return 'Trunk port service plugin'
42
43     def create_trunk(self, context, trunk):
44         return self._core_plugin._create_resource(
45             'trunk', context, trunk)
46
47     def get_trunk(self, context, id, fields=None):
48         """Return information for the specified trunk."""
49         return self._core_plugin._get_resource(
50             'trunk', context, id, fields)
51
52     def get_trunks(self, context, filters=None, fields=None):
53         return self._core_plugin._list_resource(
54             'trunk', context, filters, fields)
55
56     def get_subports(self, context, trunk_id, fields=None):
57         """Return subports for the specified trunk."""
58         trunk = self.get_trunk(context, trunk_id, fields=['sub_ports'])
59         return {'sub_ports': trunk['sub_ports']}
60
61     def update_trunk(self, context, id, trunk):
62         return self._core_plugin._update_resource(
63             'trunk',
64             context,
65             id,
66             {'trunk': trunk})
67
68     def delete_trunk(self, context, id):
69         self._core_plugin._delete_resource('trunk', context, id)
70
71     def add_subports(self, context, trunk_id, subports):
72         self._add_or_remove_subports(context, 'ADD_SUBPORTS', trunk_id,
73                                      subports)
74
75     def remove_subports(self, context, trunk_id, subports):
76         self._add_or_remove_subports(context, 'REMOVE_SUBPORTS', trunk_id,
77                                      subports)
78
79     def _add_or_remove_subports(self, context, action, trunk_id, subports):
80         res_dict = self._core_plugin._encode_resource(resource_id=trunk_id,
81                                                       resource=subports)
82         status_code, res_info = self._core_plugin._request_backend(
83             context, res_dict, 'trunk', action)
84         res_dicts = self._core_plugin._transform_response(
85             status_code, info=res_info, obj_name='trunk')
86         LOG.debug("Trunk %(action)s(): trunk_id: %(trunk_id)s "
87                   "subports: %(subports)r",
88                   {'action': action.lower(), 'trunk_id': trunk_id,
89                    'trunk': trunk_id})
90
91         return res_dicts