2 * Copyright © 2017-2018 AT&T Intellectual Property.
\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
7 * http://www.apache.org/licenses/LICENSE-2.0
\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
15 package org.onap.ccsdk.config.assignment.service;
\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
46 public class ConfigAssignmentUtils {
\r
48 private ConfigAssignmentUtils() {
\r
52 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentUtils.class);
\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
70 * Populate the Field property type for the Data type
\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
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
87 throw new SvcLogicException(String.format("Couldn't get data type (%s) ", dataTypeName));
\r
91 throw new SvcLogicException(String.format("Couldn't get data type (%s) content", dataTypeName));
\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
103 * Populate the Field property type for the Data type
\r
105 public static synchronized PropertyDefinition getPropertyDefinition(SvcLogicContext ctx, String dataTypeName,
\r
106 String propertyName) throws SvcLogicException {
\r
107 PropertyDefinition propertyDefinition = null;
\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
121 throw new SvcLogicException(String.format("couldn't get data type (%s) content.", dataTypeName));
\r
124 } catch (Exception e) {
\r
125 throw new SvcLogicException(e.getMessage());
\r
127 return propertyDefinition;
\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
140 return resourceDefinition;
\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
153 throw new SvcLogicException("service logic context is null.");
\r
156 if (componentContext == null) {
\r
157 throw new SvcLogicException("component context is null.");
\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
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
169 if (resourceAssignment.getProperty().getEntrySchema() != null) {
\r
170 entrySchema = resourceAssignment.getProperty().getEntrySchema().getType();
\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
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
192 arrayNode.add(arrayChildNode);
\r
196 arrayNode = (ArrayNode) responseNode;
\r
198 // Set the List of Complex Values
\r
199 ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, arrayNode);
\r
201 throw new SvcLogicException(
\r
202 String.format("Entry schema is not defined for dictionary (%s) info", dictionaryName));
\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
219 objectNode = (ObjectNode) responseNode;
\r
221 ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, objectNode);
\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
244 if (StringUtils.isNotBlank(resourceAssignmentData.getResourceValue())) {
\r
245 if (ValidTypes.getPrimitivePropertType().contains(resourceAssignmentData.getDataType())) {
\r
246 property.setValue(resourceAssignmentData.getResourceValue());
\r
248 JsonNode valueNode =
\r
249 TransformationUtils.getJsonNodeForString(resourceAssignmentData.getResourceValue());
\r
250 property.setValue(valueNode);
\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
258 property.setEntrySchema(null);
\r
260 resourceAssignment.setProperty(property);
\r
261 assignments.add(resourceAssignment);
\r
266 return assignments;
\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
286 if (assignment.getProperty().getValue() != null) {
\r
287 String valueContent = TransformationUtils.getJson(assignment.getProperty().getValue());
\r
288 resourceAssignmentData.setResourceValue(valueContent);
\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
299 return resourceAssignmentDataList;
\r