Replaced all tabs with spaces in java and pom.xml
[so.git] / asdc-controller / src / test / java / org / onap / so / asdc / utils / ASDCLoggingVisitorImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.asdc.utils;
22
23 import java.util.ArrayDeque;
24 import java.util.Deque;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.node.ArrayNode;
27 import com.fasterxml.jackson.databind.node.ContainerNode;
28 import com.fasterxml.jackson.databind.node.ObjectNode;
29 import com.google.common.base.CaseFormat;
30
31
32 public class ASDCLoggingVisitorImpl extends ASDCLoggingBaseVisitor<ContainerNode> {
33
34     private final ObjectMapper mapper = new ObjectMapper();
35     private ObjectNode doc;
36     private Deque<ContainerNode> nodeStore = new ArrayDeque<>();
37
38     @Override
39     public ContainerNode visitDoc(ASDCLoggingParser.DocContext ctx) {
40         doc = mapper.createObjectNode();
41         nodeStore.addFirst(doc);
42         this.visitChildren(ctx);
43         return doc;
44     }
45
46     @Override
47     public ContainerNode visitValue(ASDCLoggingParser.ValueContext ctx) {
48
49         return this.visitChildren(ctx);
50
51     }
52
53     @Override
54     public ContainerNode visitSimplePair(ASDCLoggingParser.SimplePairContext ctx) {
55         ObjectNode node = mapper.createObjectNode();
56         ((ObjectNode) nodeStore.peekFirst()).put(this.toLowerCamel(ctx.key().getText()), ctx.keyValue().getText());
57
58         return node;
59     }
60
61     @Override
62     public ContainerNode visitComplexPair(ASDCLoggingParser.ComplexPairContext ctx) {
63         ContainerNode container = nodeStore.peekFirst();
64         if (container.isArray()) {
65             ArrayNode array = (ArrayNode) container;
66             ObjectNode node = mapper.createObjectNode();
67             array.add(node);
68             nodeStore.addFirst(node);
69         } else {
70             nodeStore.addFirst(((ObjectNode) nodeStore.peekFirst()).putObject(this.toLowerCamel(ctx.key().getText())));
71         }
72         this.visitChildren(ctx);
73         return nodeStore.removeFirst();
74
75
76     }
77
78     @Override
79     public ContainerNode visitList(ASDCLoggingParser.ListContext ctx) {
80         nodeStore.addFirst(((ObjectNode) nodeStore.peekFirst()).putArray(this.keyMapper(ctx.listName().getText())));
81         this.visitChildren(ctx);
82         return nodeStore.removeFirst();
83     }
84
85     private String keyMapper(String key) {
86         if ("Service Artifacts List".equals(key)) {
87             return "serviceArtifacts";
88         } else if ("Resource Instances List".equals(key)) {
89             return "resources";
90         } else if ("Resource Artifacts List".equals(key)) {
91             return "artifacts";
92         } else {
93             return key;
94         }
95     }
96
97     private String toLowerCamel(String key) {
98         String result = key.replaceAll("\\s", "");
99         if ("ServiceArtifactsInfo".equals(result)) {
100             return "artifactInfo";
101         }
102         return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, result);
103     }
104 }