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