blob: fc51f3caacf54e6b5753889effa18853f6b719a3 [file] [log] [blame]
Lianhao Lufdb7c572019-11-28 15:12:22 +08001# Copyright (c) 2019 Intel Corp. All rights reserved.
2#
3# Licensed to the Apache Software Foundation (ASF) under one or more
4# contributor license agreements. See the NOTICE file distributed with
5# this work for additional information regarding copyright ownership.
6# The ASF licenses this file to You under the Apache License, Version 2.0
7# (the "License"); you may not use this file except in compliance with
8# the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18import logging
19import os
20import pprint
21
22from ruamel import yaml # @UnresolvedImport
23import six
24
25from vnfsdk_pkgtools.packager import utils
26
27LOG = logging.getLogger(__name__)
28
29META_FILE = 'TOSCA-Metadata/TOSCA.meta'
30
31META_FILE_VERSION_KEY = 'TOSCA-Meta-File-Version'
32META_FILE_VERSION_VALUE = '1.0'
33META_CSAR_VERSION_KEY = 'CSAR-Version'
34META_CSAR_VERSION_VALUE = '1.1'
35META_CREATED_BY_KEY = 'Created-By'
36META_CREATED_BY_VALUE = 'ONAP VNFSDK pkgtools'
37
38META_ENTRY_DEFINITIONS_KEY = 'Entry-Definitions'
39
40BASE_META = {
41 META_FILE_VERSION_KEY: META_FILE_VERSION_VALUE,
42 META_CSAR_VERSION_KEY: META_CSAR_VERSION_VALUE,
43}
44
45
46class ToscaMeta(object):
47 META_ENTRY_MANIFEST_FILE_KEY = 'ETSI-Entry-Manifest'
48 META_ENTRY_HISTORY_FILE_KEY = 'ETSI-Entry-Change-Log'
49 META_ENTRY_TESTS_DIR_KEY = 'ETSI-Entry-Tests'
50 META_ENTRY_LICENSES_DIR_KEY = 'ETSI-Entry-Licenses'
51 META_ENTRY_CERT_FILE_KEY = 'ETSI-Entry-Certificate'
52 REQUIRED_KEYS = [ META_FILE_VERSION_KEY, META_CSAR_VERSION_KEY,
53 META_CREATED_BY_KEY, META_ENTRY_DEFINITIONS_KEY,
54 META_ENTRY_MANIFEST_FILE_KEY, META_ENTRY_HISTORY_FILE_KEY,
55 META_ENTRY_LICENSES_DIR_KEY,
56 ]
57 OPTIONAL_KEYS = [META_ENTRY_TESTS_DIR_KEY, META_ENTRY_CERT_FILE_KEY]
58
59 def __init__(self, base_dir, entry, manifest=None, changelog=None,
60 licenses=None, tests=None, certificate=None,
61 meta_file_version=META_FILE_VERSION_VALUE,
62 meta_csar_version=META_CSAR_VERSION_VALUE,
63 meta_created_by=META_CREATED_BY_VALUE):
64
65 self.base_dir = base_dir
66
67 metadata = {}
68 metadata[META_FILE_VERSION_KEY] = str(meta_file_version)
69 metadata[META_CSAR_VERSION_KEY] = str(meta_csar_version)
70 metadata[META_CREATED_BY_KEY] = meta_created_by
71 metadata[META_ENTRY_DEFINITIONS_KEY] = entry
72 if manifest:
73 metadata[self.META_ENTRY_MANIFEST_FILE_KEY] = manifest
74 if changelog:
75 metadata[self.META_ENTRY_HISTORY_FILE_KEY] = changelog
76 if licenses:
77 metadata[self.META_ENTRY_LICENSES_DIR_KEY] = licenses
78 if tests:
79 metadata[self.META_ENTRY_TESTS_DIR_KEY] = tests
80 if certificate:
81 metadata[self.META_ENTRY_CERT_FILE_KEY] = certificate
82
83 self.metadata = self._validate(metadata)
84
85 def _validate(self, metadata):
86 for (key, value) in six.iteritems(BASE_META):
87 if metadata.get(key) != value:
88 raise ValueError('TOSCA.meta: {} must be {}'.format(key, value))
89
90 utils.check_file_dir(root=self.base_dir,
91 entry=metadata.get(META_ENTRY_DEFINITIONS_KEY),
92 msg='Please specify a valid entry point.',
93 check_dir=False)
94 entry_file = os.path.join(self.base_dir,
95 metadata.get(META_ENTRY_DEFINITIONS_KEY))
96 try:
97 with open(entry_file) as f:
98 v = yaml.safe_load(f)['tosca_definitions_version']
99 except:
100 raise ValueError('Entry file {} is not a valid tosca simple yaml file'.format(entry_file))
101
102 if metadata.get(self.META_ENTRY_MANIFEST_FILE_KEY):
103 utils.check_file_dir(root=self.base_dir,
104 entry=metadata[self.META_ENTRY_MANIFEST_FILE_KEY],
105 msg='Please specify a valid manifest file.',
106 check_dir=False)
107 if metadata.get(self.META_ENTRY_HISTORY_FILE_KEY):
108 utils.check_file_dir(root=self.base_dir,
109 entry=metadata[self.META_ENTRY_HISTORY_FILE_KEY],
110 msg='Please specify a valid change history file.',
111 check_dir=False)
112 if metadata.get(self.META_ENTRY_LICENSES_DIR_KEY):
113 utils.check_file_dir(root=self.base_dir,
114 entry=metadata[self.META_ENTRY_LICENSES_DIR_KEY],
115 msg='Please specify a valid license directory.',
116 check_dir=True)
117 if metadata.get(self.META_ENTRY_TESTS_DIR_KEY):
118 utils.check_file_dir(root=self.base_dir,
119 entry=metadata[self.META_ENTRY_TESTS_DIR_KEY],
120 msg='Please specify a valid test directory.',
121 check_dir=True)
122 if metadata.get(self.META_ENTRY_CERT_FILE_KEY):
123 utils.check_file_dir(root=self.base_dir,
124 entry=metadata[self.META_ENTRY_CERT_FILE_KEY],
125 msg='Please specify a valid certificate file.',
126 check_dir=False)
127 missing_keys = [key for key in self.REQUIRED_KEYS if key not in metadata]
128 if missing_keys:
129 raise ValueError('TOSCA.meta: missing keys: {}'.format(','.join(missing_keys)))
130 return metadata
131
132 def dump_as_string(self):
133 s = ""
134 for key in self.REQUIRED_KEYS + self.OPTIONAL_KEYS:
135 if self.metadata.get(key):
136 s += "{}: {}\n".format(key, self.metadata.get(key))
137 return s
138
139 @property
140 def created_by(self):
141 return self.metadata.get(META_CREATED_BY_KEY)
142
143 @property
144 def csar_version(self):
145 return self.metadata.get(META_CSAR_VERSION_KEY)
146
147 @property
148 def meta_file_version(self):
149 return self.metadata.get(META_FILE_VERSION_KEY)
150
151 @property
152 def entry_definitions(self):
153 return self.metadata.get(META_ENTRY_DEFINITIONS_KEY)
154
155 @property
156 def entry_manifest_file(self):
157 return self.metadata.get(self.META_ENTRY_MANIFEST_FILE_KEY)
158
159 @property
160 def entry_history_file(self):
161 return self.metadata.get(self.META_ENTRY_HISTORY_FILE_KEY)
162
163 @property
164 def entry_tests_dir(self):
165 return self.metadata.get(self.META_ENTRY_TESTS_DIR_KEY)
166
167 @property
168 def entry_licenses_dir(self):
169 return self.metadata.get(self.META_ENTRY_LICENSES_DIR_KEY)
170
171 @property
172 def entry_certificate_file(self):
173 return self.metadata.get(self.META_ENTRY_CERT_FILE_KEY)
174
175
176class ToscaMeta241(ToscaMeta):
177 # SOL004 v2.4.1
178 META_ENTRY_MANIFEST_FILE_KEY = 'Entry-Manifest'
179 META_ENTRY_HISTORY_FILE_KEY = 'Entry-Change-Log'
180 META_ENTRY_TESTS_DIR_KEY = 'Entry-Tests'
181 META_ENTRY_LICENSES_DIR_KEY = 'Entry-Licenses'
182 META_ENTRY_CERT_FILE_KEY = 'Entry-Certificate'
183 REQUIRED_KEYS = [ META_FILE_VERSION_KEY, META_CSAR_VERSION_KEY,
184 META_CREATED_BY_KEY, META_ENTRY_DEFINITIONS_KEY,
185 ]
186 OPTIONAL_KEYS = [ META_ENTRY_MANIFEST_FILE_KEY, META_ENTRY_HISTORY_FILE_KEY,
187 META_ENTRY_LICENSES_DIR_KEY, META_ENTRY_TESTS_DIR_KEY,
188 META_ENTRY_CERT_FILE_KEY,
189 ]
190
191
192class ToscaMeta261(ToscaMeta):
193 # SOL004 v2.6.1
194 pass
195
196
197def create_from_file(base_dir):
198 csar_metafile = os.path.join(base_dir, META_FILE)
199 if not os.path.exists(csar_metafile):
200 raise ValueError('Metadata file {0} is missing from the CSAR'.format(csar_metafile))
201 LOG.debug('CSAR metadata file: {0}'.format(csar_metafile))
202 LOG.debug('Attempting to parse CSAR metadata YAML')
203 with open(csar_metafile) as f:
204 metadata = yaml.safe_load(f)
205 LOG.debug('CSAR metadata:\n{0}'.format(pprint.pformat(metadata)))
206 # By default we assume it's SOL004 2.4.1
207 cls = ToscaMeta241
208 for key in metadata.keys():
209 if key.startswith('ETSI-'):
210 cls = ToscaMeta261
211 break
212 return cls(base_dir,
213 entry=metadata.get(META_ENTRY_DEFINITIONS_KEY),
214 manifest=metadata.get(cls.META_ENTRY_MANIFEST_FILE_KEY),
215 changelog=metadata.get(cls.META_ENTRY_HISTORY_FILE_KEY),
216 licenses=metadata.get(cls.META_ENTRY_LICENSES_DIR_KEY),
217 tests=metadata.get(cls.META_ENTRY_TESTS_DIR_KEY),
218 certificate=metadata.get(cls.META_ENTRY_CERT_FILE_KEY),
219 meta_file_version=metadata.get(META_FILE_VERSION_KEY),
220 meta_csar_version=metadata.get(META_CSAR_VERSION_KEY),
221 meta_created_by=metadata.get(META_CREATED_BY_KEY))
222