summaryrefslogtreecommitdiffstats
path: root/newton/newton/registration/views/registration.py
blob: 9f28cdff903080dcc70bc1c91c28ea92c8537746 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# Copyright (c) 2017-2018 Wind River Systems, 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.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
import json
import uuid
import traceback

from django.conf import settings
from keystoneauth1.exceptions import HttpError

from common.exceptions import VimDriverNewtonException
from common.msapi import extsys
from newton_base.registration import registration as newton_registration
from newton_base.openoapi.flavor import Flavors

logger = logging.getLogger(__name__)

# DEBUG=True

class Registry(newton_registration.Registry):

    def __init__(self):
        self.proxy_prefix = settings.MULTICLOUD_PREFIX
        self.aai_base_url = settings.AAI_BASE_URL
        self._logger = logger

    def _discover_flavors(self, vimid="", session=None, viminfo=None):
        try:
            cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)
            cloud_extra_info_str = viminfo.get('cloud_extra_info')
            if cloud_extra_info_str:
                cloud_extra_info = json.loads(cloud_extra_info_str)
            else:
                cloud_extra_info = None
            for flavor in self._get_list_resources(
                    "/flavors/detail", "compute", session, viminfo, vimid,
                    "flavors"):
                flavor_info = {
                    'flavor-id': flavor['id'],
                    'flavor-name': flavor['name'],
                    'flavor-vcpus': flavor['vcpus'],
                    'flavor-ram': flavor['ram'],
                    'flavor-disk': flavor['disk'],
                    'flavor-ephemeral': flavor['OS-FLV-EXT-DATA:ephemeral'],
                    'flavor-swap': flavor['swap'],
                    'flavor-is-public': flavor['os-flavor-access:is_public'],
                    'flavor-disabled': flavor['OS-FLV-DISABLED:disabled'],
                }
                if flavor.get('link') and len(flavor['link']) > 0:
                    flavor_info['flavor-selflink'] = flavor['link'][0]['href'] or 'http://0.0.0.0',
                else:
                    flavor_info['flavor-selflink'] = 'http://0.0.0.0',

                if settings.AAI_SCHEMA_VERSION == "v13":
                    extraResp = Flavors._get_flavor_extra_specs(session, flavor['id'])
                    extraContent = extraResp.json()
                    hpa_capabilities = self._get_hpa_capabilities(vimid, flavor,
                                                                  extraContent["extra_specs"],
                                                                  cloud_extra_info)
                    flavor_info['hpa_capabilities'] = hpa_capabilities

                self._update_resoure(
                    cloud_owner, cloud_region_id, flavor['id'],
                    flavor_info, "flavor")

        except VimDriverNewtonException as e:
            self._logger.error("VimDriverNewtonException: status:%s, response:%s" % (e.http_status, e.content))
            return
        except HttpError as e:
            self._logger.error("HttpError: status:%s, response:%s" % (e.http_status, e.response.json()))
            return
        except Exception as e:
            self._logger.error(traceback.format_exc())
            return

    def _get_hpa_capabilities(self, vimid, flavor, extra_specs, cloud_extra_info):
        """Convert flavor information to HPA capabilities for AAI"""
        cloud_owner, cloud_region_id = extsys.decode_vim_id(vimid)

        json_data = open('hpa.json').read()
        hpa_dict = json.loads(json_data)

        capabilities = []

        # Basic Capabilities
        if set(hpa_dict['basicCapabilities']['hpa-attributes']).intersection(flavor):
            capability = hpa_dict['basicCapabilities']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['hpa-feature-attributes'] = self._get_capability_attributes(
                                                       flavor,
                                                       hpa_dict['basicCapabilities']['hpa-attributes'])
            capabilities.append(capability)

        # Local Storage
        if set(hpa_dict['localStorage']['hpa-attributes']).intersection(flavor):
            capability = hpa_dict['localStorage']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['hpa-feature-attributes'] = self._get_capability_attributes(
                                                       flavor,
                                                       hpa_dict['localStorage']['hpa-attributes'])
            capabilities.append(capability)

        # CPU Topology
        if set(hpa_dict['cpuTopology']['hpa-attributes']).intersection(extra_specs):
            capability = hpa_dict['cpuTopology']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['hpa-features-attributes'] = self._get_capability_attributes(
                                                        extra_specs,
                                                        hpa_dict['cpuTopology']['hpa-attributes'])
            capabilities.append(capability)

        # CPU Pinning
        if set(hpa_dict['cpuPinning']['hpa-attributes']).intersection(extra_specs):
            capability = hpa_dict['cpuPinning']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['hpa-features-attributes'] = self._get_capability_attributes(
                                                        extra_specs,
                                                        hpa_dict['cpuPinning']['hpa-attributes'])
            capabilities.append(capability)

        # Huge Pages
        if set(hpa_dict['hugePages']['hpa-attributes']).intersection(extra_specs):
            capability = hpa_dict['hugePages']['info']
            if extra_specs['hw:mem_page_size'] not in ['small', 'large', 'any']:
                unit = ''.join(i for i in extra_specs['hw:mem_page_size'] if not i.isdigit())
                if unit == '':
                    unit = 'KB'
                hpa_dict['hugePages']['hpa-attributes']['hw:mem_page_size']['unit'] = unit
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['hpa-features-attributes'] = self._get_capability_attributes(
                                                        extra_specs,
                                                        hpa_dict['hugePages']['hpa-attributes'])
            capabilities.append(capability)

        # NUMA
        if "hw:numa_nodes" in extra_specs:
            capability = hpa_dict['numa']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            # NUMA nodes are a special case and can't use the generic get attrs function
            attributes = []
            attributes.append({
                'hpa-attribute-key': hpa_dict['numa']['hpa-attributes']['hw:numa_nodes']['key'],
                'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(extra_specs['hw:numa_nodes'])
            })
            for spec in extra_specs:
                if spec.startswith('hw:numa_cpus'):
                    cpu_num = spec.split(":")[-1]
                    attributes.append({
                        'hpa-attribute-key': 'numaCpu-' + cpu_num,
                        'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(extra_specs[spec])
                    })
                elif spec.startswith('hw:numa_mem'):
                    mem_num = spec.split(":")[-1]
                    attributes.append({
                        'hpa-attribute-key': 'numaMem-' + mem_num,
                        'hpa-attribute-value': '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}'.format(extra_specs[spec],
                                                                                               "GB")
                    })
            capability['hpa-features-attributes'] = attributes
            capabilities.append(capability)

        # PCIe Passthrough
        pci_devices = [spec for spec in extra_specs if spec.startswith("pci_passthrough:alias")]
        for device in pci_devices:
            capability = hpa_dict['pciePassthrough']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            # device will be in the form pci_passthrough:alias=ALIAS:COUNT,
            # ALIAS is expected to be in the form <NAME>-<VENDOR_ID>-<DEVICE_ID>
            device_info = extra_specs[device].split(":")
            count = device_info[-1]
            vendor_id = device_info[0].split("-")[1]
            device_id = device_info[0].split("-")[2]

            attributes = [
                {
                    'hpa-attribute-key': 'pciCount',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(count)
                },
                {
                    'hpa-attribute-key': 'pciVendorId',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(vendor_id)
                },
                {
                    'hpa-attribute-key': 'pciDeviceId',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(device_id)
                },
                {
                    'hpa-attribute-key': 'functionType',
                    'hpa-attribute-value': 'PCI-PASSTHROUGH'
                }
            ]

            capability['hpa-features-attributes'] = attributes
            capabilities.append(capability)

        # SRIOV Devices
        sriov_devices = [spec for spec in extra_specs if spec.startswith("aggregate_instance_extra_spec:sriov")]
        for device in sriov_devices:
            capability = hpa_dict['pciePassthrough']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            # device will be in the form aggregate_instance_extra_specs:sriov-device-<NAME>=<true/false>,
            # NAME is expected to be in the form <NAME>-<VENDOR_ID>-<DEVICE_ID>
            enabled = extra_specs[device]
            count = 1 if enabled == "true" else 0
            vendor_id = device.split(":")[1].split("-")[3]
            device_id = device.split(":")[1].split("-")[4]

            attributes = [
                {
                    'hpa-attribute-key': 'pciCount',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(count)
                },
                {
                    'hpa-attribute-key': 'pciVendorId',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(vendor_id)
                },
                {
                    'hpa-attribute-key': 'pciDeviceId',
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(device_id)
                },
                {
                    'hpa-attribute-key': 'functionType',
                    'hpa-attribute-value': 'SRIOV'
                }
            ]

            capability['hpa-features-attributes'] = attributes
            capabilities.append(capability)


        # OVS DPDK
        if cloud_extra_info:
            cloud_dpdk_info = cloud_extra_info.get('ovsDpdk')
            capability = hpa_dict['ovsDpdk']['info']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            capability['architecture'] = cloud_dpdk_info.get('arch')
            attributes = [
                {
                    'hpa-attribute-key': cloud_dpdk_info.get('libname'),
                    'hpa-attribute-value': '{{\"value\":\"{0}\"}}'.format(cloud_dpdk_info.get('libvalue'))
                }
            ]
            capability['hpa-features-attributes'] = attributes
            capabilities.append(capability)

        # Instruction Set Extensions
        if "hw:capabilities:cpu_info:features" in extra_specs:
            capability = hpa_dict['instructionSetExtensions']
            capability['hpa-capability-id'] = str(uuid.uuid4())
            attributes = [
                {
                    'hpa-attribute-key': hpa_dict['instructionSetExtensions']['hpa-attributes']['hw:capabilities:cpu_info:features'],
                    'hpa-attribute-value': '{{\"value\":[\"{0}\"]}}'.format(extra_specs['hw:capabilities:cpu_info:features'])
                }
            ]
            capability['hpa-features-attributes'] = attributes
            capabilities.append(capability)

        return capabilities

    def _get_capability_attributes(self, cloud_info, attributes):
        result = []
        for attr in attributes:
            if attr in cloud_info:
                attribute = {'hpa-attribute-key': attributes[attr]['key']}
                if attributes[attr]['unit']:
                    attribute['hpa-attribute-value'] = (
                        '{{\"value\":\"{0}\",\"unit\":\"{1}\"}}').format(cloud_info[attr],
                                                                         attributes[attr]['unit'])
                else:
                    attribute['hpa-attribute-value'] = '{{\"value\":\"{0}\"}}'.format(cloud_info[attr])

                result.append(attribute)
        return result