839f05c37b5660281adffffea7125bd7808d0591
[modeling/etsicatalog.git] / catalog / packages / biz / vnf_pkg_artifacts.py
1 # Copyright (C) 2019 Verizon. All Rights Reserved
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
17 from catalog.pub.database.models import VnfPackageModel
18 from catalog.pub.exceptions import ResourceNotFoundException, ArtifactNotFoundException
19 from catalog.pub.utils import fileutil
20
21 logger = logging.getLogger(__name__)
22
23
24 class FetchVnfPkgArtifact(object):
25     def fetch(self, vnfPkgId, artifactPath):
26         logger.debug("FetchVnfPkgArtifact--get--single--artifact--biz::>"
27                      "ID: %s path: %s" % (vnfPkgId, artifactPath))
28         vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
29         if not vnf_pkg.exists():
30             err_msg = "NF Package (%s) doesn't exists." % vnfPkgId
31             raise ResourceNotFoundException(err_msg)
32         vnf_pkg = vnf_pkg.get()
33         local_path = vnf_pkg.localFilePath
34         if local_path.endswith(".csar") or local_path.endswith(".zip"):
35             vnf_extract_path = fileutil.unzip_csar_to_tmp(local_path)
36             artifact_path = fileutil.get_artifact_path(vnf_extract_path, artifactPath)
37             if not artifact_path:
38                 raise ArtifactNotFoundException("Can't find artifact %s" % artifactPath)
39             with open(artifact_path, 'rt') as f:
40                 file_content = f.read()
41         else:
42             raise ArtifactNotFoundException("NF Package format is not csar or zip")
43         return file_content