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.config.model.utils;
\r
20 import java.util.Map;
\r
21 import java.util.Optional;
\r
22 import java.util.function.Supplier;
\r
23 import org.apache.commons.collections.CollectionUtils;
\r
24 import org.apache.commons.collections.MapUtils;
\r
25 import org.apache.commons.lang3.StringUtils;
\r
26 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
27 import org.onap.ccsdk.config.model.data.PropertyDefinition;
\r
28 import org.onap.ccsdk.config.model.data.ResourceAssignment;
\r
29 import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;
\r
30 import org.onap.ccsdk.config.model.data.dict.SourcesDefinition;
\r
31 import com.att.eelf.configuration.EELFLogger;
\r
32 import com.att.eelf.configuration.EELFManager;
\r
35 * ResourceDictionaryUtils.java Purpose to provide ResourceDictionaryUtils
\r
39 public class ResourceDictionaryUtils {
\r
41 private ResourceDictionaryUtils() {
\r
45 private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceDictionaryUtils.class);
\r
48 * This Method is to assign the source name to the Dictionary Definition Check to see if the source
\r
49 * definition is not present then assign, if more than one source then assign only one source.
\r
51 * @param resourceAssignment
\r
52 * @param resourceDefinition
\r
54 @SuppressWarnings("squid:S3776")
\r
55 public static void populateSourceMapping(ResourceAssignment resourceAssignment,
\r
56 ResourceDefinition resourceDefinition) {
\r
58 if (resourceAssignment != null && resourceDefinition != null
\r
59 && StringUtils.isBlank(resourceAssignment.getDictionarySource())) {
\r
61 setProperty(resourceAssignment, resourceDefinition);
\r
62 Map<String, SourcesDefinition> sourcesDefinition = resourceDefinition.getSources();
\r
64 if (sourcesDefinition != null && MapUtils.isNotEmpty(sourcesDefinition) && sourcesDefinition.size() == 1) {
\r
65 if (sourcesDefinition.get("input") != null) {
\r
66 resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_INPUT);
\r
67 } else if (sourcesDefinition.get("default") != null) {
\r
68 resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_DEFAULT);
\r
69 } else if (sourcesDefinition.get("db") != null) {
\r
70 resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_DB);
\r
71 if (resolve(() -> sourcesDefinition.get("db").getProperties().getDependencies()).isPresent()
\r
73 .isNotEmpty(sourcesDefinition.get("db").getProperties().getDependencies())) {
\r
75 .setDependencies(sourcesDefinition.get("db").getProperties().getDependencies());
\r
77 } else if (sourcesDefinition.get("mdsal") != null) {
\r
78 resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_MDSAL);
\r
79 if (resolve(() -> sourcesDefinition.get("mdsal").getProperties().getDependencies()).isPresent()
\r
81 .isNotEmpty(sourcesDefinition.get("mdsal").getProperties().getDependencies())) {
\r
83 .setDependencies(sourcesDefinition.get("mdsal").getProperties().getDependencies());
\r
86 logger.info("automapped resourceAssignment : {}", resourceAssignment);
\r
91 public static <T> Optional<T> resolve(Supplier<T> resolver) {
\r
93 T result = resolver.get();
\r
94 return Optional.ofNullable(result);
\r
95 } catch (NullPointerException e) {
\r
96 return Optional.empty();
\r
101 * Overriding ResourceAssignment Properties with properties defined in Dictionary
\r
103 private static void setProperty(ResourceAssignment resourceAssignment, ResourceDefinition resourceDefinition) {
\r
104 if (StringUtils.isNotBlank(resourceDefinition.getProperty().getType())) {
\r
105 PropertyDefinition property = resourceAssignment.getProperty();
\r
106 if (property == null) {
\r
107 property = new PropertyDefinition();
\r
109 if (resourceDefinition.getProperty() != null) {
\r
110 property.setType(resourceDefinition.getProperty().getType());
\r
111 if (resourceDefinition.getProperty().getEntrySchema() != null) {
\r
112 property.setEntrySchema(resourceDefinition.getProperty().getEntrySchema());
\r
114 resourceAssignment.setProperty(property);
\r