Add models imports endpoint and persistence structure
[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.Map;
25 import java.util.Optional;
26 import org.apache.commons.lang3.StringUtils;
27 import org.openecomp.sdc.be.model.Model;
28 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
29 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
30 import org.openecomp.sdc.common.zip.ZipUtils;
31 import org.openecomp.sdc.common.zip.exception.ZipException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36 @Component("modelBusinessLogic")
37 public class ModelBusinessLogic {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(ModelBusinessLogic.class);
40     private final ModelOperation modelOperation;
41
42     @Autowired
43     public ModelBusinessLogic(final ModelOperation modelOperation) {
44         this.modelOperation = modelOperation;
45     }
46
47     public Model createModel(final Model model) {
48         LOGGER.debug("createModel: creating model {}", model);
49         return modelOperation.createModel(model, false);
50     }
51
52     public Optional<Model> findModel(final String modelName) {
53         if (StringUtils.isEmpty(modelName)) {
54             return Optional.empty();
55         }
56         return modelOperation.findModelByName(modelName);
57     }
58
59     public void createModelImports(final String modelName, final InputStream modelImportsZip) {
60         if (StringUtils.isEmpty(modelName)) {
61             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
62         }
63         if (modelImportsZip == null) {
64             throw ModelOperationExceptionSupplier.emptyModelImports().get();
65         }
66         if (findModel(modelName).isEmpty()) {
67             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
68         }
69
70         final var fileBytes = readBytes(modelImportsZip);
71         final Map<String, byte[]> zipFilesPathContentMap = unzipInMemory(fileBytes);
72         if (zipFilesPathContentMap.isEmpty()) {
73             throw ModelOperationExceptionSupplier.emptyModelImports().get();
74         }
75
76         modelOperation.createModelImports(modelName, zipFilesPathContentMap);
77     }
78
79     private Map<String, byte[]> unzipInMemory(final byte[] fileBytes) {
80         try {
81             return ZipUtils.readZip(fileBytes, false);
82         } catch (final ZipException e) {
83             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
84         }
85     }
86
87     private byte[] readBytes(final InputStream modelImportsZip) {
88         try (final InputStream in = modelImportsZip; final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
89             final var buffer = new byte[1024];
90             int len;
91             while ((len = in.read(buffer)) != -1) {
92                 os.write(buffer, 0, len);
93             }
94             return os.toByteArray();
95         } catch (final IOException e) {
96             LOGGER.debug("Could not read the model imports zip", e);
97             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
98         }
99     }
100 }