--- /dev/null
+/*\r
+ * Copyright © 2018 IBM Intellectual Property.\r
+ * Modifications Copyright © 2018 IBM.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.ccsdk.apps.controllerblueprints.service;\r
+import com.att.eelf.configuration.EELFLogger;\r
+import com.att.eelf.configuration.EELFManager;\r
+import org.apache.commons.collections.CollectionUtils;\r
+import org.jetbrains.annotations.NotNull;\r
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelContentRepository;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum;\r
+import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.stereotype.Service;\r
+\r
+import java.io.IOException;\r
+import java.nio.file.Files;\r
+import java.nio.file.Path;\r
+import java.util.List;\r
+import java.util.Optional;\r
+\r
+/**\r
+ * This class acts as a Rest Service that would store in the Database the Blueprints.\r
+ * @author Ruben Chang\r
+ */\r
+\r
+@Service\r
+public class CbaToDatabaseService {\r
+\r
+ //Log used to trace the transactions using the EELFLogger class\r
+ private static EELFLogger log = EELFManager.getInstance().getLogger(CbaToDatabaseService.class);\r
+\r
+ @Autowired\r
+ private ConfigModelRepository configModelRepository;\r
+ @Autowired\r
+ private ConfigModelContentRepository configModelContentRepository;\r
+ @Autowired\r
+ private ConfigModelCreateService configModelCreateService; \r
+ @Autowired\r
+ private CBAContentService cbaContentService;\r
+\r
+ /**\r
+ * This method will store the blueprints into the DB on the tables CONFIG_MODEL and CONFIG_MODEL_CONTENT\r
+ * @param cbaArchiveToSave Path in which the components are stored\r
+ * @return ConfigModel The Blueprint object stored in the DB\r
+ */\r
+ public ConfigModel storeBluePrints(String cbaDirectory, String cbaFileName, Path cbaArchiveToSave) throws BluePrintException {\r
+ log.info("*************************** storeBluePrints **********************");\r
+ ConfigModel configModel = null;\r
+ CbaContent cbaContent;\r
+ String version = "1.0";//TODO Read these information from metadata\r
+ String description = "Initial description for CBA archive " + cbaFileName;//TODO\r
+\r
+ List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(cbaDirectory);\r
+ if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {\r
+ for (String fileName : serviceTemplateDirs) {\r
+ try {\r
+ String bluePrintPath = cbaDirectory.concat("/").concat(fileName);\r
+ log.debug("***** Loading service template : {}", bluePrintPath);\r
+ configModel = ConfigModelUtils.getConfigModel(bluePrintPath);\r
+\r
+ configModel = this.configModelCreateService.saveConfigModel(configModel);\r
+\r
+ log.info("Loaded service template successfully: {}", fileName);\r
+ } catch (Exception e) {\r
+ throw new BluePrintException("Load config model " + fileName + " error : "+e.getMessage());\r
+ }\r
+ }\r
+ } else {\r
+ throw new BluePrintException("Invalid structure. The unzipped file does not contains Blueprints");\r
+ }\r
+\r
+ byte[] file;\r
+ try {\r
+ file = Files.readAllBytes(cbaArchiveToSave);\r
+ } catch (IOException e) {\r
+ throw new BluePrintException("Fail to read the CBA to save in database.", e);\r
+ }\r
+\r
+ cbaContent = this.cbaContentService.saveCBAContent(cbaFileName, version, CbaStateEnum.DRAFT.getState(), description, file);\r
+ configModel.setConfigModelCBA(cbaContent);\r
+\r
+ return configModel;\r
+ }\r
+\r
+ /**\r
+ * This is a deleteConfigModel method\r
+ *\r
+ * @param id id\r
+ * @throws BluePrintException BluePrintException\r
+ */\r
+ public void deleteCBA(@NotNull Long id) throws BluePrintException {\r
+ Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);\r
+\r
+ //TODO: Delete CBA and COnfigModel\r
+\r
+ }\r
+ \r
+ /**\r
+ * Get a list of the controller blueprint archives\r
+ * @return List<CbaContent> List with the controller blueprint archives\r
+ */\r
+ public List<CbaContent> listCBAFiles() {\r
+ return this.cbaContentService.getList();\r
+ }\r
+\r
+ /**\r
+ * Find a Controller Blueprint Archive by UUID\r
+ * @param uuID the User Identifier Controller Blueprint archive\r
+ * @return Optional<CbaContent> the Controller Blueprint archive\r
+ */\r
+ public Optional<CbaContent> findByUUID(String uuID) {\r
+ return this.cbaContentService.findByUUID(uuID);\r
+ }\r
+}
\ No newline at end of file