summaryrefslogtreecommitdiffstats
path: root/vio/vio/pub/vim/vimapi/network/OperatePort.py
blob: 4a059f1bb31ed2568cb2ae61eafb30ed8eb13780 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
# Copyright (c) 2017-2018 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"
                    }

    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
        if port.fixed_ips:
            subnet_id = port.fixed_ips[0]['subnet_id']
        else:
            subnet_id = port.subnet_id
        result['subnetId'] = 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, tenantid)
        body = translate(self.keys_mapping, body)
        if 'network_id' in body:
            net = network.network_get(body['network_id'])
            body['network_id'] = net.id
        if 'subnetId' in body:
            subnet = network.subnet_get(body['subnetId'])
            body['subnetId'] = subnet.id
        if "ip" in body:
            body['fixed_ips'] = [{'subnet_id': body.pop('subnetId'),
                                  "ip_address": body.pop('ip')}]
        else:
            body['fixed_ips'] = [{'subnet_id': body.pop('subnetId')}]
        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, ignore_missing=False):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info, tenantid)
        port = network.port_find(portid, ignore_missing=ignore_missing)
        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, tenantid)
        return network.port_delete(portid)

    def list_ports(self, vimid, tenantid, **query):
        vim_info = self.get_vim_info(vimid)
        network = self.auth(vim_info, tenantid)
        query.update({"project_id": tenantid})
        resp = network.ports_get(**query)
        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