summaryrefslogtreecommitdiffstats
path: root/vio/vio/pub/vim/vimapi/network/OperateSubnet.py
blob: b9b4c272a549578a23d0eb5c979a00593d7ece17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging

from vio.pub.msapi.extsys import get_vim_by_id
from vio.pub.vim.drivers.openstacksdk import neutron_v2_0
from vio.pub.vim.vimapi.network.OperateNetwork import BaseNet,translate


logger = logging.getLogger(__name__)


class OperateSubnet(BaseNet):
    keys_mapping = {"tenantId": "project_id",
                    "networkId": "network_id",
                    "ipVersion": "ip_version",
                    "gaetwayIp": "gateway_ip",
                    "dnsNameservers": "dns_nameservers",
                    "hostRoutes": "host_routes",
                    "allocationPools": "allocation_pools"
                    }

    def ___init__(self, params):
        super(OperateSubnet, self).__init__(params)

    def _convert(self, subnet):
        result = {}
        result['status'] = 'ok'
        result['id'] = subnet.id
        result['networkId'] = subnet.network_id
        result['name'] = subnet.name
        result['allocationPools'] = subnet.allocation_pools
        result['gatewayIp'] = subnet.gateway_ip
        result['dnsNameServers'] = subnet.dns_nameservers
        result['ipVersion'] = subnet.ip_version
        result['enableDhcp'] = subnet.is_dhcp_enabled
        result['hostRoutes'] = subnet.host_routes
        result['cidr'] = subnet.cidr
        return result

    def create_subnet(self, vimid, tenantid, body):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        body = translate(self.keys_mapping, body)
        subnet = network.subnet_create(**body)
        vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']}
        resp = self._convert(subnet)
        resp.update(vim_dict)
        return resp

    def list_subnet(self, vimid, tenantid, subnetid):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        subnet = network.subnet_get(subnetid)
        vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']}
        resp = self._convert(subnet)
        resp.update(vim_dict)
        return resp

    def delete_subnet(self, vimid, tenantid, subnetid):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        return network.subnet_delete(subnetid)

    def list_subnets(self, vimid, tenantid):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        tenant = {"project_id": tenantid}
        resp = network.subnets_get(**tenant)
        vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']}
        subnets = {'subnets': []}
        if resp:
            for subnet in resp:
                subnets['subnets'].append(self._convert(subnet))
        subnets.update(vim_dict)
        return subnets