2 * Copyright © 2017-2018 AT&T 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
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
31 import java.util.List;
\r
32 import java.util.Optional;
\r
35 * ResourceDictionaryService.java Purpose: Provide DataDictionaryService Service
\r
36 * DataDictionaryService
\r
38 * @author Brinda Santh
\r
42 public class ResourceDictionaryService {
\r
44 private ResourceDictionaryRepository resourceDictionaryRepository;
\r
47 * This is a DataDictionaryService, used to save and get the Resource Mapping stored in database
\r
49 * @param dataDictionaryRepository
\r
52 public ResourceDictionaryService(ResourceDictionaryRepository dataDictionaryRepository) {
\r
53 this.resourceDictionaryRepository = dataDictionaryRepository;
\r
57 * This is a getDataDictionaryByName service
\r
60 * @return DataDictionary
\r
61 * @throws BluePrintException
\r
63 public ResourceDictionary getResourceDictionaryByName(String name) throws BluePrintException {
\r
64 if (StringUtils.isNotBlank(name)) {
\r
65 return resourceDictionaryRepository.findByName(name).get();
\r
67 throw new BluePrintException("Resource Mapping Name Information is missing.");
\r
72 * This is a searchResourceDictionaryByNames service
\r
75 * @return List<ResourceDictionary>
\r
76 * @throws BluePrintException
\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
83 throw new BluePrintException("No Search Information provide");
\r
88 * This is a searchResourceDictionaryByTags service
\r
91 * @return List<ResourceDictionary>
\r
92 * @throws BluePrintException
\r
94 public List<ResourceDictionary> searchResourceDictionaryByTags(String tags) throws BluePrintException {
\r
95 if (StringUtils.isNotBlank(tags)) {
\r
96 return resourceDictionaryRepository.findByTagsContainingIgnoreCase(tags);
\r
98 throw new BluePrintException("No Search Information provide");
\r
103 * This is a saveDataDictionary service
\r
105 * @param resourceDictionary
\r
106 * @return DataDictionary
\r
107 * @throws BluePrintException
\r
109 public ResourceDictionary saveResourceDictionary(ResourceDictionary resourceDictionary)
\r
110 throws BluePrintException {
\r
111 if (resourceDictionary != null) {
\r
112 ResourceDictionaryValidator.validateResourceDictionary(resourceDictionary);
\r
114 ResourceDefinition resourceDefinition =
\r
115 JacksonUtils.readValue(resourceDictionary.getDefinition(), ResourceDefinition.class);
\r
117 if (resourceDefinition == null) {
\r
118 throw new BluePrintException(
\r
119 "Resource dictionary definition is not valid content " + resourceDictionary.getDefinition());
\r
122 resourceDefinition.setName(resourceDictionary.getName());
\r
123 resourceDefinition.setResourcePath(resourceDictionary.getResourcePath());
\r
124 resourceDefinition.setResourceType(resourceDictionary.getResourceType());
\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
134 propertyDefinition.setEntrySchema(null);
\r
136 resourceDefinition.setTags(resourceDictionary.getTags());
\r
137 resourceDefinition.setUpdatedBy(resourceDictionary.getUpdatedBy());
\r
139 String definitionContent = JacksonUtils.getJson(resourceDefinition, true);
\r
140 resourceDictionary.setDefinition(definitionContent);
\r
142 Optional<ResourceDictionary> dbResourceDictionaryData =
\r
143 resourceDictionaryRepository.findByName(resourceDictionary.getName());
\r
144 if (dbResourceDictionaryData.isPresent()) {
\r
145 ResourceDictionary dbResourceDictionary = dbResourceDictionaryData.get();
\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
158 resourceDictionary = resourceDictionaryRepository.save(resourceDictionary);
\r
161 throw new BluePrintException("Resource Dictionary information is missing");
\r
163 return resourceDictionary;
\r
167 * This is a deleteResourceDictionary service
\r
170 * @throws BluePrintException
\r
172 public void deleteResourceDictionary(String name) throws BluePrintException {
\r
173 if (name != null) {
\r
174 resourceDictionaryRepository.deleteByName(name);
\r
176 throw new BluePrintException("Resource Mapping Id Information is missing.");
\r