Add subnet adaptor of vimdriver
[vfc/gvnfm/vnflcm.git] / lcm / lcm / pub / vimapi / adaptor.py
1 # Copyright 2017 ZTE Corporation.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #         http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import logging
16 import json
17 import traceback
18 import sys
19
20 from lcm.pub.utils.values import ignore_case_get, set_opt_val
21 from . import api
22 from .exceptions import VimException
23
24 logger = logging.getLogger(__name__)
25
26 RES_EXIST, RES_NEW = 0, 1
27 NET_PRIVATE, NET_SHSRED = 0, 1
28 VLAN_TRANSPARENT_NO, VLAN_TRANSPARENT_YES = 0, 1
29 IP_V4, IP_V6 = 4, 6
30 DHCP_DISABLED, DHCP_ENABLED = 0, 1
31 RES_VOLUME, RES_NETWORK, RES_SUBNET, RES_PORT, RES_FLAVOR, RES_VM = range(6)
32
33 def create_vim_res(data, do_notify, do_rollback):
34     try:
35         for vol in ignore_case_get(data, "volume_storages"):
36             create_volume(vol, do_notify, 10)
37         for network in ignore_case_get(data, "vls"):
38             create_network(network, do_notify, 20)
39         for subnet in ignore_case_get(data, "vls"):
40             create_subnet(subnet, do_notify, 30)
41             
42             
43     except VimException as e:
44         logger.error(e.message)
45         do_rollback(e.message)
46     except:
47         logger.error(traceback.format_exc())
48         do_rollback(str(sys.exc_info()))
49     
50 def create_volume(vol, do_notify, progress):
51     param = {
52         "tenant": vol["properties"]["location_info"]["tenant"], 
53         "volumeName": vol["properties"]["volume_name"], 
54         "volumeSize": int(ignore_case_get(vol["properties"], "size", "0"))
55     }
56     set_opt_val(param, "imageName", ignore_case_get(vol, "image_file"))
57     set_opt_val(param, "volumeType", ignore_case_get(vol["properties"], "custom_volume_type"))
58     vim_id = vol["properties"]["location_info"]["vimid"],
59     ret = api.create_volume(vim_id, param)
60     do_notify(RES_VOLUME, progress, ret)
61     
62 def create_network(network, do_notify, progress):
63     param = {
64         "tenant": network["properties"]["location_info"]["tenant"],     
65         "networkName": network["properties"]["network_name"],
66         "shared": NET_PRIVATE,
67         "networkType": network["properties"]["network_type"],
68         "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
69     }
70     set_opt_val(param, "vlanTransparent", 
71         ignore_case_get(network["properties"], "vlan_transparent"), VLAN_TRANSPARENT_YES)
72     set_opt_val(param, "segmentationId", ignore_case_get(network["properties"], "segmentation_id"))
73     vim_id = network["properties"]["location_info"]["vimid"],
74     ret = api.create_network(vim_id, param)
75     do_notify(RES_NETWORK, progress, ret)
76     
77 def create_subnet(subnet, do_notify, progress):
78     param = {
79         "tenant": subnet["properties"]["location_info"]["tenant"],      
80         "networkName": subnet["properties"]["network_name"],
81         "subnetName": subnet["properties"]["name"],
82         "cidr": ignore_case_get(subnet["properties"], "cidr"),
83         "ipVersion": ignore_case_get(subnet["properties"], "ip_version", IP_V4)
84     }
85     set_opt_val(param, "enableDhcp", 
86         ignore_case_get(subnet["properties"], "dhcp_enabled"), DHCP_ENABLED)
87     set_opt_val(param, "gatewayIp", ignore_case_get(subnet["properties"], "gateway_ip"))
88     set_opt_val(param, "dnsNameservers", ignore_case_get(subnet["properties"], "dns_nameservers"))
89     allocation_pool = {}
90     set_opt_val(allocation_pool, "start", ignore_case_get(subnet["properties"], "start_ip"))
91     set_opt_val(allocation_pool, "end", ignore_case_get(subnet["properties"], "end_ip"))
92     if allocation_pool:
93         param["allocationPools"] = [allocation_pool]
94     set_opt_val(param, "hostRoutes", ignore_case_get(subnet["properties"], "host_routes"))
95     vim_id = network["properties"]["location_info"]["vimid"],
96     ret = api.create_subnet(vim_id, param)
97     do_notify(RES_SUBNET, progress, ret)
98