Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / utils / ResourceDictionaryUtils.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \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
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \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
16  */\r
17 \r
18 package org.onap.ccsdk.config.model.utils;\r
19 \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
33 \r
34 /**\r
35  * ResourceDictionaryUtils.java Purpose to provide ResourceDictionaryUtils\r
36  *\r
37  * @version 1.0\r
38  */\r
39 public class ResourceDictionaryUtils {\r
40 \r
41     private ResourceDictionaryUtils() {\r
42         // Do nothing\r
43     }\r
44 \r
45     private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceDictionaryUtils.class);\r
46 \r
47     /**\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
50      *\r
51      * @param resourceAssignment\r
52      * @param resourceDefinition\r
53      */\r
54     @SuppressWarnings("squid:S3776")\r
55     public static void populateSourceMapping(ResourceAssignment resourceAssignment,\r
56             ResourceDefinition resourceDefinition) {\r
57 \r
58         if (resourceAssignment != null && resourceDefinition != null\r
59                 && StringUtils.isBlank(resourceAssignment.getDictionarySource())) {\r
60 \r
61             setProperty(resourceAssignment, resourceDefinition);\r
62             Map<String, SourcesDefinition> sourcesDefinition = resourceDefinition.getSources();\r
63 \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
72                             && CollectionUtils\r
73                                     .isNotEmpty(sourcesDefinition.get("db").getProperties().getDependencies())) {\r
74                         resourceAssignment\r
75                                 .setDependencies(sourcesDefinition.get("db").getProperties().getDependencies());\r
76                     }\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
80                             && CollectionUtils\r
81                                     .isNotEmpty(sourcesDefinition.get("mdsal").getProperties().getDependencies())) {\r
82                         resourceAssignment\r
83                                 .setDependencies(sourcesDefinition.get("mdsal").getProperties().getDependencies());\r
84                     }\r
85                 }\r
86                 logger.info("automapped resourceAssignment : {}", resourceAssignment);\r
87             }\r
88         }\r
89     }\r
90 \r
91     public static <T> Optional<T> resolve(Supplier<T> resolver) {\r
92         try {\r
93             T result = resolver.get();\r
94             return Optional.ofNullable(result);\r
95         } catch (NullPointerException e) {\r
96             return Optional.empty();\r
97         }\r
98     }\r
99 \r
100     /**\r
101      * Overriding ResourceAssignment Properties with properties defined in Dictionary\r
102      */\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
108             }\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
113                 }\r
114                 resourceAssignment.setProperty(property);\r
115             }\r
116         }\r
117     }\r
118 \r
119 }\r