2 * Copyright © 2018 IBM Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\r
5 * Licensed under the Apache License, Version 2.0 (the "License");
\r
6 * you may not use this file except in compliance with the License.
\r
7 * You may obtain a copy of the License at
\r
9 * http://www.apache.org/licenses/LICENSE-2.0
\r
11 * Unless required by applicable law or agreed to in writing, software
\r
12 * distributed under the License is distributed on an "AS IS" BASIS,
\r
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 * See the License for the specific language governing permissions and
\r
15 * limitations under the License.
\r
18 package org.onap.ccsdk.apps.controllerblueprints.service;
\r
19 import com.att.eelf.configuration.EELFLogger;
\r
20 import com.att.eelf.configuration.EELFManager;
\r
21 import org.apache.commons.collections.CollectionUtils;
\r
22 import org.jetbrains.annotations.NotNull;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.service.domain.CbaContent;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ConfigModel;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelContentRepository;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ConfigModelRepository;
\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.utils.CbaStateEnum;
\r
29 import org.onap.ccsdk.apps.controllerblueprints.service.utils.ConfigModelUtils;
\r
30 import org.springframework.beans.factory.annotation.Autowired;
\r
31 import org.springframework.stereotype.Service;
\r
33 import java.io.IOException;
\r
34 import java.nio.file.Files;
\r
35 import java.nio.file.Path;
\r
36 import java.util.List;
\r
37 import java.util.Optional;
\r
40 * This class acts as a Rest Service that would store in the Database the Blueprints.
\r
41 * @author Ruben Chang
\r
45 public class CbaToDatabaseService {
\r
47 //Log used to trace the transactions using the EELFLogger class
\r
48 private static EELFLogger log = EELFManager.getInstance().getLogger(CbaToDatabaseService.class);
\r
51 private ConfigModelRepository configModelRepository;
\r
53 private ConfigModelContentRepository configModelContentRepository;
\r
55 private ConfigModelCreateService configModelCreateService;
\r
57 private CBAContentService cbaContentService;
\r
60 * This method will store the blueprints into the DB on the tables CONFIG_MODEL and CONFIG_MODEL_CONTENT
\r
61 * @param cbaArchiveToSave Path in which the components are stored
\r
62 * @return ConfigModel The Blueprint object stored in the DB
\r
64 public ConfigModel storeBluePrints(String cbaDirectory, String cbaFileName, Path cbaArchiveToSave) throws BluePrintException {
\r
65 log.info("*************************** storeBluePrints **********************");
\r
66 ConfigModel configModel = null;
\r
67 CbaContent cbaContent;
\r
68 String version = "1.0";//TODO Read these information from metadata
\r
69 String description = "Initial description for CBA archive " + cbaFileName;//TODO
\r
71 List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(cbaDirectory);
\r
72 if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {
\r
73 for (String fileName : serviceTemplateDirs) {
\r
75 String bluePrintPath = cbaDirectory.concat("/").concat(fileName);
\r
76 log.debug("***** Loading service template : {}", bluePrintPath);
\r
77 configModel = ConfigModelUtils.getConfigModel(bluePrintPath);
\r
79 configModel = this.configModelCreateService.saveConfigModel(configModel);
\r
81 log.info("Loaded service template successfully: {}", fileName);
\r
82 } catch (Exception e) {
\r
83 throw new BluePrintException("Load config model " + fileName + " error : "+e.getMessage());
\r
87 throw new BluePrintException("Invalid structure. The unzipped file does not contains Blueprints");
\r
92 file = Files.readAllBytes(cbaArchiveToSave);
\r
93 } catch (IOException e) {
\r
94 throw new BluePrintException("Fail to read the CBA to save in database.", e);
\r
97 cbaContent = this.cbaContentService.saveCBAContent(cbaFileName, version, CbaStateEnum.DRAFT.getState(), description, file);
\r
98 configModel.setConfigModelCBA(cbaContent);
\r
100 return configModel;
\r
104 * This is a deleteConfigModel method
\r
107 * @throws BluePrintException BluePrintException
\r
109 public void deleteCBA(@NotNull Long id) throws BluePrintException {
\r
110 Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);
\r
112 //TODO: Delete CBA and COnfigModel
\r
117 * Get a list of the controller blueprint archives
\r
118 * @return List<CbaContent> List with the controller blueprint archives
\r
120 public List<CbaContent> listCBAFiles() {
\r
121 return this.cbaContentService.getList();
\r
125 * Find a Controller Blueprint Archive by UUID
\r
126 * @param uuID the User Identifier Controller Blueprint archive
\r
127 * @return Optional<CbaContent> the Controller Blueprint archive
\r
129 public Optional<CbaContent> findByUUID(String uuID) {
\r
130 return this.cbaContentService.findByUUID(uuID);
\r