Add log and comment
[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     """
26     Fetch the artifact of VNF package
27     """
28     def fetch(self, vnfPkgId, artifactPath):
29         """
30         Fetch artifact by given vnf package id and the path of artifact
31         :param vnfPkgId:
32         :param artifactPath:
33         :return:
34         """
35         logger.debug("FetchVnfPkgArtifact--get--single--artifact--biz::>"
36                      "ID: %s path: %s" % (vnfPkgId, artifactPath))
37         vnf_pkg = VnfPackageModel.objects.filter(vnfPackageId=vnfPkgId)
38         if not vnf_pkg.exists():
39             err_msg = "NF Package (%s) doesn't exists." % vnfPkgId
40             raise ResourceNotFoundException(err_msg)
41         vnf_pkg = vnf_pkg.get()
42         local_path = vnf_pkg.localFilePath
43         if local_path.endswith(".csar") or local_path.endswith(".zip"):
44             vnf_extract_path = fileutil.unzip_csar_to_tmp(local_path)
45             artifact_path = fileutil.get_artifact_path(vnf_extract_path, artifactPath)
46             if not artifact_path:
47                 raise ArtifactNotFoundException("Can't find artifact %s" % artifactPath)
48             with open(artifact_path, 'rt') as f:
49                 file_content = f.read()
50         else:
51             raise ArtifactNotFoundException("NF Package format is not csar or zip")
52         return file_content