Coverage for manila/network/__init__.py: 93%

59 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2026-02-18 22:19 +0000

1# Copyright 2013 OpenStack Foundation 

2# Copyright 2014 Mirantis Inc. 

3# All Rights Reserved. 

4# 

5# Licensed under the Apache License, Version 2.0 (the "License"); you may 

6# not use this file except in compliance with the License. You may obtain 

7# a copy of the License at 

8# 

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. 

16import abc 

17 

18from oslo_config import cfg 

19from oslo_utils import importutils 

20 

21from manila.db import base as db_base 

22from manila import exception 

23from manila.i18n import _ 

24 

25network_opts = [ 

26 cfg.StrOpt( 

27 'network_api_class', 

28 default='manila.network.neutron.' 

29 'neutron_network_plugin.NeutronNetworkPlugin', 

30 help='The full class name of the Networking API class to use.'), 

31] 

32 

33network_base_opts = [ 

34 cfg.BoolOpt( 

35 'network_plugin_ipv4_enabled', 

36 default=True, 

37 help="Whether to support IPv4 network resource, Default=True."), 

38 cfg.BoolOpt( 

39 'network_plugin_ipv6_enabled', 

40 default=False, 

41 help="Whether to support IPv6 network resource, Default=False. " 

42 "If this option is True, the value of " 

43 "'network_plugin_ipv4_enabled' will be ignored."), 

44] 

45 

46CONF = cfg.CONF 

47 

48 

49def API(config_group_name=None, label='user'): 

50 """Selects class and config group of network plugin. 

51 

52 :param config_group_name: name of config group to be used for 

53 registration of networking opts. 

54 :returns: instance of networking plugin class 

55 """ 

56 CONF.register_opts(network_opts, group=config_group_name) 

57 if config_group_name: 

58 network_api_class = getattr(CONF, config_group_name).network_api_class 

59 else: 

60 network_api_class = CONF.network_api_class 

61 cls = importutils.import_class(network_api_class) 

62 return cls(config_group_name=config_group_name, label=label) 

63 

64 

65class NetworkBaseAPI(db_base.Base, metaclass=abc.ABCMeta): 

66 """User network plugin for setting up main net interfaces.""" 

67 

68 def __init__(self, config_group_name=None, db_driver=None): 

69 if config_group_name: 

70 CONF.register_opts(network_base_opts, 

71 group=config_group_name) 

72 else: 

73 CONF.register_opts(network_base_opts) 

74 self.configuration = getattr(CONF, 

75 str(config_group_name), CONF) 

76 super(NetworkBaseAPI, self).__init__(db_driver=db_driver) 

77 

78 def _verify_share_network(self, share_server_id, share_network): 

79 if share_network is None: 

80 msg = _("'Share network' is not provided for setting up " 

81 "network interfaces for 'Share server' " 

82 "'%s'.") % share_server_id 

83 raise exception.NetworkBadConfigurationException(reason=msg) 

84 

85 def _verify_share_network_subnet(self, share_server_id, 

86 share_network_subnet): 

87 if share_network_subnet is None: 

88 msg = _("'Share network subnet' is not provided for setting up " 

89 "network interfaces for 'Share server' " 

90 "'%s'.") % share_server_id 

91 raise exception.NetworkBadConfigurationException(reason=msg) 

92 

93 def update_network_allocation(self, context, share_server): 

94 """Update network allocation. 

95 

96 Optional method to be called by the manager after share server creation 

97 which can be overloaded in case the port state has to be updated. 

98 

99 :param context: RequestContext object 

100 :param share_server: share server object 

101 :return: list of updated ports or None if nothing was updated 

102 """ 

103 

104 @abc.abstractmethod 

105 def allocate_network(self, context, share_server, share_network=None, 

106 share_network_subnet=None, **kwargs): 

107 pass 

108 

109 @abc.abstractmethod 

110 def deallocate_network(self, context, share_server_id, share_network=None, 

111 share_network_subnet=None): 

112 pass 

113 

114 @abc.abstractmethod 

115 def manage_network_allocations( 

116 self, context, allocations, share_server, share_network=None, 

117 share_network_subnet=None): 

118 pass 

119 

120 @abc.abstractmethod 

121 def unmanage_network_allocations(self, context, share_server_id): 

122 pass 

123 

124 @property 

125 def enabled_ip_versions(self): 

126 if not hasattr(self, '_enabled_ip_versions'): 

127 self._enabled_ip_versions = set() 

128 if self.configuration.network_plugin_ipv6_enabled: 

129 self._enabled_ip_versions.add(6) 

130 if self.configuration.network_plugin_ipv4_enabled: 

131 self._enabled_ip_versions.add(4) 

132 if not self._enabled_ip_versions: 

133 msg = _("Either 'network_plugin_ipv4_enabled' or " 

134 "'network_plugin_ipv6_enabled' " 

135 "should be configured to 'True'.") 

136 raise exception.NetworkBadConfigurationException(reason=msg) 

137 return self._enabled_ip_versions 

138 

139 @abc.abstractmethod 

140 def include_network_info(self, share_network_subnet): 

141 """Includes share-network-subnet with plugin specific data.""" 

142 pass