4bb87d7fca5822755fea12fb9352e9f0949c5de0
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T 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 \r
20 import org.apache.commons.lang3.StringUtils;\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.data.EntrySchema;\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition;\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition;\r
26 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary;\r
27 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository;\r
28 import org.onap.ccsdk.apps.controllerblueprints.service.validator.ResourceDictionaryValidator;\r
29 import org.springframework.stereotype.Service;\r
30 \r
31 import java.util.List;\r
32 import java.util.Optional;\r
33 \r
34 /**\r
35  * ResourceDictionaryService.java Purpose: Provide DataDictionaryService Service\r
36  * DataDictionaryService\r
37  *\r
38  * @author Brinda Santh\r
39  * @version 1.0\r
40  */\r
41 @Service\r
42 public class ResourceDictionaryService {\r
43 \r
44     private ResourceDictionaryRepository resourceDictionaryRepository;\r
45 \r
46     /**\r
47      * This is a DataDictionaryService, used to save and get the Resource Mapping stored in database\r
48      * \r
49      * @param dataDictionaryRepository\r
50      * \r
51      */\r
52     public ResourceDictionaryService(ResourceDictionaryRepository dataDictionaryRepository) {\r
53         this.resourceDictionaryRepository = dataDictionaryRepository;\r
54     }\r
55 \r
56     /**\r
57      * This is a getDataDictionaryByName service\r
58      * \r
59      * @param name\r
60      * @return DataDictionary\r
61      * @throws BluePrintException\r
62      */\r
63     public ResourceDictionary getResourceDictionaryByName(String name) throws BluePrintException {\r
64         if (StringUtils.isNotBlank(name)) {\r
65             return resourceDictionaryRepository.findByName(name).get();\r
66         } else {\r
67             throw new BluePrintException("Resource Mapping Name Information is missing.");\r
68         }\r
69     }\r
70 \r
71     /**\r
72      * This is a searchResourceDictionaryByNames service\r
73      * \r
74      * @param names\r
75      * @return List<ResourceDictionary>\r
76      * @throws BluePrintException\r
77      */\r
78     public List<ResourceDictionary> searchResourceDictionaryByNames(List<String> names)\r
79             throws BluePrintException {\r
80         if (names != null && !names.isEmpty()) {\r
81             return resourceDictionaryRepository.findByNameIn(names);\r
82         } else {\r
83             throw new BluePrintException("No Search Information provide");\r
84         }\r
85     }\r
86 \r
87     /**\r
88      * This is a searchResourceDictionaryByTags service\r
89      * \r
90      * @param tags\r
91      * @return List<ResourceDictionary>\r
92      * @throws BluePrintException\r
93      */\r
94     public List<ResourceDictionary> searchResourceDictionaryByTags(String tags) throws BluePrintException {\r
95         if (StringUtils.isNotBlank(tags)) {\r
96             return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags);\r
97         } else {\r
98             throw new BluePrintException("No Search Information provide");\r
99         }\r
100     }\r
101 \r
102     /**\r
103      * This is a saveDataDictionary service\r
104      * \r
105      * @param resourceDictionary\r
106      * @return DataDictionary\r
107      * @throws BluePrintException\r
108      */\r
109     public ResourceDictionary saveResourceDictionary(ResourceDictionary resourceDictionary)\r
110             throws BluePrintException {\r
111         if (resourceDictionary != null) {\r
112             ResourceDictionaryValidator.validateResourceDictionary(resourceDictionary);\r
113 \r
114             ResourceDefinition resourceDefinition =\r
115                     JacksonUtils.readValue(resourceDictionary.getDefinition(), ResourceDefinition.class);\r
116 \r
117             if (resourceDefinition == null) {\r
118                 throw new BluePrintException(\r
119                         "Resource dictionary definition is not valid content " + resourceDictionary.getDefinition());\r
120             }\r
121 \r
122             resourceDefinition.setName(resourceDictionary.getName());\r
123             resourceDefinition.setResourcePath(resourceDictionary.getResourcePath());\r
124             resourceDefinition.setResourceType(resourceDictionary.getResourceType());\r
125 \r
126             PropertyDefinition propertyDefinition = new PropertyDefinition();\r
127             propertyDefinition.setType(resourceDictionary.getDataType());\r
128             propertyDefinition.setDescription(resourceDictionary.getDescription());\r
129             if(StringUtils.isNotBlank(resourceDictionary.getEntrySchema())){\r
130                 EntrySchema entrySchema = new EntrySchema();\r
131                 entrySchema.setType(resourceDictionary.getEntrySchema());\r
132                 propertyDefinition.setEntrySchema(entrySchema);\r
133             }else{\r
134                 propertyDefinition.setEntrySchema(null);\r
135             }\r
136             resourceDefinition.setTags(resourceDictionary.getTags());\r
137             resourceDefinition.setUpdatedBy(resourceDictionary.getUpdatedBy());\r
138 \r
139             String definitionContent = JacksonUtils.getJson(resourceDefinition, true);\r
140             resourceDictionary.setDefinition(definitionContent);\r
141 \r
142             Optional<ResourceDictionary> dbResourceDictionaryData =\r
143                     resourceDictionaryRepository.findByName(resourceDictionary.getName());\r
144             if (dbResourceDictionaryData.isPresent()) {\r
145                 ResourceDictionary dbResourceDictionary = dbResourceDictionaryData.get();\r
146 \r
147                 dbResourceDictionary.setName(resourceDictionary.getName());\r
148                 dbResourceDictionary.setDefinition(resourceDictionary.getDefinition());\r
149                 dbResourceDictionary.setDescription(resourceDictionary.getDescription());\r
150                 dbResourceDictionary.setResourceType(resourceDictionary.getResourceType());\r
151                 dbResourceDictionary.setResourcePath(resourceDictionary.getResourcePath());\r
152                 dbResourceDictionary.setDataType(resourceDictionary.getDataType());\r
153                 dbResourceDictionary.setEntrySchema(resourceDictionary.getEntrySchema());\r
154                 dbResourceDictionary.setTags(resourceDictionary.getTags());\r
155                 dbResourceDictionary.setValidValues(resourceDictionary.getValidValues());\r
156                 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary);\r
157             } else {\r
158                 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary);\r
159             }\r
160         } else {\r
161             throw new BluePrintException("Resource Dictionary information is missing");\r
162         }\r
163         return resourceDictionary;\r
164     }\r
165 \r
166     /**\r
167      * This is a deleteResourceDictionary service\r
168      * \r
169      * @param name\r
170      * @throws BluePrintException\r
171      */\r
172     public void deleteResourceDictionary(String name) throws BluePrintException {\r
173         if (name != null) {\r
174             resourceDictionaryRepository.deleteByName(name);\r
175         } else {\r
176             throw new BluePrintException("Resource Mapping Id Information is missing.");\r
177         }\r
178 \r
179     }\r
180 }\r