summaryrefslogtreecommitdiffstats
path: root/blueprints-processor/plugin/model-provider/src/main/java/org/onap/ccsdk/config/model/utils/ResourceAssignmentUtils.java
blob: 3cdf95c869677ad56d92a25b66797dd8dfdabb65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.config.model.utils;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.onap.ccsdk.config.model.ConfigModelConstant;
import org.onap.ccsdk.config.model.ConfigModelException;
import org.onap.ccsdk.config.model.ValidTypes;
import org.onap.ccsdk.config.model.data.ResourceAssignment;
import org.onap.ccsdk.config.model.data.dict.ResourceDefinition;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.base.Preconditions;

public class ResourceAssignmentUtils {

    private ResourceAssignmentUtils() {

    }

    private static EELFLogger logger = EELFManager.getInstance().getLogger(ResourceAssignmentUtils.class);

    public static String getArtifactNodeContent(String nodeTemplateName, Map<String, Object> context) {
        Preconditions.checkArgument(StringUtils.isNotBlank(nodeTemplateName),
                "getArtifactNodeContent missing template name");
        return (String) context.get(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + nodeTemplateName + ".content");
    }

    public static List<ResourceAssignment> getArtifactNodeMapping(String nodeTemplateName,
            Map<String, Object> context) {
        Preconditions.checkArgument(StringUtils.isNotBlank(nodeTemplateName),
                "getArtifactNodeMapping missing template name");
        List<ResourceAssignment> resourceAssignments = null;
        String resourceMappingContent =
                (String) context.get(ConfigModelConstant.PROPERTY_NODE_TEMPLATES_DOT + nodeTemplateName + ".mapping");
        if (StringUtils.isNotBlank(resourceMappingContent)) {
            resourceAssignments = TransformationUtils.getListfromJson(resourceMappingContent, ResourceAssignment.class);

        } else {
            logger.warn("missing mapping content for node template ({})", nodeTemplateName);
        }
        return resourceAssignments;
    }

    // Not used Any whre
    public static synchronized void cleanContextTemplateNDictionaryKeys(Map<String, Object> componentContext) {
        String recipeName = (String) componentContext.get(ConfigModelConstant.PROPERTY_ACTION_NAME);
        Set<String> removeSet = new HashSet<>();
        componentContext.forEach((key, value) -> {
            if (StringUtils.isNotBlank(key)
                    && (key.startsWith(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + ".")
                            || key.startsWith(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + recipeName + "."))) {
                removeSet.add(key);
            }
        });
        componentContext.keySet().removeAll(removeSet);
    }

