~nova-network-poc/nova/network-service

« back to all changes in this revision

Viewing changes to nova/network/vlan/compute.py

  • Committer: Hisaharu Ishii
  • Date: 2011-03-30 10:33:12 UTC
  • Revision ID: ishii.hisaharu@lab.ntt.co.jp-20110330103312-h0j86holxv3zl8u0
Added zope interface for net agents

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
#    License for the specific language governing permissions and limitations
16
16
#    under the License.
17
17
import socket
 
18
from zope import interface
18
19
 
19
20
from nova import flags
20
21
from nova import rpc
21
22
from nova import utils
22
 
 
 
23
from nova.network import service
23
24
from nova.network.vlan import common
24
25
from nova.network.vlan.db import api as db_api
25
26
 
74
75
                         "args": {"network_id": network_ref['id']}})
75
76
    return host
76
77
 
77
 
def bind_vnics_to_ports(context, vnic_ids, is_vpn): 
78
 
    """Gets a fixed ips from the pool."""
79
 
    rpc.call(context,
80
 
             _get_network_topic(context),
81
 
             {"method": "allocate_fixed_ips",
82
 
              "args": {"vnic_ids": vnic_ids,
83
 
                       "vpn": is_vpn}})
84
 
 
85
 
def setup_compute_network(context, vnic_ids, is_vpn): 
86
 
    """Set up compute."""
87
 
    driver = utils.import_object(FLAGS.ns_vlan_network_agent_driver)
88
 
    for vnic_id in vnic_ids:
89
 
        network_ref = db_api.network_get_by_ethernet_card(context, vnic_id)
90
 
        driver.ensure_vlan_bridge(network_ref['vlan'], 
91
 
                                  network_ref['bridge'])
92
 
 
93
 
def teardown_compute_network(context, vnic_ids):
94
 
    """Clean up compute node."""
95
 
    for vnic_id in vnic_ids:
96
 
        fixed_ip = vnic_id.get('fixed_ip')
97
 
        if not FLAGS.stub_network and fixed_ip:
98
 
            floating_ips = fixed_ip.get('floating_ips') or []
99
 
 
100
 
            for floating_ip in floating_ips:
101
 
                address = floating_ip['address']
102
 
            
103
 
                network_topic = _queue_get_for(context,
104
 
                                     FLAGS.ns_vlan_network_topic,
105
 
                                     floating_ip['host'])
106
 
            
107
 
                rpc.cast(context,
108
 
                         network_topic,
109
 
                         {"method": "disassociate_floating_ip",
110
 
                          "args": {"floating_address": address}})
111
 
            
112
 
            address = fixed_ip['address']
113
 
            if address:
114
 
                # NOTE(vish): Currently, nothing needs to be done on the
115
 
                #             network node until release. If this changes,
116
 
            
117
 
                db_api.fixed_ip_update(context.elevated(), address,
118
 
                                       {'allocated':False}) 
119
 
 
120
 
def requires_file_injection():
121
 
    """This service does not requires file injection."""
122
 
    return False
123
 
 
124
 
def get_network_info(context, vnic_id):
125
 
    """Gets all the data related to IP network.
126
 
    The output is a dictionary containing the following keys:
127
 
        cidr, cidrv6, netmask, netmask_v6 gateway, gateway_v6, 
128
 
        dhcp_server, broadcast, dns, ra_server, mac_address,
129
 
        ip_address, bridge, and address_v6 
130
 
    """
131
 
    ethernet_card = db_api.ethernet_card_get(context, vnic_id)
132
 
    if not ethernet_card:
133
 
        raise ValueError("Invalid vnic ID!")
134
 
 
135
 
    # Get the IP address associated with this VNIC
136
 
    ip_address = db_api.ip_address_get_by_ethernet_card(context, 
137
 
                                                        ethernet_card.id)
138
 
    if not ip_address:
139
 
        raise ValueError("VNIC is not mapped to IP!")
140
 
 
141
 
    network = ip_address.network
142
 
    if network['cidr_v6']:
143
 
        ipv6_addr = utils.to_global_ipv6(network['cidr_v6'],
144
 
                                         ethernet_card['mac_address'])
145
 
    else:
146
 
        ipv6_addr = None
147
 
 
148
 
    net, mask = common.get_net_and_mask(network['cidr'])
149
 
    return dict(mac_address=ethernet_card['mac_address'],
150
 
                ip_address=ip_address['address'], bridge=network['bridge'],
151
 
                cidr=network['cidr'], netmask=network['netmask'],
152
 
                gateway=network['gateway'], broadcast=network['broadcast'],
153
 
                dns=network['dns'], dhcp_server=network['gateway'], 
154
 
                cidr_v6=network['cidr_v6'], address_v6=ipv6_addr, 
155
 
                ra_server=network['ra_server'], 
156
 
                netmask_v6=network['netmask_v6'], 
157
 
                gateway_v6=network['gateway_v6'],
158
 
                net=net, mask=mask) 
 
78
class NetworkAgentService(object):
 
79
    """An OpenStack network agent service object."""
 
