aboutsummaryrefslogtreecommitdiffstats
path: root/ice_validator/preload_vnfapi/vnfapi_generator.py
blob: 517c789d90d48050592c5117c8ff0772164ac0be (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
# -*- coding: utf8 -*-
# ============LICENSE_START====================================================
# org.onap.vvp/validation-scripts
# ===================================================================
# Copyright © 2019 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the "License");
# you may not use this software 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.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the "License");
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# 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.
#
# ============LICENSE_END============================================
#
#

import json
import os

from preload.generator import (
    get_json_template,
    get_or_create_template,
    AbstractPreloadGenerator,
)

THIS_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(THIS_DIR, "vnfapi_data")


def get_or_create_network_template(network_role, vm_networks):
    """
    If the network role already exists in vm_networks, then
    return that otherwise create a blank template and return that
    """
    return get_or_create_template(
        DATA_DIR, "network-role", network_role, vm_networks, "vm-network"
    )


class VnfApiPreloadGenerator(AbstractPreloadGenerator):
    @classmethod
    def supports_output_passing(cls):
        return False

    @classmethod
    def format_name(cls):
        return "VNF-API"

    @classmethod
    def output_sub_dir(cls):
        return "vnfapi"

    def generate_module(self, vnf_module, output_dir):
        preload = get_json_template(DATA_DIR, "preload_template")
        self._populate(preload, vnf_module)
        outfile = "{}/{}.json".format(output_dir, vnf_module.vnf_name)
        with open(outfile, "w") as f:
            json.dump(preload, f, indent=4)

    def add_floating_ips(self, network_template, network):
        # only one floating IP is really supported, in the preload model
        # so for now we'll just use the last one.  We might revisit this
        # and if multiple floating params exist, then come up with an
        # approach to pick just one
        for ip in network.floating_ips:
            key = "floating-ip" if ip.ip_version == 4 else "floating-ip-v6"
            network_template[key] = self.replace(ip.param, single=True)

    def add_fixed_ips(self, network_template, port):
        for ip in port.fixed_ips:
            if ip.ip_version == 4:
                network_template["network-ips"].append(
                    {"ip-address": self.replace(ip.param, single=True)}
                )
                network_template["ip-count"] += 1
            else:
                network_template["network-ips-v6"].append(
                    {"ip-address": self.replace(ip.param, single=True)}
                )
                network_template["ip-count-ipv6"] += 1

    def _populate(self, preload, vnf_module):
        self._add_availability_zones(preload, vnf_module)
        self._add_vnf_networks(preload, vnf_module)
        self._add_vms(preload, vnf_module)
        self._add_parameters(preload, vnf_module)

    def _add_availability_zones(self, preload, vnf_module):
        zones = preload["input"]["vnf-topology-information"]["vnf-assignments"][
            "availability-zones"
        ]
        for zone in vnf_module.availability_zones:
            zones.append({"availability-zone": self.replace(zone, single=True)})

    def _add_vnf_networks(self, preload, vnf_module):
        networks = preload["input"]["vnf-topology-information"]["vnf-assignments"][
            "vnf-networks"
        ]
        for network in vnf_module.networks:
            network_data = {
                "network-role": network.network_role,
                "network-name": self.replace(
                    network.name_param,
                    "VALUE FOR: network name for {}".format(network.name_param),
                ),
            }
            for subnet in network.subnet_params:
                key = "ipv6-subnet-id" if "_v6_" in subnet else "subnet-id"
                network_data[key] = subnet
            networks.append(network_data)

    def _add_vms(self, preload, vnf_module):
        vm_list = preload["input"]["vnf-topology-information"]["vnf-assignments"][
            "vnf-vms"
        ]
        for vm in vnf_module.virtual_machine_types:
            vm_template = get_json_template(DATA_DIR, "vm")
            vm_template["vm-type"] = vm.vm_type
            vm_template["vm-count"] = vm.vm_count
            for name in vm.names:
                value = self.replace(name, single=True)
                vm_template["vm-names"]["vm-name"].append(value)
            vm_list.append(vm_template)
            vm_networks = vm_template["vm-networks"]
            for port in vm.ports:
                role = port.network.network_role
                network_template = get_or_create_network_template(role, vm_networks)
                network_template["network-role"] = role
                network_template["network-role-tag"] = role
                network_template["use-dhcp"] = "Y" if port.uses_dhcp else "N"
                self.add_fixed_ips(network_template, port)
                self.add_floating_ips(network_template, port)

    def _add_parameters(self, preload, vnf_module):
        params = preload["input"]["vnf-topology-information"]["vnf-parameters"]
        for key, value in vnf_module.preload_parameters.items():
            params.append(
                {
                    "vnf-parameter-name": key,
                    "vnf-parameter-value": self.replace(key, value),
                }
            )