    public static synchronized Object getDictionaryKeyValue(Map<String, Object> componentContext,
            ResourceAssignment resourceMapping) {
        Object value = null;
        if (resourceMapping != null && componentContext != null) {
            String recipeName = (String) componentContext.get(ConfigModelConstant.PROPERTY_ACTION_NAME);
            String dictionaryKeyName = resourceMapping.getDictionaryName();
            value = componentContext
                    .get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + "." + dictionaryKeyName);
        }
        return value;
    }

    public static synchronized Object getDictionaryKeyValue(Map<String, Object> componentContext,
            ResourceDefinition resourceDictionary) {
        Object value = null;
        if (resourceDictionary != null && componentContext != null) {
            String recipeName = (String) componentContext.get(ConfigModelConstant.PROPERTY_ACTION_NAME);
            String dictionaryKeyName = resourceDictionary.getName();
            value = componentContext
                    .get(ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + "." + dictionaryKeyName);
        }
        return value;
    }

    public static synchronized Object getTemplateKeyValue(Map<String, Object> componentContext,
            ResourceAssignment resourceMapping) {
        Object value = null;
        if (resourceMapping != null && componentContext != null) {
            String recipeName = (String) componentContext.get(ConfigModelConstant.PROPERTY_ACTION_NAME);
            String templateKeyName = resourceMapping.getName();
            value = componentContext
                    .get(ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + recipeName + "." + templateKeyName);
        }
        return value;
    }

    public static synchronized void setResourceDataValue(Map<String, Object> componentContext,
            ResourceAssignment resourceAssignment, Object value) throws ConfigModelException {

        if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getName())
                && resourceAssignment.getProperty() != null) {

            String recipeName = (String) componentContext.get(ConfigModelConstant.PROPERTY_ACTION_NAME);
            String templateKeyName = resourceAssignment.getName();
            String dictionaryKeyName = resourceAssignment.getDictionaryName();

            if (StringUtils.isBlank(dictionaryKeyName)) {
                resourceAssignment.setDictionaryName(templateKeyName);
                dictionaryKeyName = templateKeyName;
                logger.warn("Missing dictionary key, setting with template key ({}) as dictionary key ({})",
                        templateKeyName, dictionaryKeyName);
            }
            String type = resourceAssignment.getProperty().getType();
            try {
                if (StringUtils.isNotBlank(type)) {
                    Object convertedValue = convertResourceValue(type, value);

                    componentContext.put(
                            ConfigModelConstant.PROPERTY_DICTIONARY_KEY_DOT + recipeName + "." + dictionaryKeyName,
                            convertedValue);
                    componentContext.put(
                            ConfigModelConstant.PROPERTY_RECIPE_KEY_DOT + recipeName + "." + templateKeyName,
                            convertedValue);

                    logger.trace("Setting Resource Value ({}) for Resource Name ({}) of type ({}) ", convertedValue,
                            dictionaryKeyName, type);

                    resourceAssignment.getProperty().setValue(convertedValue);
                    resourceAssignment.setUpdatedDate(new Date());
                    resourceAssignment.setUpdatedBy(ConfigModelConstant.USER_SYSTEM);
                    resourceAssignment.setStatus(ConfigModelConstant.STATUS_SUCCESS);
                }
            } catch (Exception e) {
                throw new ConfigModelException(String.format(
                        "Failed in setting value for template key (%s) and dictionary key (%s) of type (%s) with error message (%s)",
                        templateKeyName, dictionaryKeyName, type, e.getMessage()));
            }
        } else {
            throw new ConfigModelException(
                    String.format("Failed in setting resource value for resource mapping (%s)", resourceAssignment));
        }
    }

    private static Object convertResourceValue(String type, Object value) {
        Object convertedValue = null;

        if (value == null || value instanceof NullNode) {
            logger.info("Returning {} value from convertResourceValue", value);
            return null;
        }

        if (ValidTypes.getPrimitivePropertType().contains(type)) {
            convertedValue = convertPrimitiveResourceValue(type, value);
        } else {
            // Case where Resource is non-primitive type
            if (value instanceof JsonNode || value instanceof ObjectNode || value instanceof ArrayNode) {
                convertedValue = value;
            } else if (value instanceof String) {
                convertedValue = TransformationUtils.getJsonNodeForString((String) value);
            } else {
                convertedValue = TransformationUtils.getJsonNodeForObject(value);
            }
        }
        return convertedValue;
    }

    @SuppressWarnings("squid:S3776")
    private static Object convertPrimitiveResourceValue(String type, Object value) {
        Object convertedValue = value;

        if (value instanceof ArrayNode) {
            if (ValidTypes.DATA_TYPE_BOOLEAN.equalsIgnoreCase(type)) {
                convertedValue = ((ArrayNode) value).asBoolean();
            } else if (ValidTypes.DATA_TYPE_INTEGER.equalsIgnoreCase(type)) {
                convertedValue = ((ArrayNode) value).asInt();
            } else if (ValidTypes.DATA_TYPE_FLOAT.equalsIgnoreCase(type)) {
                convertedValue = Float.valueOf(((ArrayNode) value).toString());
            } else {
                convertedValue = ((ArrayNode) value).toString();
            }
        } else if (value instanceof JsonNode) {
            if (ValidTypes.DATA_TYPE_BOOLEAN.equalsIgnoreCase(type)) {
                convertedValue = ((JsonNode) value).asBoolean();
            } else if (ValidTypes.DATA_TYPE_INTEGER.equalsIgnoreCase(type)) {
                convertedValue = ((JsonNode) value).asInt();
            } else if (ValidTypes.DATA_TYPE_FLOAT.equalsIgnoreCase(type)) {
                convertedValue = Float.valueOf(((JsonNode) value).asText());
            } else {
                convertedValue = ((JsonNode) value).asText();
            }
        } else if (value instanceof String) {
            if (ValidTypes.DATA_TYPE_BOOLEAN.equalsIgnoreCase(type)) {
                convertedValue = Boolean.valueOf((String) value);
            } else if (ValidTypes.DATA_TYPE_INTEGER.equalsIgnoreCase(type)) {
                convertedValue = Integer.valueOf((String) value);
            } else if (ValidTypes.DATA_TYPE_FLOAT.equalsIgnoreCase(type)) {
                convertedValue = Float.valueOf((String) value);
            }
        }
        logger.info("Returning value ({}) from convertPrimitiveResourceValue", convertedValue);
        return convertedValue;
    }

    @SuppressWarnings("squid:S1172")
    public static synchronized void setFailedResourceDataValue(Map<String, Object> componentContext,
            ResourceAssignment resourceAssignment, String message) {
        setFailedResourceDataValue(resourceAssignment, message);
    }

    public static synchronized void setFailedResourceDataValue(ResourceAssignment resourceAssignment, String message) {
        if (resourceAssignment != null && StringUtils.isNotBlank(resourceAssignment.getName())
                && resourceAssignment.getProperty() != null) {
            resourceAssignment.setUpdatedDate(new Date());
            resourceAssignment.setStatus(ConfigModelConstant.STATUS_FAILURE);
            resourceAssignment.setUpdatedBy(ConfigModelConstant.USER_SYSTEM);
            resourceAssignment.setMessage(message);
        }
    }

    public static synchronized void assertTemplateKeyValueNotNull(Map<String, Object> componentContext,
            ResourceAssignment resourceAssignment) throws ConfigModelException {
        if (resourceAssignment != null && resourceAssignment.getProperty() != null
                && BooleanUtils.isTrue(resourceAssignment.getProperty().getRequired())
                && getTemplateKeyValue(componentContext, resourceAssignment) == null) {
            logger.error("failed to populate mandatory resource mapping ({})", resourceAssignment);
            throw new ConfigModelException(
                    String.format("failed to populate mandatory resource mapping (%s)", resourceAssignment));
        }
    }

    @SuppressWarnings({"squid:S3776", "squid:S1141"})
    public static synchronized String generateResourceDataForAssignments(List<ResourceAssignment> assignments)
            throws ConfigModelException {
        String result = "{}";
        try {
            ObjectMapper mapper = new ObjectMapper();
            JsonNode root = mapper.readTree(result);
            for (ResourceAssignment resourceMapping : assignments) {
                if (resourceMapping != null && resourceMapping.getName() != null
                        && resourceMapping.getProperty() != null) {

                    String type = resourceMapping.getProperty().getType();
                    Object value = resourceMapping.getProperty().getValue();
                    logger.info("Generating Resource name ({}), type ({}), value ({})", resourceMapping.getName(), type,
                            value);
                    if (value == null) {
                        ((ObjectNode) root).set(resourceMapping.getName(), null);
                    } else if (value instanceof JsonNode) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (JsonNode) value);
                    } else if (ValidTypes.DATA_TYPE_STRING.equalsIgnoreCase(type)) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (String) value);
                    } else if (ValidTypes.DATA_TYPE_BOOLEAN.equalsIgnoreCase(type)) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (Boolean) value);
                    } else if (ValidTypes.DATA_TYPE_INTEGER.equalsIgnoreCase(type)) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (Integer) value);
                    } else if (ValidTypes.DATA_TYPE_FLOAT.equalsIgnoreCase(type)) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (Float) value);
                    } else if (ValidTypes.DATA_TYPE_TIMESTAMP.equalsIgnoreCase(type)) {
                        ((ObjectNode) root).put(resourceMapping.getName(), (String) value);
                    } else {
                        JsonNode jsonNode = TransformationUtils.getJsonNodeForObject(value);
                        if (jsonNode != null) {
                            ((ObjectNode) root).put(resourceMapping.getName(), jsonNode);
                        } else {
                            ((ObjectNode) root).set(resourceMapping.getName(), null);
                        }
                    }
                }
            }
            result = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
            logger.info("Generated Resource Param Data ({})", result);
        } catch (Exception e) {
            throw new ConfigModelException(e.getMessage(), e);
        }
        return result;
    }

}