summaryrefslogtreecommitdiffstats
path: root/vio/vio/pub/vim/vimapi/network/OperatePort.py
blob: 4363f510e7816becab293161f1c4db2994c46fa0 (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
# Copyright (c) 2017 VMware, Inc.
#
# 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.

import logging

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


logger = logging.getLogger(__name__)


class OperatePort(BaseNet):
    keys_mapping = {"tenantId": "project_id",
                    "networkId": "network_id",
                    "vnicType": "binding:vnic_type",
                    "securityGroups": "security_groups",
                    "macAddress": "mac_address",
                    "subnetId": "subnet_id",
                    "ip": "ip_address"
                    }

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

    def _convert(self, port):
        result = {}
        result['status'] = 'ok'
        result['id'] = port.id
        result['networkId'] = port.network_id
        result['name'] = port.name
        result['vnicType'] = port.binding_vnic_type
        result['macAddress'] = port.mac_address
        result['subnetId'] = port.subnet_id or port.fixed_ips[0]['subnet_id']
        result['securityGroups'] = port.security_group_ids
        return result

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

    def list_port(self, vimid, tenantid, portid):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        port = network.port_find(portid)
        if port is None:
            return port
        vim_dict = {"vimName": vim_info['name'], "vimId": vim_info['vimId']}
        resp = self._convert(port)
        resp.update(vim_dict)
        return resp

    def delete_port(self, vimid, tenantid, portid):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info)
        return network.port_delete(portid)

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