947f33922ebb106492b89fd94cbdc655467a40b0
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct.utils;
22
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.sdc.logging.api.Logger;
25 import org.openecomp.sdc.logging.api.LoggerFactory;
26 import org.openecomp.sdc.vendorlicense.dao.types.VendorLicenseModelEntity;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
28 import org.openecomp.sdc.versioning.dao.types.Version;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.List;
35 import java.util.zip.ZipEntry;
36 import java.util.zip.ZipOutputStream;
37
38 public class VSPCommon {
39
40   private static final Logger log = (Logger) LoggerFactory.getLogger(VSPCommon.class.getName());
41
42   public static VspDetails createVspDetails(String id, Version version, String name, String desc,
43                                             String vendorName, String vlm, String icon,
44                                             String category, String subCategory,
45                                             String licenseAgreement, List<String> featureGroups) {
46     VspDetails vspDetails = new VspDetails(id, version);
47     vspDetails.setName(name);
48     vspDetails.setDescription(desc);
49     vspDetails.setIcon(icon);
50     vspDetails.setCategory(category);
51     vspDetails.setSubCategory(subCategory);
52     vspDetails.setVendorName(vendorName);
53     vspDetails.setVendorId(vlm);
54     vspDetails.setVlmVersion(new Version(1, 0));
55     vspDetails.setLicenseAgreement(licenseAgreement);
56     vspDetails.setFeatureGroups(featureGroups);
57     return vspDetails;
58   }
59
60
61   public static VendorLicenseModelEntity createVendorLicenseModel(String name, String desc,
62                                                                   String icon) {
63     VendorLicenseModelEntity vendorLicenseModel = new VendorLicenseModelEntity();
64     vendorLicenseModel.setVendorName(name);
65     vendorLicenseModel.setDescription(desc);
66     vendorLicenseModel.setIconRef(icon);
67     return vendorLicenseModel;
68   }
69
70   public static void zipDir(File file, String path, ZipOutputStream zos) {
71     zipDir(file, path, zos, false);
72   }
73
74   public static void zipDir(File file, String path, ZipOutputStream zos, boolean isRootDir) {
75     if (file.isDirectory()) {
76       path += File.separator + file.getName();
77       File[] files = file.listFiles();
78       if (files != null) {
79         for (File innerFile : files) {
80           if (isRootDir) {
81             zipDir(innerFile, "", zos, false);
82           } else {
83             zipDir(innerFile, path, zos, false);
84           }
85         }
86       }
87     } else {
88
89       try {
90         if (!path.isEmpty()) {
91           path += File.separator;
92         }
93         zos.putNextEntry(new ZipEntry(path + file.getName()));
94         InputStream is = new FileInputStream(file);
95         byte[] data = FileUtils.toByteArray(is);
96         zos.write(data);
97         zos.closeEntry();
98       } catch (IOException exception) {
99         log.debug("",exception);
100       }
101     }
102   }
103
104 }