15221ae64b9828b34bbfebd9cec15834059ee015
[ccsdk/features.git] /
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.features.assignment.service;\r
19 \r
20 import java.util.ArrayList;\r
21 import java.util.List;\r
22 import java.util.Map;\r
23 import org.apache.commons.collections.CollectionUtils;\r
24 import org.apache.commons.collections.IteratorUtils;\r
25 import org.apache.commons.collections.MapUtils;\r
26 import org.apache.commons.lang3.StringUtils;\r
27 import org.onap.ccsdk.features.data.adaptor.domain.ResourceAssignmentData;\r
28 import org.onap.ccsdk.features.model.ConfigModelConstant;\r
29 import org.onap.ccsdk.features.model.ConfigModelException;\r
30 import org.onap.ccsdk.features.model.ValidTypes;\r
31 import org.onap.ccsdk.features.model.data.DataType;\r
32 import org.onap.ccsdk.features.model.data.EntrySchema;\r
33 import org.onap.ccsdk.features.model.data.PropertyDefinition;\r
34 import org.onap.ccsdk.features.model.data.ResourceAssignment;\r
35 import org.onap.ccsdk.features.model.data.dict.ResourceDefinition;\r
36 import org.onap.ccsdk.features.model.domain.ResourceDictionary;\r
37 import org.onap.ccsdk.features.model.utils.JsonUtils;\r
38 import org.onap.ccsdk.features.model.utils.ResourceAssignmentUtils;\r
39 import org.onap.ccsdk.features.model.utils.TransformationUtils;\r
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;\r
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;\r
42 import com.att.eelf.configuration.EELFLogger;\r
43 import com.att.eelf.configuration.EELFManager;\r
44 import com.fasterxml.jackson.databind.JsonNode;\r
45 import com.fasterxml.jackson.databind.node.ArrayNode;\r
46 import com.fasterxml.jackson.databind.node.JsonNodeFactory;\r
47 import com.fasterxml.jackson.databind.node.ObjectNode;\r
48 \r
49 public class ConfigAssignmentUtils {\r
50 \r
51     private ConfigAssignmentUtils() {\r
52 \r
53     }\r
54 \r
55     private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentUtils.class);\r
56 \r
57     public static synchronized Object getContextKeyValue(SvcLogicContext context, String key) {\r
58         Object value = null;\r
59         if (context != null && key != null && context.getAttributeKeySet().contains(key)) {\r
60             String strValue = context.getAttribute(key);\r
61             if (StringUtils.isNotBlank(strValue)) {\r
62                 value = strValue;\r
63             }\r
64         }\r
65         return value;\r
66     }\r
67 \r
68     /*\r
69      * Populate the Field property type for the Data type\r
70      */\r
71     public static synchronized String getPropertyType(SvcLogicContext ctx, String dataTypeName, String propertyName)\r
72             throws SvcLogicException {\r
73         String type = ValidTypes.DATA_TYPE_STRING;\r
74         try {\r
75             if (ctx != null && StringUtils.isNotBlank(dataTypeName) && StringUtils.isNotBlank(propertyName)) {\r
76                 String dataTypeContent = ctx.getAttribute(ConfigModelConstant.PROPERTY_DATA_TYPES_DOT + dataTypeName);\r
77                 if (StringUtils.isNotBlank(dataTypeContent)) {\r
78                     DataType dataType = TransformationUtils.readValue(dataTypeContent, DataType.class);\r
79                     if (dataType != null && dataType.getProperties() != null\r
80                             && dataType.getProperties().containsKey(propertyName)) {\r
81                         PropertyDefinition propertyDefinition = dataType.getProperties().get(propertyName);\r
82                         if (StringUtils.isNotBlank(propertyDefinition.getType())) {\r
83                             type = propertyDefinition.getType();\r
84                             logger.trace("Data type({})'s property ({}) is ({})", dataTypeName, propertyName, type);\r
85                         } else {\r
86                             throw new SvcLogicException(String.format("Couldn't get data type (%s) ", dataTypeName));\r
87                         }\r
88                     }\r
89                 } else {\r
90                     throw new SvcLogicException(String.format("Couldn't get data type (%s) content", dataTypeName));\r
91                 }\r
92             }\r
93         } catch (Exception e) {\r
94             logger.error("couldn't get data type({})'s property ({}), type ({}), error message ({}).", dataTypeName,\r
95                     propertyName, type, e.getMessage());\r
96             throw new SvcLogicException(e.getMessage());\r
97         }\r
98         return type;\r
99     }\r
100 \r
101     /*\r
102      * Populate the Field property type for the Data type\r
103      */\r
104     public static synchronized PropertyDefinition getPropertyDefinition(SvcLogicContext ctx, String dataTypeName,\r
105             String propertyName) throws SvcLogicException {\r
106         PropertyDefinition propertyDefinition = null;\r
107         try {\r
108             if (ctx != null && StringUtils.isNotBlank(dataTypeName) && StringUtils.isNotBlank(propertyName)) {\r
109                 String dataTypeContent = ctx.getAttribute(ConfigModelConstant.PROPERTY_DATA_TYPES_DOT + dataTypeName);\r
110                 if (StringUtils.isNotBlank(dataTypeContent)) {\r
111                     DataType dataType = TransformationUtils.readValue(dataTypeContent, DataType.class);\r
112                     if (dataType != null && dataType.getProperties() != null\r
113                             && dataType.getProperties().containsKey(propertyName)) {\r
114                         propertyDefinition = dataType.getProperties().get(propertyName);\r
115                         if (propertyDefinition == null) {\r
116                             throw new SvcLogicException(String.format("couldn't get data type (%s) ", dataTypeName));\r
117                         }\r
118                     }\r
119                 } else {\r
120                     throw new SvcLogicException(String.format("couldn't get data type (%s) content.", dataTypeName));\r
121                 }\r
122             }\r
123         } catch (Exception e) {\r
124             throw new SvcLogicException(e.getMessage());\r
125         }\r
126         return propertyDefinition;\r
127     }\r
128 \r
129     public static synchronized ResourceDefinition getDictionaryDefinition(Map<String, ResourceDictionary> dictionaries,\r
130             String dictionaryName) {\r
131         ResourceDefinition resourceDefinition = null;\r
132         if (dictionaries != null && StringUtils.isNotBlank(dictionaryName)) {\r
133             ResourceDictionary resourceDictionary = dictionaries.get(dictionaryName);\r
134             if (resourceDictionary != null && resourceDictionary.getDefinition() != null) {\r
135                 resourceDefinition =\r
136                         TransformationUtils.treeToValue(resourceDictionary.getDefinition(), ResourceDefinition.class);\r
137             }\r
138         }\r
139         return resourceDefinition;\r
140     }\r
141 \r
142     @SuppressWarnings("squid:S3776")\r
143     public static synchronized void populateValueForOutputMapping(SvcLogicContext ctx,\r
144             Map<String, Object> componentContext, ResourceAssignment resourceAssignment,\r
145             Map<String, String> outputKeyMapping, JsonNode responseNode)\r
146             throws ConfigModelException, SvcLogicException {\r
147         if (resourceAssignment == null) {\r
148             throw new SvcLogicException("resourceAssignment is null.");\r
149         }\r
150 \r
151         if (ctx == null) {\r
152             throw new SvcLogicException("service logic context is null.");\r
153         }\r
154 \r
155         if (componentContext == null) {\r
156             throw new SvcLogicException("component context is null.");\r
157         }\r
158 \r
159         logger.info("populating value for output mapping ({}), from json ({})", outputKeyMapping, responseNode);\r
160         String dictionaryName = resourceAssignment.getDictionaryName();\r
161         String type = resourceAssignment.getProperty().getType();\r
162 \r
163         String entrySchema = null;\r
164         if (ValidTypes.getPrimitivePropertType().contains(type)) {\r
165             ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, responseNode);\r
166         } else if (ValidTypes.getListPropertType().contains(type)) {\r
167             // Array Types\r
168             if (resourceAssignment.getProperty().getEntrySchema() != null) {\r
169                 entrySchema = resourceAssignment.getProperty().getEntrySchema().getType();\r
170             }\r
171 \r
172             if (StringUtils.isNotBlank(entrySchema)) {\r
173                 ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();\r
174                 if (ValidTypes.getPrimitivePropertType().contains(entrySchema)) {\r
175                     arrayNode = (ArrayNode) responseNode;\r
176                 } else if (MapUtils.isNotEmpty(outputKeyMapping)) {\r
177                     List<JsonNode> responseArrayNode = IteratorUtils.toList(responseNode.elements());\r
178                     for (JsonNode responseSingleJsonNode : responseArrayNode) {\r
179                         if (responseSingleJsonNode != null) {\r
180                             ObjectNode arrayChildNode = JsonNodeFactory.instance.objectNode();\r
181                             for (Map.Entry<String, String> mapping : outputKeyMapping.entrySet()) {\r
182                                 JsonNode responseKeyValue = responseSingleJsonNode.get(mapping.getKey());\r
183 \r
184                                 String propertyTypeForDataType =\r
185                                         ConfigAssignmentUtils.getPropertyType(ctx, entrySchema, mapping.getKey());\r
186                                 logger.info("For List Type Resource: key ({}), value ({}), type  ({})",\r
187                                         mapping.getKey(), responseKeyValue, propertyTypeForDataType);\r
188                                 JsonUtils.populateJsonNodeValues(mapping.getValue(), responseKeyValue,\r
189                                         propertyTypeForDataType, arrayChildNode);\r
190                             }\r
191                             arrayNode.add(arrayChildNode);\r
192                         }\r
193                     }\r
194                 } else {\r
195                     arrayNode = (ArrayNode) responseNode;\r
196                 }\r
197                 // Set the List of Complex Values\r
198                 ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, arrayNode);\r
199             } else {\r
200                 throw new SvcLogicException(\r
201                         String.format("Entry schema is not defined for dictionary (%s) info", dictionaryName));\r
202             }\r
203         } else {\r
204             // Complex Types\r
205             ObjectNode objectNode = null;\r
206             if (MapUtils.isNotEmpty(outputKeyMapping)) {\r
207                 objectNode = JsonNodeFactory.instance.objectNode();\r
208                 for (Map.Entry<String, String> mapping : outputKeyMapping.entrySet()) {\r
209                     JsonNode responseKeyValue = responseNode.get(mapping.getKey());\r
210                     String propertyTypeForDataType =\r
211                             ConfigAssignmentUtils.getPropertyType(ctx, entrySchema, mapping.getKey());\r
212                     logger.info("For Complex Type Resource: key ({}), value ({}), type  ({})", mapping.getKey(),\r
213                             responseKeyValue, propertyTypeForDataType);\r
214                     JsonUtils.populateJsonNodeValues(mapping.getValue(), responseKeyValue, propertyTypeForDataType,\r
215                             objectNode);\r
216                 }\r
217             } else {\r
218                 objectNode = (ObjectNode) responseNode;\r
219             }\r
220             ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, objectNode);\r
221         }\r
222     }\r
223 \r
224     @SuppressWarnings("squid:S3776")\r
225     public static synchronized List<ResourceAssignment> convertResoureAssignmentDataList(\r
226             List<ResourceAssignmentData> resourceAssignmentDataList) {\r
227         List<ResourceAssignment> assignments = new ArrayList<>();\r
228         if (CollectionUtils.isNotEmpty(resourceAssignmentDataList)) {\r
229             for (ResourceAssignmentData resourceAssignmentData : resourceAssignmentDataList) {\r
230                 if (resourceAssignmentData != null) {\r
231                     ResourceAssignment resourceAssignment = new ResourceAssignment();\r
232                     resourceAssignment.setName(resourceAssignmentData.getTemplateKeyName());\r
233                     resourceAssignment.setVersion(resourceAssignmentData.getVersion());\r
234                     resourceAssignment.setUpdatedBy(resourceAssignmentData.getUpdatedBy());\r
235                     resourceAssignment.setUpdatedDate(resourceAssignmentData.getUpdatedDate());\r
236                     resourceAssignment.setDictionaryName(resourceAssignmentData.getResourceName());\r
237                     resourceAssignment.setDictionarySource(resourceAssignmentData.getSource());\r
238                     resourceAssignment.setStatus(resourceAssignmentData.getStatus());\r
239                     resourceAssignment.setMessage(resourceAssignmentData.getMessage());\r
240                     PropertyDefinition property = new PropertyDefinition();\r
241                     property.setType(resourceAssignmentData.getDataType());\r
242 \r
243                     if (StringUtils.isNotBlank(resourceAssignmentData.getResourceValue())) {\r
244                         if (ValidTypes.getPrimitivePropertType().contains(resourceAssignmentData.getDataType())) {\r
245                             property.setValue(resourceAssignmentData.getResourceValue());\r
246                         } else {\r
247                             JsonNode valueNode =\r
248                                     TransformationUtils.getJsonNodeForString(resourceAssignmentData.getResourceValue());\r
249                             property.setValue(valueNode);\r
250                         }\r
251                     }\r
252                     if (StringUtils.isNotBlank(resourceAssignmentData.getEntrySchema())) {\r
253                         EntrySchema entrySchema = new EntrySchema();\r
254                         entrySchema.setType(resourceAssignmentData.getEntrySchema());\r
255                         property.setEntrySchema(entrySchema);\r
256                     } else {\r
257                         property.setEntrySchema(null);\r
258                     }\r
259                     resourceAssignment.setProperty(property);\r
260                     assignments.add(resourceAssignment);\r
261                 }\r
262             }\r
263 \r
264         }\r
265         return assignments;\r
266     }\r
267 \r
268     @SuppressWarnings("squid:S3776")\r
269     public static synchronized List<ResourceAssignmentData> convertResoureAssignmentList(\r
270             List<ResourceAssignment> assignments) {\r
271         List<ResourceAssignmentData> resourceAssignmentDataList = new ArrayList<>();\r
272         if (CollectionUtils.isNotEmpty(assignments)) {\r
273             for (ResourceAssignment assignment : assignments) {\r
274                 if (assignment != null) {\r
275                     ResourceAssignmentData resourceAssignmentData = new ResourceAssignmentData();\r
276                     resourceAssignmentData.setTemplateKeyName(assignment.getName());\r
277                     resourceAssignmentData.setVersion(assignment.getVersion());\r
278                     resourceAssignmentData.setUpdatedBy(assignment.getUpdatedBy());\r
279                     resourceAssignmentData.setUpdatedDate(assignment.getUpdatedDate());\r
280                     if (assignment.getProperty() != null) {\r
281                         resourceAssignmentData.setDataType(assignment.getProperty().getType());\r
282                         if (assignment.getProperty().getEntrySchema() != null) {\r
283                             resourceAssignmentData.setEntrySchema(assignment.getProperty().getEntrySchema().getType());\r
284                         }\r
285                         if (assignment.getProperty().getValue() != null) {\r
286                             String valueContent = TransformationUtils.getJson(assignment.getProperty().getValue());\r
287                             resourceAssignmentData.setResourceValue(valueContent);\r
288                         }\r
289                     }\r
290                     resourceAssignmentData.setResourceName(assignment.getDictionaryName());\r
291                     resourceAssignmentData.setSource(assignment.getDictionarySource());\r
292                     resourceAssignmentData.setStatus(assignment.getStatus());\r
293                     resourceAssignmentData.setMessage(assignment.getMessage());\r
294                     resourceAssignmentDataList.add(resourceAssignmentData);\r
295                 }\r
296             }\r
297         }\r
298         return resourceAssignmentDataList;\r
299     }\r
300 \r
301 }\r