aboutsummaryrefslogtreecommitdiffstats
path: root/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicContext.java
blob: 98cf7e05ec1ef3a637e087dd7c56fa318e05976a (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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*-
 * ============LICENSE_START=======================================================
 * ONAP : CCSDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 * 						reserved.
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */

package org.onap.ccsdk.sli.core.sli;

import java.util.*;

import com.google.gson.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


public class SvcLogicContext {

    private static final Logger LOG = LoggerFactory.getLogger(SvcLogicContext.class);
    private final SecurePrinter securePrinter = new SecurePrinter();
    public static final String CTX_NULL_VALUE="";
    private static final String LENGTH="_length";

    private HashMap<String, String> attributes;

    private String status = SvcLogicConstants.SUCCESS;

    public SvcLogicContext() {
        this.attributes = new HashMap<>();

    }

    public SvcLogicContext(Properties props) {
        this.attributes = new HashMap<>();

        if (props.containsKey(CommonConstants.SERVICE_LOGIC_STATUS)) {
            this.status = props.getProperty(CommonConstants.SERVICE_LOGIC_STATUS);
        }

        for (Object nameObj : props.keySet()) {
            String propName = (String) nameObj;
            attributes.put(propName, props.getProperty(propName));
        }
    }

    public String getAttribute(String name) {
        if (attributes.containsKey(name)) {
            return attributes.get(name);
        } else {
            return null;
        }
    }

    public void setAttribute(String name, String value) {
        if (value == null) {
            if (attributes.containsKey(name)) {
                attributes.remove(name);
            }
        } else {
            attributes.put(name, value);
        }
    }

    public Set<String> getAttributeKeySet() {
        return attributes.keySet();
    }

    public Boolean isSuccess() {
        return status.equals(SvcLogicConstants.SUCCESS);
    }

    @Deprecated
    public String getStatus() {
        return status;
    }

    @Deprecated
    public void setStatus(String status) {
        this.status = status;
    }

    public void markFailed() {
        this.status = SvcLogicConstants.FAILURE;
    }

    public void markSuccess() {
        this.status = SvcLogicConstants.SUCCESS;
    }

    public Properties toProperties() {
        Properties props = new Properties();

        if (status != null) {
            props.setProperty(CommonConstants.SERVICE_LOGIC_STATUS, status);
        }

        String attrName;
        String attrVal;
        for (Map.Entry<String, String> entry : attributes.entrySet()) {
            attrName = entry.getKey();
            attrVal = entry.getValue();
            if (attrVal == null) {
                LOG.warn("attribute {} value is null - setting to empty string", attrName);
                props.setProperty(attrName, "");
            } else {
                props.setProperty(attrName, attrVal);
            }
        }

        return props;
    }

    public void mergeDocument(String pfx, Document doc) {
        String prefix = "";

        if (pfx != null) {
            prefix = pfx;
        }

        Element root = doc.getDocumentElement();

        mergeElement(prefix, root, null);
    }

    public void mergeElement(String pfx, Element element, Map<String, Integer> nodeMap) {

        // In XML, cannot tell the difference between containers and lists.
        // So, have to treat each element as both (ugly but necessary).
        // We do this by passing a nodeMap to be used to count instance of each tag,
        // which will be used to set _length and to set index

        LOG.trace("mergeElement({},{},{})", pfx, element.getTagName(), nodeMap);

        String curTagName = element.getTagName();
        String prefix = curTagName;

        if (pfx != null) {
            prefix = pfx + "." + prefix;
        }

        int myIdx = 0;

        if (nodeMap != null) {
            if (nodeMap.containsKey(curTagName)) {
                myIdx = nodeMap.get(curTagName);
            }

            nodeMap.put(curTagName, myIdx + 1);
            this.setAttribute(prefix + "_length", Integer.toString(myIdx + 1));
        }

        NodeList children = element.getChildNodes();

        int numChildren = children.getLength();

        Map<String, Integer> childMap = new HashMap<>();
        Map<String, Integer> idxChildMap = new HashMap<>();

        for (int i = 0; i < numChildren; i++) {
            Node curNode = children.item(i);

            if (curNode instanceof Text) {
                Text curText = (Text) curNode;
                String curTextValue = curText.getTextContent();
                LOG.trace("Setting ctx variable {} = {}", prefix, curTextValue);
                this.setAttribute(prefix, curText.getTextContent());


            } else if (curNode instanceof Element) {
                mergeElement(prefix, (Element) curNode, childMap);
                if (nodeMap != null) {

                    mergeElement(prefix + "[" + myIdx + "]", (Element) curNode, idxChildMap);

                }
            }
        }

    }

    public void mergeJson(String pfx, String jsonString) {

        JsonParser jp = new JsonParser();
        JsonElement element = jp.parse(jsonString);
        String root = "";
        if ((pfx != null) && (pfx.length() > 0)) {
            root = pfx + ".";
        }
        if (element.isJsonObject()) {
            writeJsonObject(element.getAsJsonObject(), root);
        } else if (element.isJsonArray()) {
            handleJsonArray("", element.getAsJsonArray(), root);
        }
    }


    protected void writeJsonObject(JsonObject obj, String root) {
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            if (entry.getValue().isJsonObject()) {
                writeJsonObject(entry.getValue().getAsJsonObject(), root + key + ".");
            } else if (entry.getValue().isJsonArray()) {
                JsonArray array = entry.getValue().getAsJsonArray();
                handleJsonArray(key, array, root);
            } else {
                //Handles when a JSON obj is nested within a JSON obj
                if(!root.endsWith(".")){
                    root = root + ".";
                }
                if(entry.getValue().isJsonNull()) {
                    this.setAttribute(root + key, CTX_NULL_VALUE);
                }else {
                    this.setAttribute(root + key, entry.getValue().getAsString());
                }
            }
        }
    }

    protected void handleJsonArray(String key, JsonArray array,  String root) {
        this.setAttribute(root + key + LENGTH, String.valueOf(array.size()));
        Integer arrayIdx = 0;
        for (JsonElement element : array) {
            String prefix = root + key + "[" + arrayIdx + "]";

            if (element.isJsonArray()) {
                handleJsonArray(key, element.getAsJsonArray(), prefix);
            } else if (element.isJsonObject()) {
                writeJsonObject(element.getAsJsonObject(),  prefix + ".");
            } else if (element.isJsonNull()) {
                this.setAttribute(prefix, CTX_NULL_VALUE);
            } else if (element.isJsonPrimitive()) {
                this.setAttribute(prefix, element.getAsString());
            }
            arrayIdx++;
        }
    }


    public String resolve(String ctxVarName) {

        if (ctxVarName.indexOf('[') == -1) {
            // Ctx variable contains no arrays
            return getAttribute(ctxVarName);
        }

        // Resolve any array references
        StringBuilder sbuff = new StringBuilder();
        String[] ctxVarParts = ctxVarName.split("\\[");
        sbuff.append(ctxVarParts[0]);
        for (int i = 1; i < ctxVarParts.length; i++) {
            if (ctxVarParts[i].startsWith("$")) {
                int endBracketLoc = ctxVarParts[i].indexOf(']');
                if (endBracketLoc == -1) {
                    // Missing end bracket ... give up parsing
                    LOG.warn("Variable reference {} seems to be missing a ']'", ctxVarName);
                    return getAttribute(ctxVarName);
                }

                String idxVarName = ctxVarParts[i].substring(1, endBracketLoc);
                String remainder = ctxVarParts[i].substring(endBracketLoc);

                sbuff.append("[");
                sbuff.append(this.getAttribute(idxVarName));
                sbuff.append(remainder);

            } else {
                // Index is not a variable reference
                sbuff.append("[");
                sbuff.append(ctxVarParts[i]);
            }
        }

        return getAttribute(sbuff.toString());
    }

    public String toJsonString(String pfx) {
        JsonParser jp = new JsonParser();

        String jsonString = this.toJsonString();
        JsonObject jsonRoot = (JsonObject) jp.parse(jsonString);
        JsonObject targetJson = jsonRoot.getAsJsonObject(pfx);
        if (targetJson == null) {
            return("");
        } else {
            return(targetJson.toString());
        }
    }

    public String toJsonString() {
        JsonObject root = new JsonObject();
        JsonElement lastJsonObject = root;
        JsonElement currJsonLeaf = root;

        String attrName = null;
        String attrVal = null;

        // Sort properties so that arrays will be reconstructed in proper order
        TreeMap<String, String> sortedAttributes = new TreeMap<>();
        sortedAttributes.putAll(attributes);

        // Loop through properties, sorted by key
        for (Map.Entry<String, String> entry : sortedAttributes.entrySet()) {
            attrName = entry.getKey();
            attrVal = entry.getValue();

            currJsonLeaf = root;
            String curFieldName = null;
            JsonArray curArray = null;
            lastJsonObject = null;
            boolean addNeeded = false;

            // Split property names by period and iterate through parts
            for (String attrNamePart : attrName.split("\\.")) {

            	// Add last object found to JSON tree.  Need to handle
				// this way because last element found (leaf) needs to be
				// assigned the property value.
                if (lastJsonObject != null) {
                    if (addNeeded) {
                        if (currJsonLeaf.isJsonArray()) {
                            ((JsonArray) currJsonLeaf).add(lastJsonObject);
                        } else {
                            ((JsonObject) currJsonLeaf).add(curFieldName, lastJsonObject);
                        }
                    }
                    currJsonLeaf = (JsonObject) lastJsonObject;
                }
                addNeeded = false;
                // See if current level should be a JsonArray or JsonObject based on
				// whether name part contains square brackets.
                if (!attrNamePart.contains("[")) {
                	// This level should be inserted as a JsonObject
                    curFieldName = attrNamePart;
                    lastJsonObject = ((JsonObject) currJsonLeaf).get(curFieldName);
                    if (lastJsonObject == null) {
                        lastJsonObject = new JsonObject();
                        addNeeded = true;
                    } else if (!lastJsonObject.isJsonObject()) {
                        LOG.error("Unexpected condition - expecting to find JsonObject, but found " + lastJsonObject.getClass().getName());
                        lastJsonObject = new JsonObject();
                        addNeeded = true;
                    }
                } else {
                	// This level should be inserted as a JsonArray.

                    String[] curFieldNameParts = attrNamePart.split("[\\[\\]]");
                    curFieldName = curFieldNameParts[0];
                    int curIndex = Integer.parseInt(curFieldNameParts[1]);


                    curArray = ((JsonObject) currJsonLeaf).getAsJsonArray(curFieldName);

                    if (curArray == null) {
                    	// This is the first time we see this array.
						// Create a new JsonArray and add it to current
						// leaf
                        curArray = new JsonArray();
                        ((JsonObject) currJsonLeaf).add(curFieldName, curArray);
                    }

                    // Current leaf should point to the JsonArray for this level.
					// lastJsonObject should point to the array item entry to append
					// the next level to - which is a new one if the index value
					// isn't the end of the current array.
                    currJsonLeaf = curArray;
                    if (curArray.size() == curIndex + 1) {
                        lastJsonObject = curArray.get(curArray.size() - 1);
                    } else {
                        lastJsonObject = new JsonObject();
                        addNeeded = true;
                    }
                }
            }

            // Done parsing property name.  Add the value of this
			// property to the current json leaf, either as a property
			// or as a string (if the current leaf is a JsonArray)

            if (!curFieldName.endsWith("_length")) {
                if (currJsonLeaf.isJsonArray()) {
                    if ("true".equals(attrVal) || "false".equals(attrVal)) {
                        ((JsonArray) currJsonLeaf).add(Boolean.valueOf(attrVal));
                    } else if ("null".equals(attrVal)) {
                        ((JsonArray) currJsonLeaf).add(new JsonNull());
                    } else {
                        ((JsonArray) currJsonLeaf).add(attrVal);
                    }
                } else {
                    if (("true".equals(attrVal) || "false".equals(attrVal))) {
                        ((JsonObject) currJsonLeaf).addProperty(curFieldName, Boolean.valueOf(attrVal));
                    } else if ("null".equals(attrVal)){

                        ((JsonObject) currJsonLeaf).add(curFieldName, new JsonNull());
                    } else {
                        ((JsonObject) currJsonLeaf).addProperty(curFieldName, attrVal);
                    }
                }
            }
        }

        return (root.toString());
    }

    public void printProperties(Properties props) {
        securePrinter.printProperties(props);
    }

    public void printAttributes() {
        securePrinter.printAttributes(attributes);
    }

    public void printProperties(Properties props, String subpath) {
        securePrinter.printProperties(props, subpath);
    }

    public void printAttributes(String subpath) {
        securePrinter.printAttributes(attributes, subpath);
    }
}