| 1 |
# Copyright 2016 Hewlett Packard Enterprise Development Company, LP |
| 2 |
# Copyright 2016 Red Hat Inc. |
| 3 |
# |
| 4 |
# All Rights Reserved. |
| 5 |
# |
| 6 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 |
# not use this file except in compliance with the License. You may obtain |
| 8 |
# a copy of the License at |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, software |
| 12 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 |
# License for the specific language governing permissions and limitations |
| 15 |
# under the License. |
| 16 |
|
| 17 |
from oslo_log import log as logging |
| 18 |
|
| 19 |
from neutron_lib.api import validators as lib_validators |
| 20 |
from neutron_lib.callbacks import events |
| 21 |
from neutron_lib.callbacks import registry |
| 22 |
from neutron_lib.services.qos import constants |
| 23 |
|
| 24 |
LOG = logging.getLogger(__name__) |
| 25 |
|
| 26 |
|
| 27 |
@registry.has_registry_receivers |
| 28 |
class DriverBase: |
| 29 |
|
| 30 |
def __init__(self, name, vif_types, vnic_types, |
| 31 |
supported_rules, |
| 32 |
requires_rpc_notifications=False): |
| 33 |
"""Instantiate a qos driver. |
| 34 |
|
| 35 |
:param name: driver name. |
| 36 |
:param vif_types: list of interfaces (VIFs) supported. |
| 37 |
:param vnic_types: list of vnic types supported. |
| 38 |
:param supported_rules: dict of supported rules. |
| 39 |
:param requires_rpc_notifications: indicates if this driver |
| 40 |
expects rpc push notifications to be sent from the driver. |
| 41 |
""" |
| 42 |
|
| 43 |
self.name = name |
| 44 |
self.vif_types = vif_types |
| 45 |
self.vnic_types = vnic_types |
| 46 |
self.supported_rules = supported_rules |
| 47 |
self.requires_rpc_notifications = requires_rpc_notifications |
| 48 |
|
| 49 |
@registry.receives(constants.QOS_PLUGIN, [events.AFTER_INIT]) |
| 50 |
def _register(self, resource, event, trigger, **kwargs): |
| 51 |
if self.is_loaded: |
| 52 |
# trigger is the QosServiceDriverManager |
| 53 |
trigger.register_driver(self) |
| 54 |
|
| 55 |
def is_loaded(self): |
| 56 |
"""True if the driver is active for the Neutron Server. |
| 57 |
|
| 58 |
Implement this property to determine if your driver is actively |
| 59 |
configured for this Neutron Server deployment. |
| 60 |
""" |
| 61 |
return True |
| 62 |
|
| 63 |
def is_vif_type_compatible(self, vif_type): |
| 64 |
"""True if the driver is compatible with the VIF type.""" |
| 65 |
return vif_type in self.vif_types |
| 66 |
|
| 67 |
def is_vnic_compatible(self, vnic_type): |
| 68 |
"""True if the driver is compatible with the specific VNIC type.""" |
| 69 |
return vnic_type in self.vnic_types |
| 70 |
|
| 71 |
def is_rule_supported(self, rule): |
| 72 |
supported_parameters = self.supported_rules.get(rule.rule_type) |
| 73 |
if not supported_parameters: |
| 74 |
LOG.debug("Rule type %(rule_type)s is not supported by " |
| 75 |
"%(driver_name)s", |
| 76 |
{'rule_type': rule.rule_type, |
| 77 |
'driver_name': self.name}) |
| 78 |
return False |
| 79 |
for parameter, validators in supported_parameters.items(): |
| 80 |
parameter_value = rule.get(parameter) |
| 81 |
for validator_type, validator_data in validators.items(): |
| 82 |
validator_function = lib_validators.get_validator( |
| 83 |
validator_type) |
| 84 |
validate_result = validator_function(parameter_value, |
| 85 |
validator_data) |
| 86 |
# NOTE(slaweq): validator functions returns None if data is |
| 87 |
# valid or string with reason why data is not valid |
| 88 |
if validate_result: |
| 89 |
LOG.debug("Parameter %(parameter)s=%(value)s in " |
| 90 |
"rule type %(rule_type)s is not " |
| 91 |
"supported by %(driver_name)s. " |
| 92 |
"Validate result: %(validate_result)s", |
| 93 |
{'parameter': parameter, |
| 94 |
'value': parameter_value, |
| 95 |
'rule_type': rule.rule_type, |
| 96 |
'driver_name': self.name, |
| 97 |
'validate_result': validate_result}) |
| 98 |
return False |
| 99 |
return True |
| 100 |
|
| 101 |
def validate_rule_for_port(self, context, rule, port): |
| 102 |
"""Return True/False for valid/invalid. |
| 103 |
|
| 104 |
This is only meant to be used when a rule is compatible |
| 105 |
with some ports/networks but not with others (depending on |
| 106 |
port/network properties). |
| 107 |
|
| 108 |
Returns True by default for backwards compatibility. |
| 109 |
""" |
| 110 |
return True |
| 111 |
|
| 112 |
def validate_rule_for_network(self, context, rule, network_id): |
| 113 |
"""Return True/False for valid/invalid. |
| 114 |
|
| 115 |
This is only meant to be used when a rule is compatible |
| 116 |
with some networks but not with others (depending on |
| 117 |
network properties). |
| 118 |
|
| 119 |
Returns True by default for backwards compatibility. |
| 120 |
""" |
| 121 |
return True |
| 122 |
|
| 123 |
def create_policy(self, context, policy): |
| 124 |
"""Create policy invocation. |
| 125 |
|
| 126 |
This method can be implemented by the specific driver subclass |
| 127 |
to update the backend where necessary with the specific policy |
| 128 |
information. |
| 129 |
|
| 130 |
:param context: current running context information |
| 131 |
:param policy: a QoSPolicy object being created, which will have no |
| 132 |
rules. |
| 133 |
""" |
| 134 |
|
| 135 |
def create_policy_precommit(self, context, policy): |
| 136 |
"""Create policy precommit. |
| 137 |
|
| 138 |
This method can be implemented by the specific driver subclass |
| 139 |
to handle the precommit event of a policy that is being created. |
| 140 |
|
| 141 |
:param context: current running context information |
| 142 |
:param policy: a QoSPolicy object being created, which will have no |
| 143 |
rules. |
| 144 |
""" |
| 145 |
|
| 146 |
def update_policy(self, context, policy): |
| 147 |
"""Update policy invocation. |
| 148 |
|
| 149 |
This method can be implemented by the specific driver subclass |
| 150 |
to update the backend where necessary. |
| 151 |
|
| 152 |
:param context: current running context information |
| 153 |
:param policy: a QoSPolicy object being updated. |
| 154 |
""" |
| 155 |
|
| 156 |
def update_policy_precommit(self, context, policy): |
| 157 |
"""Update policy precommit. |
| 158 |
|
| 159 |
This method can be implemented by the specific driver subclass |
| 160 |
to handle update precommit event of a policy that is being updated. |
| 161 |
|
| 162 |
:param context: current running context information |
| 163 |
:param policy: a QoSPolicy object being updated. |
| 164 |
""" |
| 165 |
|
| 166 |
def delete_policy(self, context, policy): |
| 167 |
"""Delete policy invocation. |
| 168 |
|
| 169 |
This method can be implemented by the specific driver subclass |
| 170 |
to delete the backend policy where necessary. |
| 171 |
|
| 172 |
:param context: current running context information |
| 173 |
:param policy: a QoSPolicy object being deleted |
| 174 |
""" |
| 175 |
|
| 176 |
def delete_policy_precommit(self, context, policy): |
| 177 |
"""Delete policy precommit. |
| 178 |
|
| 179 |
This method can be implemented by the specific driver subclass |
| 180 |
to handle delete precommit event of a policy that is being deleted. |
| 181 |
|
| 182 |
:param context: current running context information |
| 183 |
:param policy: a QoSPolicy object being deleted |
| 184 |
""" |