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