80
 
 
81
    interface.implements(service.INetworkOpenStackApiService)
 
82
 
 
83
    def bind_vnics_to_ports(self, context, vnic_ids, is_vpn): 
 
84
        """Gets a fixed ips from the pool.
 
85
        
 
86
        Args:
 
87
           context: Nova context needed for DB access.
 
88
           vnic_ids: list of VNIC IDs
 
89
           is_vpn: Boolean to check if the call is for VPN.          
 
90
        """
 
91
        rpc.call(context,
 
92
                 _get_network_topic(context),
 
93
                 {"method": "allocate_fixed_ips",
 
94
                  "args": {"vnic_ids": vnic_ids,
 
95
                           "vpn": is_vpn}})
 
96
    
 
97
    def setup_compute_network(self, context, vnic_ids, is_vpn): 
 
98
        """Set up the compute node for networking.
 
99
        
 
100
        Args:
 
101
           context: Nova context needed for DB access.
 
102
           vnic_ids: list of VNIC IDs
 
103
           is_vpn: Boolean to check if the call is for VPN.
 
104
        """
 
105
        driver = utils.import_object(FLAGS.ns_vlan_network_agent_driver)
 
106
        for vnic_id in vnic_ids:
 
107
            network_ref = db_api.network_get_by_ethernet_card(context, vnic_id)
 
108
            driver.ensure_vlan_bridge(network_ref['vlan'], 
 
109
                                      network_ref['bridge'])
 
110
    
 
111
    def teardown_compute_network(self, context, vnic_ids):
 
112
        """Clean up the compute node.
 
113
        
 
114
        Args:
 
115
           context: Nova context needed for DB access.
 
116
           vnic_ids: list of VNIC IDs
 
117
        """
 
118
        for vnic_id in vnic_ids:
 
119
            fixed_ip = vnic_id.get('fixed_ip')
 
120
            if not FLAGS.stub_network and fixed_ip:
 
121
                floating_ips = fixed_ip.get('floating_ips') or []
 
122
    
 
123
                for floating_ip in floating_ips:
 
124
                    address = floating_ip['address']
 
125
                
 
126
                    network_topic = _queue_get_for(context,
 
127
                                         FLAGS.ns_vlan_network_topic,
 
128
                                         floating_ip['host'])
 
129
                
 
130
                    rpc.cast(context,
 
131
                             network_topic,
 
132
                             {"method": "disassociate_floating_ip",
 
133
                              "args": {"floating_address": address}})
 
134
                
 
135
                address = fixed_ip['address']
 
136
                if address:
 
137
                    # NOTE(vish): Currently, nothing needs to be done on the
 
138
                    #             network node until release. If this changes,
 
139
                
 
140
                    db_api.fixed_ip_update(context.elevated(), address,
 
141
                                           {'allocated':False}) 
 
142
    
 
143
    def requires_file_injection(self):
 
144
        """This service does not requires file injection.
 
145
        """
 
146
        return False
 
147
    
 
148
    def get_network_info(self, context, vnic_id):
 
149
        """Gets network data for a given VNIC.
 
150
        
 
151
        Args:
 
152
           context: Nova context needed for DB access.
 
153
           vnic_id: VNIC ID to get the network information for.
 
154
        
 
155
        Returns:
 
156
           A dictionary containing the following keys:
 
157
           cidr, cidrv6, netmask, netmask_v6 gateway, gateway_v6, 
 
158
           dhcp_server, broadcast, dns, ra_server, mac_address,
 
159
           ip_address, bridge, and address_v6
 
160
        """
 
161
        ethernet_card = db_api.ethernet_card_get(context, vnic_id)
 
162
        if not ethernet_card:
 
163
            raise ValueError("Invalid vnic ID!")
 
164
    
 
165
        # Get the IP address associated with this VNIC
 
166
        ip_address = db_api.ip_address_get_by_ethernet_card(context, 
 
167
                                                        ethernet_card.id)
 
168
        if not ip_address:
 
169
            raise ValueError("VNIC is not mapped to IP!")
 
170
    
 
171
        network = ip_address.network
 
172
        if network['cidr_v6']:
 
173
           ipv6_addr = utils.to_global_ipv6(network['cidr_v6'],
 
174
                                         ethernet_card['mac_address'])
 
175
        else:
 
176
           ipv6_addr = None
 
177
    
 
178
        net, mask = common.get_net_and_mask(network['cidr'])
 
179
        return dict(mac_address=ethernet_card['mac_address'],
 
180
                ip_address=ip_address['address'], bridge=network['bridge'],
 
181
                cidr=network['cidr'], netmask=network['netmask'],
 
182
                gateway=network['gateway'], broadcast=network['broadcast'],
 
183
                dns=network['dns'], dhcp_server=network['gateway'], 
 
184
                cidr_v6=network['cidr_v6'], address_v6=ipv6_addr, 
 
185
                ra_server=network['ra_server'], 
 
186
                    netmask_v6=network['netmask_v6'], 
 
187
                    gateway_v6=network['gateway_v6'],
 
188
                net=net, mask=mask)