2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.config.assignment.service;
\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.config.data.adaptor.domain.ResourceAssignmentData;
\r
28 import org.onap.ccsdk.config.model.ConfigModelConstant;
\r
29 import org.onap.ccsdk.config.model.ConfigModelException;
\r
30 import org.onap.ccsdk.config.model.ValidTypes;
\r
31 import org.onap.ccsdk.config.model.data.DataType;
\r
32 import org.onap.ccsdk.config.model.data.EntrySchema;
\r
33 import org.onap.ccsdk.config.model.data.PropertyDefinition;
\r
34 import org.onap.ccsdk.config.model.data.ResourceAssignment;
\r
35 import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;
\r
36 import org.onap.ccsdk.config.model.domain.ResourceDictionary;
\r
37 import org.onap.ccsdk.config.model.utils.JsonUtils;
\r
38 import org.onap.ccsdk.config.model.utils.ResourceAssignmentUtils;
\r
39 import org.onap.ccsdk.config.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
49 public class ConfigAssignmentUtils {
\r
51 private ConfigAssignmentUtils() {
\r
55 private static EELFLogger logger = EELFManager.getInstance().getLogger(ConfigAssignmentUtils.class);
\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
73 * Populate the Field property type for the Data type
\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
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
90 throw new SvcLogicException(String.format("Couldn't get data type (%s) ", dataTypeName));
\r
94 throw new SvcLogicException(String.format("Couldn't get data type (%s) content", dataTypeName));
\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
106 * Populate the Field property type for the Data type
\r
108 public static synchronized PropertyDefinition getPropertyDefinition(SvcLogicContext ctx, String dataTypeName,
\r
109 String propertyName) throws SvcLogicException {
\r
110 PropertyDefinition propertyDefinition = null;
\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
124 throw new SvcLogicException(String.format("couldn't get data type (%s) content.", dataTypeName));
\r
127 } catch (Exception e) {
\r
128 throw new SvcLogicException(e.getMessage());
\r
130 return propertyDefinition;
\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
143 return resourceDefinition;
\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
156 throw new SvcLogicException("service logic context is null.");
\r
159 if (componentContext == null) {
\r
160 throw new SvcLogicException("component context is null.");
\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
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
172 if (resourceAssignment.getProperty().getEntrySchema() != null) {
\r
173 entrySchema = resourceAssignment.getProperty().getEntrySchema().getType();
\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
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
195 arrayNode.add(arrayChildNode);
\r
199 arrayNode = (ArrayNode) responseNode;
\r
201 // Set the List of Complex Values
\r
202 ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, arrayNode);
\r
204 throw new SvcLogicException(
\r
205 String.format("Entry schema is not defined for dictionary (%s) info", dictionaryName));
\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
222 objectNode = (ObjectNode) responseNode;
\r
224 ResourceAssignmentUtils.setResourceDataValue(componentContext, resourceAssignment, objectNode);
\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
247 if (StringUtils.isNotBlank(resourceAssignmentData.getResourceValue())) {
\r
248 if (ValidTypes.getPrimitivePropertType().contains(resourceAssignmentData.getDataType())) {
\r
249 property.setValue(resourceAssignmentData.getResourceValue());
\r
251 JsonNode valueNode =
\r
252 TransformationUtils.getJsonNodeForString(resourceAssignmentData.getResourceValue());
\r
253 property.setValue(valueNode);
\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
261 property.setEntrySchema(null);
\r
263 resourceAssignment.setProperty(property);
\r
264 assignments.add(resourceAssignment);
\r
269 return assignments;
\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
289 if (assignment.getProperty().getValue() != null) {
\r
290 String valueContent = TransformationUtils.getJson(assignment.getProperty().getValue());
\r
291 resourceAssignmentData.setResourceValue(valueContent);
\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
302 return resourceAssignmentDataList;
\r