2 * Copyright © 2017-2018 AT&T Intellectual Property.
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.ccsdk.apps.controllerblueprints.resource.dict.utils;
\r
19 import com.fasterxml.jackson.databind.JsonNode;
\r
20 import org.apache.commons.collections.MapUtils;
\r
21 import org.apache.commons.lang3.StringUtils;
\r
22 import org.onap.ccsdk.apps.controllerblueprints.core.ConfigModelConstant;
\r
23 import org.onap.ccsdk.apps.controllerblueprints.core.data.EntrySchema;
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition;
\r
25 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment;
\r
26 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.data.DictionaryDefinition;
\r
27 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.data.DictionaryDependency;
\r
28 import org.slf4j.Logger;
\r
29 import org.slf4j.LoggerFactory;
\r
31 import java.util.Map;
\r
32 import java.util.Optional;
\r
33 import java.util.function.Supplier;
\r
36 * ResourceDictionaryUtils.java Purpose to provide ResourceDictionaryUtils
\r
38 * @author Brinda Santh
\r
41 public class ResourceDictionaryUtils {
\r
43 private ResourceDictionaryUtils() {
\r
47 private static final Logger log = LoggerFactory.getLogger(ResourceDictionaryUtils.class);
\r
50 * This Method is to assign the source name to the Dictionary Definition Check to see if the source
\r
51 * definition is not present then assign, if more than one source then assign only one first source.
\r
53 * @param resourceAssignment
\r
54 * @param dictionaryDefinition
\r
56 @SuppressWarnings("squid:S3776")
\r
57 public static void populateSourceMapping(ResourceAssignment resourceAssignment,
\r
58 DictionaryDefinition dictionaryDefinition) {
\r
60 if (resourceAssignment != null && dictionaryDefinition != null
\r
61 && StringUtils.isBlank(resourceAssignment.getDictionarySource())) {
\r
63 // Overwrite the Property Definitions from Dictionary
\r
64 setProperty(resourceAssignment, dictionaryDefinition);
\r
66 Map<String, JsonNode> dictionarySource = dictionaryDefinition.getSource();
\r
67 Map<String, DictionaryDependency> dictionaryDependencyMap = dictionaryDefinition.getDependency();
\r
69 if (MapUtils.isNotEmpty(dictionarySource)) {
\r
70 String source = findFirstSource(dictionarySource);
\r
72 // Populate and Assign First Source
\r
73 if (StringUtils.isNotBlank(source)) {
\r
74 // Set Dictionary Source
\r
75 resourceAssignment.setDictionarySource(source);
\r
77 if (MapUtils.isNotEmpty(dictionaryDependencyMap)) {
\r
79 DictionaryDependency dictionaryDependency = dictionaryDependencyMap.get(source);
\r
80 if (dictionaryDependency != null) {
\r
81 resourceAssignment.setDependencies(dictionaryDependency.getNames());
\r
85 resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_INPUT);
\r
87 log.info("auto map resourceAssignment : {}", resourceAssignment);
\r
92 public static <T> Optional<T> resolve(Supplier<T> resolver) {
\r
94 T result = resolver.get();
\r
95 return Optional.ofNullable(result);
\r
96 } catch (NullPointerException e) {
\r
97 return Optional.empty();
\r
101 private static String findFirstSource(Map<String, JsonNode> dictionarySource) {
\r
102 String source = null;
\r
103 if (MapUtils.isNotEmpty(dictionarySource)) {
\r
104 source = dictionarySource.keySet().stream().findFirst().get();
\r
110 * Overriding ResourceAssignment Properties with properties defined in Dictionary
\r
112 private static void setProperty(ResourceAssignment resourceAssignment, DictionaryDefinition dictionaryDefinition) {
\r
113 if (StringUtils.isNotBlank(dictionaryDefinition.getDataType())) {
\r
114 PropertyDefinition property = resourceAssignment.getProperty();
\r
115 if (property == null) {
\r
116 property = new PropertyDefinition();
\r
118 property.setDefaultValue(dictionaryDefinition.getDefaultValue());
\r
119 property.setType(dictionaryDefinition.getDataType());
\r
120 if (StringUtils.isNotBlank(dictionaryDefinition.getEntrySchema())) {
\r
121 EntrySchema entrySchema = new EntrySchema();
\r
122 entrySchema.setType(dictionaryDefinition.getEntrySchema());
\r
123 property.setEntrySchema(entrySchema);
\r
125 resourceAssignment.setProperty(property);
\r