34204202d4e8ef9de2252ffa52659663e1260ad1
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2018 IBM Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\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
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\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
16  */\r
17 \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
32 \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
38 \r
39 /**\r
40  * This class acts as a Rest Service that would store in the Database the Blueprints.\r
41  * @author Ruben Chang\r
42  */\r
43 \r
44 @Service\r
45 public class CbaToDatabaseService {\r
46 \r
47     //Log used to trace the transactions using the EELFLogger class\r
48     private static EELFLogger log = EELFManager.getInstance().getLogger(CbaToDatabaseService.class);\r
49 \r
50     @Autowired\r
51     private ConfigModelRepository configModelRepository;\r
52     @Autowired\r
53     private ConfigModelContentRepository configModelContentRepository;\r
54     @Autowired\r
55     private ConfigModelCreateService configModelCreateService;  \r
56         @Autowired\r
57     private CBAContentService cbaContentService;\r
58 \r
59     /**\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
63      */\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
70 \r
71         List<String> serviceTemplateDirs = ConfigModelUtils.getBlueprintNames(cbaDirectory);\r
72         if (CollectionUtils.isNotEmpty(serviceTemplateDirs)) {\r
73             for (String fileName : serviceTemplateDirs) {\r
74                 try {\r
75                     String bluePrintPath = cbaDirectory.concat("/").concat(fileName);\r
76                     log.debug("***** Loading service template :  {}", bluePrintPath);\r
77                     configModel = ConfigModelUtils.getConfigModel(bluePrintPath);\r
78 \r
79                     configModel = this.configModelCreateService.saveConfigModel(configModel);\r
80 \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
84                 }\r
85             }\r
86         } else {\r
87             throw new BluePrintException("Invalid structure. The unzipped file does not contains Blueprints");\r
88         }\r
89 \r
90         byte[] file;\r
91         try {\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
95         }\r
96 \r
97         cbaContent = this.cbaContentService.saveCBAContent(cbaFileName, version, CbaStateEnum.DRAFT.getState(), description, file);\r
98         configModel.setConfigModelCBA(cbaContent);\r
99 \r
100         return configModel;\r
101     }\r
102 \r
103     /**\r
104      * This is a deleteConfigModel method\r
105      *\r
106      * @param id id\r
107      * @throws BluePrintException BluePrintException\r
108      */\r
109     public void deleteCBA(@NotNull Long id) throws BluePrintException {\r
110         Optional<ConfigModel> dbConfigModel = configModelRepository.findById(id);\r
111 \r
112        //TODO: Delete CBA and COnfigModel\r
113 \r
114     }\r
115         \r
116         /**\r
117      * Get a list of the controller blueprint archives\r
118      * @return List<CbaContent> List with the controller blueprint archives\r
119      */\r
120     public List<CbaContent> listCBAFiles() {\r
121         return this.cbaContentService.getList();\r
122     }\r
123 \r
124     /**\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
128      */\r
129     public Optional<CbaContent> findByUUID(String uuID) {\r
130         return this.cbaContentService.findByUUID(uuID);\r
131     }\r
132 }