1 from abc import ABC, abstractmethod
2 from pathlib import Path
3 from typing import Iterable, Any, Optional, Mapping
5 from preload.model import VnfModule
8 class AbstractPreloadInstance(ABC):
10 Represents the data source for a single instance of a preload for
11 any format. The implementation of AbstractPreloadGenerator will
12 call the methods of this class to retrieve the necessary data
13 to populate the preload. If a data element is not available,
14 then simply return ``None`` and a suitable placeholder will be
15 placed in the preload.
20 def output_dir(self) -> Path:
22 Base output directory where the preload will be generated. Please
23 note, that the generator may create nested directories under this
24 directory for the preload.
26 :return: Path to the desired output directory. This directory
27 and its parents will be created by the generator if
28 it is not already present.
30 raise NotImplementedError()
34 def module_label(self) -> str:
36 Identifier of the module. This must match the base name of the
37 heat module (ex: if the Heat file name is base.yaml, then the label
40 :return: string name of the module
42 raise NotImplementedError()
46 def vf_module_name(self) -> Optional[str]:
48 :return: module name to populate in the preload if available
50 raise NotImplementedError()
54 def flag_incompletes(self) -> bool:
56 If True, then the generator will modify the file name of any
57 generated preload to end with _incomplete.<ext> if any preload
58 value was not satisfied by the data source. If False, then
59 the file name will be the same regardless of the completeness
62 :return: True if file names should denote preload incompleteness
64 raise NotImplementedError()
68 def preload_basename(self) -> str:
70 Base name of the preload that will be used by the generator to create
73 raise NotImplementedError()
77 def vnf_name(self) -> Optional[str]:
79 :return: the VNF name to populate in the prelad if available
81 raise NotImplementedError()
85 def vnf_type(self) -> Optional[str]:
87 The VNF Type must be match the values in SDC. It is a concatenation
88 of <Service Instance Name>/<Resource Instance Name>.
90 :return: VNF Type to populate in the preload if available
92 raise NotImplementedError()
96 def vf_module_model_name(self) -> Optional[str]:
98 :return: Module model name if available
100 raise NotImplementedError()
103 def get_availability_zone(self, index: int, param_name: str) -> Optional[str]:
105 Retrieve the value for the availability zone at requested zero-based
106 index (i.e. 0, 1, 2, etc.)
108 :param index: index of availability zone (0, 1, etc.)
109 :param param_name: Name of the parameter from Heat
110 :return: value for the AZ if available
112 raise NotImplementedError()
115 def get_network_name(self, network_role: str, name_param: str) -> Optional[str]:
117 Retrieve the OpenStack name of the network for the given network role.
119 :param network_role: Network role from Heat template
120 :param name_param: Network name parameter from Heat
121 :return: Name of the network if available
123 raise NotImplementedError()
127 self, network_role: str, ip_version: int, param_name: str
130 Retrieve the subnet's UUID for the given network and IP version (4 or 6).
132 :param network_role: Network role from Heat template
133 :param ip_version: IP Version (4 or 6)
134 :param param_name: Parameter name from Heat
135 :return: UUID of the subnet if available
137 raise NotImplementedError()
141 self, network_role: str, ip_version: int, param_name: str
144 Retrieve the OpenStack Subnet name for the given network role and IP version
146 :param network_role: Network role from Heat template
147 :param ip_version: IP Version (4 or 6)
148 :param param_name: Parameter name from Heat
149 :return: Name of the subnet if available
151 raise NotImplementedError()
154 def get_vm_name(self, vm_type: str, index: int, param_name: str) -> Optional[str]:
156 Retrieve the vm name for the given VM type and index.
158 :param vm_type: VM Type from Heat template
159 :param index: Zero-based index of the VM for the vm-type
160 :param param_name: Parameter name from Heat
161 :return: VM Name if available
163 raise NotImplementedError()
167 self, vm_type: str, network_role: str, ip_version: int, param_name: str
170 Retreive the floating IP for the VM and Port identified by VM Type,
171 Network Role, and IP Version.
173 :param vm_type: VM Type from Heat template
174 :param network_role: Network Role from Heat template
175 :param ip_version: IP Version (4 or 6)
176 :param param_name: Parameter name from Heat
177 :return: floating IP address if available
179 raise NotImplementedError()
183 self, vm_type: str, network_role: str, ip_version: int, index: int, param: str
186 Retreive the fixed IP for the VM and Port identified by VM Type,
187 Network Role, IP Version, and index.
189 :param vm_type: VM Type from Heat template
190 :param network_role: Network Role from Heat template
191 :param ip_version: IP Version (4 or 6)
192 :param index: zero-based index for the IP for the given
193 VM Type, Network Role, IP Version combo
194 :param param_name: Parameter name from Heat
195 :return: floating IP address if available
197 raise NotImplementedError()
200 def get_vnf_parameter(self, key: str, value: Any) -> Optional[Any]:
202 Retrieve the value for the given key. These will be placed in the
203 tag-values/vnf parameters in the preload. If a value was specified in
204 the environment packaged in the Heat for for the VNF module, then
205 that value will be passed in ``value``. This class can return
206 the value or ``None`` if it does not have a value for the given key.
208 :param key: parameter name from Heat
209 :param value: Value from Heat env file if it was assigned there;
211 :return: Returns the value for the object. This should
212 be a str, dict, or list. The generator will
213 format it properly based on the selected output format
215 raise NotImplementedError()
218 def get_additional_parameters(self) -> Mapping[str, Any]:
220 Return any additional parameters that should be added to the VNF parameters.
222 This can be useful if you want to duplicate paramters in tag values that are
223 also in the other sections (ex: VM names).
225 :return: dict of str to object mappings that the generator must add to
226 the vnf_parameters/tag values
228 raise NotImplementedError()
231 class AbstractPreloadDataSource(ABC):
233 Represents a data source for a VNF preload data. Implementations of this
234 class can be dynamically discovered if they are in a preload plugin module.
235 A module is considered a preload plugin module if it starts with
236 prelaod_ and is available as a top level module on Python's sys.path.
238 The ``get_module_preloads`` will be invoked for each module in
239 the VNF. An instance of AbstractPreloadInstance must be returned for
240 each instance of the preload module that is to be created.
243 :param path: The path to the configuration source selected
244 in either the VVP GUI or command-line. This
245 may be a file or directory depending upon
246 the source_type defined by this data source
249 def __init__(self, path: Path):
254 def get_source_type(cls) -> str:
256 If 'FILE' returned, then the config source will be a specific
257 file; If 'DIR', then the config source will be a directory
260 raise NotImplementedError()
264 def get_identifier(cls) -> str:
266 Identifier for the given data source. This is the value that
267 can be passed via --preload-source-type.
269 :return: short identifier for this data source type
274 def get_name(self) -> str:
276 Human readable name to describe the preload data source. It is
277 recommended not to exceed 50 characters.
279 :return: human readable name of the preload data source (ex: Environment Files)
281 raise NotImplementedError()
284 def get_module_preloads(
285 self, module: VnfModule
286 ) -> Iterable[AbstractPreloadInstance]:
288 For the requested module, return an instance of AbstractPreloadInstance
289 for every preload module you wish to be created.
291 :param module: Module of the VNF
292 :return: iterable of preloads to create for the given module
294 raise NotImplementedError()
297 class BlankPreloadInstance(AbstractPreloadInstance):
299 Used to create blank preload templates. VVP will always create
300 a template of a preload in the requested format with no data provided.
303 def __init__(self, output_dir: Path, module_name: str):
304 self._output_dir = output_dir
305 self._module_name = module_name
308 def flag_incompletes(self) -> bool:
312 def preload_basename(self) -> str:
313 return self._module_name
316 def vf_module_name(self) -> Optional[str]:
319 def get_vm_name(self, vm_type: str, index: int, param_name: str) -> Optional[str]:
322 def get_availability_zone(self, index: int, param_name: str) -> Optional[str]:
326 def output_dir(self) -> Path:
327 return self._output_dir
330 def module_label(self) -> str:
331 return self._module_name
334 def vnf_name(self) -> Optional[str]:
338 def vnf_type(self) -> Optional[str]:
342 def vf_module_model_name(self) -> Optional[str]:
345 def get_network_name(self, network_role: str, name_param: str) -> Optional[str]:
349 self, network_role: str, ip_version: int, param_name: str
354 self, network_role: str, ip_version: int, param_name: str
359 self, vm_type: str, network_role: str, ip_version: int, param_name: str
364 self, vm_type: str, network_role: str, ip_version: int, index: int, param: str
368 def get_vnf_parameter(self, key: str, value: Any) -> Optional[Any]:
371 def get_additional_parameters(self) -> Mapping[str, Any]: