Support querying of model by type
[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.datatypes.enums.ModelTypeEnum;
29 import org.openecomp.sdc.be.model.Model;
30 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
31 import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
32 import org.openecomp.sdc.common.zip.ZipUtils;
33 import org.openecomp.sdc.common.zip.exception.ZipException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 /**
40  * This class is responsible for handling the business logic of a Model.
41  */
42 @Component("modelBusinessLogic")
43 public class ModelBusinessLogic {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(ModelBusinessLogic.class);
46     private final ModelOperation modelOperation;
47     private final DataTypeImportManager dataTypeImportManager;
48
49     @Autowired
50     public ModelBusinessLogic(final ModelOperation modelOperation, final DataTypeImportManager dataTypeImportManager) {
51         this.modelOperation = modelOperation;
52         this.dataTypeImportManager = dataTypeImportManager;
53     }
54
55     public Model createModel(final Model model, final String datatypesYaml) {
56         createModel(model);
57         dataTypeImportManager.createDataTypes(datatypesYaml, model.getName(), false);
58         return model;
59     }
60
61     public Model createModel(final Model model) {
62         LOGGER.debug("creating model {}", model);
63         return modelOperation.createModel(model, false);
64     }
65
66     public Optional<Model> findModel(final String modelName) {
67         if (StringUtils.isEmpty(modelName)) {
68             return Optional.empty();
69         }
70         return modelOperation.findModelByName(modelName);
71     }
72
73     /**
74      * Loads the list of models.
75      *
76      * @return the list of models
77      */
78     public List<Model> listModels() {
79         return modelOperation.findAllModels();
80     }
81     
82     public List<Model> listModels(final ModelTypeEnum modelType) {
83         return modelOperation.findModels(modelType);
84     }
85
86     public void createModelImports(final String modelName, final InputStream modelImportsZip) {
87         if (StringUtils.isEmpty(modelName)) {
88             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
89         }
90         if (modelImportsZip == null) {
91             throw ModelOperationExceptionSupplier.emptyModelImports().get();
92         }
93         if (findModel(modelName).isEmpty()) {
94             throw ModelOperationExceptionSupplier.invalidModel(modelName).get();
95         }
96
97         final var fileBytes = readBytes(modelImportsZip);
98         final Map<String, byte[]> zipFilesPathContentMap = unzipInMemory(fileBytes);
99         if (zipFilesPathContentMap.isEmpty()) {
100             throw ModelOperationExceptionSupplier.emptyModelImports().get();
101         }
102
103         modelOperation.createModelImports(modelName, zipFilesPathContentMap);
104     }
105
106     private Map<String, byte[]> unzipInMemory(final byte[] fileBytes) {
107         try {
108             return ZipUtils.readZip(fileBytes, false);
109         } catch (final ZipException e) {
110             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
111         }
112     }
113
114     private byte[] readBytes(final InputStream modelImportsZip) {
115         try (final InputStream in = modelImportsZip; final ByteArrayOutputStream os = new ByteArrayOutputStream()) {
116             final var buffer = new byte[1024];
117             int len;
118             while ((len = in.read(buffer)) != -1) {
119                 os.write(buffer, 0, len);
120             }
121             return os.toByteArray();
122         } catch (final IOException e) {
123             LOGGER.debug("Could not read the model imports zip", e);
124             throw ModelOperationExceptionSupplier.couldNotReadImports().get();
125         }
126     }
127 }