Import VSP with non-standard data types
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / ModelBusinessLogic.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19 package org.openecomp.sdc.be.components.impl;
20
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdc.be.model.Model;
29 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
30 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
31 import org.openecomp.sdc.common.zip.ZipUtils;
32 import org.openecomp.sdc.common.zip.exception.ZipException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37
38 /**
39  * This class is responsible for handling the business logic of a Model.
40  */
41 @Component("modelBusinessLogic")
42 public class ModelBusinessLogic {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(ModelBusinessLogic.class);
45     private final ModelOperation modelOperation;
46     private final DataTypeImportManager dataTypeImportManager;
47
48     @Autowired
49     public ModelBusinessLogic(final ModelOperation modelOperation, final DataTypeImportManager dataTypeImportManager) {
50         this.modelOperation = modelOperation;
51         this.dataTypeImportManager = dataTypeImportManager;
52     }
53     
54     public Model createModel(final Model model, final String datatypesYaml) {
55         createModel(model);
56         dataTypeImportManager.createDataTypes(datatypesYaml, model.getName());
57         return model;
58     }
59
60     public Model createModel(final Model model) {
61         LOGGER.debug("creating model {}", model);
62         return modelOperation.createModel(model, false);
63     }
64
65     public Optional<Model> findModel(final String modelName) {
66         if (StringUtils.isEmpty(modelName)) {
67             return Optional.empty();
68         }
69         return modelOperation.findModelByName(modelName);
70     }
71
72     /**
73      * Loads the list of models.
74      *
75      * @return the list of models
76      */
77     public List<Model> listModels() {
78         return modelOperation.findAllModels();
79     }
80
81     public void createModelImports(final String modelName, final InputStream modelImportsZip) {
82         if (StringUtils.isEmpty(modelName)) {
83             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
84         }
85         if (modelImportsZip == null) {
86             throw ModelOperationExceptionSupplier.emptyModelImports().get();
87         }
88         if (findModel(modelName).isEmpty()) {
89             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
90         }
91
92         final var fileBytes = readBytes(modelImportsZip);
93         final Map<String, byte[]> zipFilesPathContentMap = unzipInMemory(fileBytes);
94         if (zipFilesPathContentMap.isEmpty()) {
95             throw ModelOperationExceptionSupplier.emptyModelImports().get();
96         }
97
98         modelOperation.createModelImports(modelName, zipFilesPathContentMap);
99     }
100
101     private Map<String, byte[]> unzipInMemory(final byte[] fileBytes) {
102         try {
103             return ZipUtils.readZip(fileBytes, false);
104         } catch (final ZipException e) {
105             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
106         }
107     }
108
109     private byte[] readBytes(final InputStream modelImportsZip) {
110         try (final InputStream in = modelImportsZip; final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
111             final var buffer = new byte[1024];
112             int len;
113             while ((len = in.read(buffer)) != -1) {
114                 os.write(buffer, 0, len);
115             }
116             return os.toByteArray();
117         } catch (final IOException e) {
118             LOGGER.debug("Could not read the model imports zip", e);
119             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
120         }
121     }
122 }