Feature 10954 to automatically select WIMs for inter-datacenter networks
[osm/LCM.git] / osm_lcm / data_utils / wim.py
1 # -*- coding: utf-8 -*-
2
3 # This file is part of OSM Life-Cycle Management module
4 #
5 # Copyright 2022 ETSI
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License"); you may
8 # not use this file except in compliance with the License. You may obtain
9 # a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 # License for the specific language governing permissions and limitations
17 # under the License.
18 ##
19
20
21 from osm_lcm.data_utils.database.vim_account import VimAccountDB
22 from osm_lcm.data_utils.database.wim_account import WimAccountDB
23 from osm_lcm.data_utils.vim import get_vims_to_connect
24 from osm_lcm.lcm_utils import LcmException
25
26 __author__ = (
27 "Lluis Gifre <lluis.gifre@cttc.es>, Ricard Vilalta <ricard.vilalta@cttc.es>"
28 )
29
30
31 def get_candidate_wims(vims_to_connect):
32 all_wim_accounts = WimAccountDB.get_all_wim_accounts()
33 candidate_wims = {}
34 for wim_id, db_wim in all_wim_accounts.items():
35 wim_port_mapping = db_wim.get("config", {}).get("wim_port_mapping", [])
36 wim_dc_ids = {
37 m.get("datacenter_id") for m in wim_port_mapping if m.get("datacenter_id")
38 }
39 not_reachable_vims = vims_to_connect.difference(wim_dc_ids)
40 if len(not_reachable_vims) > 0:
41 continue
42 # TODO: consider adding other filtering fields such as supported layer(s) [L2, L3, ...]
43 candidate_wims[wim_id] = db_wim
44 return candidate_wims
45
46
47 def select_feasible_wim_account(db_nsr, db_vnfrs, target_vld, vld_params, logger):
48 logger.info("Checking if WIM is needed for VLD({:s})...".format(str(target_vld)))
49 if target_vld.get("mgmt-network", False):
50 logger.info(
51 "WIM not needed, VLD({:s}) is a management network".format(str(target_vld))
52 )
53 return None, None # assume mgmt networks do not use a WIM
54
55 # check if WIM account is explicitly False
56 wim_account_id = vld_params.get("wimAccountId")
57 if wim_account_id is not None and not wim_account_id:
58 logger.info(
59 "VLD({:s}) explicitly specifies not to use a WIM".format(str(target_vld))
60 )
61 return None, None # WIM account explicitly set to False, do not use a WIM
62
63 # find VIMs to be connected by VLD
64 vims_to_connect = get_vims_to_connect(db_nsr, db_vnfrs, target_vld, logger)
65 # check if we need a WIM to interconnect the VNFs in different VIMs
66 if len(vims_to_connect) < 2:
67 logger.info(
68 "WIM not needed, VLD({:s}) does not involve multiple VIMs".format(
69 str(target_vld)
70 )
71 )
72 return None, None
73 # if more than one VIM needs to be connected...
74 logger.info(
75 "WIM is needed, multiple VIMs to interconnect: {:s}".format(
76 str(vims_to_connect)
77 )
78 )
79 # find a WIM having these VIMs on its wim_port_mapping setting
80 candidate_wims = get_candidate_wims(vims_to_connect)
81 logger.info("Candidate WIMs: {:s}".format(str(candidate_wims)))
82
83 # check if a desired wim_account_id is specified in vld_params
84 wim_account_id = vld_params.get("wimAccountId")
85 if wim_account_id:
86 # check if the desired WIM account is feasible
87 # implicitly checks if it exists in the DB
88 db_wim = candidate_wims.get(wim_account_id)
89 if db_wim:
90 return wim_account_id, db_wim
91 msg = (
92 "WimAccountId specified in VldParams({:s}) cannot be used "
93 "to connect the required VIMs({:s}). Candidate WIMs are: {:s}"
94 )
95 raise LcmException(
96 msg.format(str(vld_params), str(vims_to_connect), str(candidate_wims))
97 )
98
99 # if multiple candidate WIMs: report error message
100 if len(candidate_wims) > 1:
101 msg = (
102 "Multiple candidate WIMs found ({:s}) and wim_account not specified. "
103 "Please, specify the WIM account to be used."
104 )
105 raise LcmException(msg.format(str(candidate_wims.keys())))
106
107 # a single candidate WIM has been found, retrieve it
108 return candidate_wims.popitem() # returns tuple (wim_account_id, db_wim)
109
110
111 def get_target_wim_attrs(nsr_id, target_vld, vld_params):
112 target_vims = [
113 "vim:{:s}".format(vim_id) for vim_id in vld_params["vim-network-name"]
114 ]
115 wim_vld = "nsrs:{}:vld.{}".format(nsr_id, target_vld["id"])
116 vld_type = target_vld.get("type")
117 if vld_type is None:
118 vld_type = "ELAN" if len(target_vims) > 2 else "ELINE"
119 target_wim_attrs = {
120 "sdn": True,
121 "target_vims": target_vims,
122 "vlds": [wim_vld],
123 "type": vld_type,
124 }
125 return target_wim_attrs
126
127
128 def get_sdn_ports(vld_params, db_wim):
129 if vld_params.get("provider-network"):
130 # if SDN ports are specified in VLD params, use them
131 return vld_params["provider-network"].get("sdn-ports")
132
133 # otherwise, compose SDN ports required
134 wim_port_mapping = db_wim.get("config", {}).get("wim_port_mapping", [])
135 sdn_ports = []
136 for vim_id in vld_params["vim-network-name"]:
137 db_vim = VimAccountDB.get_vim_account_with_id(vim_id)
138 vim_name = db_vim["name"]
139 mapping = next(
140 (m for m in wim_port_mapping if m["datacenter_id"] == vim_name),
141 None,
142 )
143 if mapping is None:
144 msg = "WIM({:s},{:s}) does not specify a mapping for VIM({:s},{:s})"
145 raise LcmException(
146 msg.format(
147 db_wim["name"],
148 db_wim["_id"],
149 db_vim["name"],
150 db_vim["_id"],
151 )
152 )
153 sdn_port = {
154 "device_id": vim_name,
155 "switch_id": mapping.get("device_id"),
156 "switch_port": mapping.get("device_interface_id"),
157 "service_endpoint_id": mapping.get("service_endpoint_id"),
158 }
159 service_mapping_info = mapping.get("service_mapping_info", {})
160 encapsulation = service_mapping_info.get("encapsulation", {})
161 if encapsulation.get("type"):
162 sdn_port["service_endpoint_encapsulation_type"] = encapsulation["type"]
163 if encapsulation.get("vlan"):
164 sdn_port["vlan"] = encapsulation["vlan"]
165 sdn_ports.append(sdn_port)
166 return sdn_ports