b9567db1391b6dd87945478eeae47c74d74ef56e
[ccsdk/cds.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.ccsdk.apps.controllerblueprints.service;\r
18 \r
19 import org.apache.commons.lang3.StringUtils;\r
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;\r
21 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils;\r
22 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.data.DictionaryDefinition;\r
23 import org.onap.ccsdk.apps.controllerblueprints.service.domain.ResourceDictionary;\r
24 import org.onap.ccsdk.apps.controllerblueprints.service.repository.ResourceDictionaryRepository;\r
25 import org.onap.ccsdk.apps.controllerblueprints.service.validator.ResourceDictionaryValidator;\r
26 import org.springframework.stereotype.Service;\r
27 \r
28 import java.util.List;\r
29 import java.util.Optional;\r
30 \r
31 /**\r
32  * ResourceDictionaryService.java Purpose: Provide DataDictionaryService Service\r
33  * DataDictionaryService\r
34  *\r
35  * @author Brinda Santh\r
36  * @version 1.0\r
37  */\r
38 @Service\r
39 public class ResourceDictionaryService {\r
40 \r
41     private ResourceDictionaryRepository resourceDictionaryRepository;\r
42 \r
43     /**\r
44      * This is a DataDictionaryService, used to save and get the Resource Mapping stored in database\r
45      * \r
46      * @param dataDictionaryRepository\r
47      * \r
48      */\r
49     public ResourceDictionaryService(ResourceDictionaryRepository dataDictionaryRepository) {\r
50         this.resourceDictionaryRepository = dataDictionaryRepository;\r
51     }\r
52 \r
53     /**\r
54      * This is a getDataDictionaryByName service\r
55      * \r
56      * @param name\r
57      * @return DataDictionary\r
58      * @throws BluePrintException\r
59      */\r
60     public ResourceDictionary getResourceDictionaryByName(String name) throws BluePrintException {\r
61         if (StringUtils.isNotBlank(name)) {\r
62             return resourceDictionaryRepository.findByName(name).get();\r
63         } else {\r
64             throw new BluePrintException("Resource Mapping Name Information is missing.");\r
65         }\r
66     }\r
67 \r
68     /**\r
69      * This is a searchResourceDictionaryByNames service\r
70      * \r
71      * @param names\r
72      * @return List<ResourceDictionary>\r
73      * @throws BluePrintException\r
74      */\r
75     public List<ResourceDictionary> searchResourceDictionaryByNames(List<String> names)\r
76             throws BluePrintException {\r
77         if (names != null && !names.isEmpty()) {\r
78             return resourceDictionaryRepository.findByNameIn(names);\r
79         } else {\r
80             throw new BluePrintException("No Search Information provide");\r
81         }\r
82     }\r
83 \r
84     /**\r
85      * This is a searchResourceDictionaryByTags service\r
86      * \r
87      * @param tags\r
88      * @return List<ResourceDictionary>\r
89      * @throws BluePrintException\r
90      */\r
91     public List<ResourceDictionary> searchResourceDictionaryByTags(String tags) throws BluePrintException {\r
92         if (StringUtils.isNotBlank(tags)) {\r
93             return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags);\r
94         } else {\r
95             throw new BluePrintException("No Search Information provide");\r
96         }\r
97     }\r
98 \r
99     /**\r
100      * This is a saveDataDictionary service\r
101      * \r
102      * @param resourceDictionary\r
103      * @return DataDictionary\r
104      * @throws BluePrintException\r
105      */\r
106     public ResourceDictionary saveResourceDictionary(ResourceDictionary resourceDictionary)\r
107             throws BluePrintException {\r
108         if (resourceDictionary != null) {\r
109             ResourceDictionaryValidator.validateResourceDictionary(resourceDictionary);\r
110 \r
111             DictionaryDefinition dictionaryDefinition =\r
112                     JacksonUtils.readValue(resourceDictionary.getDefinition(), DictionaryDefinition.class);\r
113 \r
114             if (dictionaryDefinition == null) {\r
115                 throw new BluePrintException(\r
116                         "Resource dictionary definition is not valid content " + resourceDictionary.getDefinition());\r
117             }\r
118 \r
119             dictionaryDefinition.setName(resourceDictionary.getName());\r
120             dictionaryDefinition.setResourcePath(resourceDictionary.getResourcePath());\r
121             dictionaryDefinition.setResourceType(resourceDictionary.getResourceType());\r
122             dictionaryDefinition.setDataType(resourceDictionary.getDataType());\r
123             dictionaryDefinition.setEntrySchema(resourceDictionary.getEntrySchema());\r
124             dictionaryDefinition.setTags(resourceDictionary.getTags());\r
125             dictionaryDefinition.setDescription(resourceDictionary.getDescription());\r
126             dictionaryDefinition.setUpdatedBy(resourceDictionary.getUpdatedBy());\r
127 \r
128             String definitionContent = JacksonUtils.getJson(dictionaryDefinition, true);\r
129             resourceDictionary.setDefinition(definitionContent);\r
130 \r
131             Optional<ResourceDictionary> dbResourceDictionaryData =\r
132                     resourceDictionaryRepository.findByName(resourceDictionary.getName());\r
133             if (dbResourceDictionaryData.isPresent()) {\r
134                 ResourceDictionary dbResourceDictionary = dbResourceDictionaryData.get();\r
135 \r
136                 dbResourceDictionary.setName(resourceDictionary.getName());\r
137                 dbResourceDictionary.setDefinition(resourceDictionary.getDefinition());\r
138                 dbResourceDictionary.setDescription(resourceDictionary.getDescription());\r
139                 dbResourceDictionary.setResourceType(resourceDictionary.getResourceType());\r
140                 dbResourceDictionary.setResourcePath(resourceDictionary.getResourcePath());\r
141                 dbResourceDictionary.setDataType(resourceDictionary.getDataType());\r
142                 dbResourceDictionary.setEntrySchema(resourceDictionary.getEntrySchema());\r
143                 dbResourceDictionary.setTags(resourceDictionary.getTags());\r
144                 dbResourceDictionary.setValidValues(resourceDictionary.getValidValues());\r
145                 resourceDictionary = resourceDictionaryRepository.save(dbResourceDictionary);\r
146             } else {\r
147                 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary);\r
148             }\r
149         } else {\r
150             throw new BluePrintException("Resource Dictionary information is missing");\r
151         }\r
152         return resourceDictionary;\r
153     }\r
154 \r
155     /**\r
156      * This is a deleteResourceDictionary service\r
157      * \r
158      * @param name\r
159      * @throws BluePrintException\r
160      */\r
161     public void deleteResourceDictionary(String name) throws BluePrintException {\r
162         if (name != null) {\r
163             resourceDictionaryRepository.deleteByName(name);\r
164         } else {\r
165             throw new BluePrintException("Resource Mapping Id Information is missing.");\r
166         }\r
167 \r
168     }\r
169 }\r