Controller Blueprints MS
[ccsdk/cds.git] / ms / controllerblueprints / modules / resource-dict / src / main / java / org / onap / ccsdk / apps / controllerblueprints / resource / dict / utils / ResourceDictionaryUtils.java
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.resource.dict.utils;\r
18 \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
30 \r
31 import java.util.Map;\r
32 import java.util.Optional;\r
33 import java.util.function.Supplier;\r
34 \r
35 /**\r
36  * ResourceDictionaryUtils.java Purpose to provide ResourceDictionaryUtils\r
37  *\r
38  * @author Brinda Santh\r
39  * @version 1.0\r
40  */\r
41 public class ResourceDictionaryUtils {\r
42 \r
43     private ResourceDictionaryUtils() {\r
44         // Do nothing\r
45     }\r
46 \r
47     private static final Logger log = LoggerFactory.getLogger(ResourceDictionaryUtils.class);\r
48 \r
49     /**\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
52      *\r
53      * @param resourceAssignment\r
54      * @param dictionaryDefinition\r
55      */\r
56     @SuppressWarnings("squid:S3776")\r
57     public static void populateSourceMapping(ResourceAssignment resourceAssignment,\r
58                                              DictionaryDefinition dictionaryDefinition) {\r
59 \r
60         if (resourceAssignment != null && dictionaryDefinition != null\r
61                 && StringUtils.isBlank(resourceAssignment.getDictionarySource())) {\r
62 \r
63             // Overwrite the Property Definitions from Dictionary\r
64             setProperty(resourceAssignment, dictionaryDefinition);\r
65 \r
66             Map<String, JsonNode> dictionarySource = dictionaryDefinition.getSource();\r
67             Map<String, DictionaryDependency> dictionaryDependencyMap = dictionaryDefinition.getDependency();\r
68 \r
69             if (MapUtils.isNotEmpty(dictionarySource)) {\r
70                 String source = findFirstSource(dictionarySource);\r
71 \r
72                 // Populate and Assign First Source\r
73                 if (StringUtils.isNotBlank(source)) {\r
74                     // Set Dictionary Source\r
75                     resourceAssignment.setDictionarySource(source);\r
76 \r
77                     if (MapUtils.isNotEmpty(dictionaryDependencyMap)) {\r
78                         // Set Dependencies\r
79                         DictionaryDependency dictionaryDependency = dictionaryDependencyMap.get(source);\r
80                         if (dictionaryDependency != null) {\r
81                             resourceAssignment.setDependencies(dictionaryDependency.getNames());\r
82                         }\r
83                     }\r
84                 } else {\r
85                     resourceAssignment.setDictionarySource(ConfigModelConstant.SOURCE_INPUT);\r
86                 }\r
87                 log.info("auto map resourceAssignment : {}", resourceAssignment);\r
88             }\r
89         }\r
90     }\r
91 \r
92     public static <T> Optional<T> resolve(Supplier<T> resolver) {\r
93         try {\r
94             T result = resolver.get();\r
95             return Optional.ofNullable(result);\r
96         } catch (NullPointerException e) {\r
97             return Optional.empty();\r
98         }\r
99     }\r
100 \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
105         }\r
106         return source;\r
107     }\r
108 \r
109     /**\r
110      * Overriding ResourceAssignment Properties with properties defined in Dictionary\r
111      */\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
117             }\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
124             }\r
125             resourceAssignment.setProperty(property);\r
126         }\r
127     }\r
128 \r
129 }\r