feature: sol004 and sol007
[osm/osmclient.git] / osmclient / common / sol007_package.py
1 # Copyright ETSI Contributors and Others.
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """Python module for interacting with ETSI GS NFV-SOL007 compliant packages.
17
18 This module provides a SOL007Package class for validating and interacting with
19 ETSI SOL007 packages. A valid SOL007 package may have its files arranged according
20 to one of the following two structures:
21
22 SOL007 with metadata directory SOL007 without metadata directory
23
24 native_charm_vnf/ native_charm_vnf/
25 ├── TOSCA-Metadata ├── native_charm_nsd.mf
26 │ └── TOSCA.meta ├── native_charm_nsd.yaml
27 ├── manifest.mf ├── ChangeLog.txt
28 ├── Definitions ├── Licenses
29 │ └── native_charm_nsd.yaml │ └── license.lic
30 ├── Files ├── Files
31 │ ├── icons │ └── icons
32 │ │ └── osm.png │ └── osm.png
33 │ ├── Licenses └── Scripts
34 │ │ └── license.lic ├── cloud_init
35 │ └── changelog.txt │ └── cloud-config.txt
36 └── Scripts └── charms
37 ├── cloud_init └── simple
38 │ └── cloud-config.txt ├── config.yaml
39 └── charms ├── hooks
40 └── simple │ ├── install
41 ├── config.yaml ...
42 ├── hooks │
43 │ ├── install └── src
44 ... └── charm.py
45 └── src
46 └── charm.py
47 """
48
49 import yaml
50 import datetime
51 import os
52 from .sol_package import SOLPackage
53
54
55 class SOL007PackageException(Exception):
56 pass
57
58
59 class SOL007Package(SOLPackage):
60 _MANIFEST_NSD_INVARIANT_ID = "nsd_invariant_id"
61 _MANIFEST_NSD_NAME = "nsd_name"
62 _MANIFEST_NSD_DESIGNER = "nsd_designer"
63 _MANIFEST_NSD_FILE_STRUCTURE_VERSION = "nsd_file_structure_version"
64 _MANIFEST_NSD_RELEASE_DATE_TIME = "nsd_release_date_time"
65 _MANIFEST_NSD_COMPATIBLE_SPECIFICATION_VERSIONS = (
66 "compatible_specification_versions"
67 )
68
69 _MANIFEST_ALL_FIELDS = [
70 _MANIFEST_NSD_INVARIANT_ID,
71 _MANIFEST_NSD_NAME,
72 _MANIFEST_NSD_DESIGNER,
73 _MANIFEST_NSD_FILE_STRUCTURE_VERSION,
74 _MANIFEST_NSD_RELEASE_DATE_TIME,
75 _MANIFEST_NSD_COMPATIBLE_SPECIFICATION_VERSIONS,
76 ]
77
78 def __init__(self, package_path=""):
79 super().__init__(package_path)
80
81 def generate_manifest_data_from_descriptor(self):
82 descriptor_path = os.path.join(
83 self._package_path, self.get_descriptor_location()
84 )
85 with open(descriptor_path, "r") as descriptor:
86 try:
87 nsd_data = yaml.safe_load(descriptor)["nsd"]
88 except yaml.YAMLError as e:
89 print("Error reading descriptor {}: {}".format(descriptor_path, e))
90 return
91
92 self._manifest_metadata = {}
93 self._manifest_metadata[self._MANIFEST_NSD_INVARIANT_ID] = nsd_data.get(
94 "id", "default-id"
95 )
96 self._manifest_metadata[self._MANIFEST_NSD_NAME] = nsd_data.get(
97 "name", "default-name"
98 )
99 self._manifest_metadata[self._MANIFEST_NSD_DESIGNER] = nsd_data.get(
100 "designer", "OSM"
101 )
102 self._manifest_metadata[
103 self._MANIFEST_NSD_FILE_STRUCTURE_VERSION
104 ] = nsd_data.get("version", "1.0")
105 self._manifest_metadata[self._MANIFEST_NSD_RELEASE_DATE_TIME] = (
106 datetime.datetime.now().astimezone().isoformat()
107 )
108 self._manifest_metadata[
109 self._MANIFEST_NSD_COMPATIBLE_SPECIFICATION_VERSIONS
110 ] = "2.7.1"