Add network 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
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
30 def create_vim_res(data, do_notify, do_rollback):
31     try:
32         for vol in ignore_case_get(data, "volume_storages"):
33             create_volume(vol, do_notify, 10)
34         for network in ignore_case_get(data, "vls"):
35             create_network(network, do_notify, 20)
36             
37     except VimException as e:
38         logger.error(e.message)
39         do_rollback(e.message)
40     except:
41         logger.error(traceback.format_exc())
42         do_rollback(str(sys.exc_info()))
43     
44 def create_volume(vol, do_notify, progress)
45     param = {
46         "tenant": vol["properties"]["location_info"]["tenant"], 
47         "volumeName": vol["properties"]["volume_name"], 
48         "volumeSize": int(ignore_case_get(vol["properties"], "size", "0")),
49         "imageName": ignore_case_get(vol, "image_file"),
50         "volumeType": ignore_case_get(vol["properties"], "custom_volume_type")
51     }
52     vim_id = vol["properties"]["location_info"]["vimid"],       
53     ret = api.create_volume(vim_id, param)
54     do_notify(progress, ret)
55     
56 def create_network(network, do_notify, progress):
57     param = {
58         "tenant": network["properties"]["location_info"]["tenant"],     
59         "networkName": network["properties"]["network_name"],
60         "shared": NET_PRIVATE,
61         "networkType": network["properties"]["network_type"],
62         "physicalNetwork": ignore_case_get(network["properties"], "physical_network")
63     }
64     vlan_transparent = ignore_case_get(network["properties"], "vlan_transparent")
65     if vlan_transparent:
66         param["vlanTransparent"] = VLAN_TRANSPARENT_YES
67     segmentation_id = ignore_case_get(network["properties"], "segmentation_id")
68     if segmentation_id:
69         param["segmentationId"] = int(segmentation_id)
70     vim_id = network["properties"]["location_info"]["vimid"],
71     ret = api.create_network(vim_id, param)
72     do_notify(progress, ret)
73     
74