Containerization feature of SO
[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
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.databind.node.ArrayNode;
28 import com.fasterxml.jackson.databind.node.ContainerNode;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
30 import com.google.common.base.CaseFormat;
31
32
33 public class ASDCLoggingVisitorImpl extends ASDCLoggingBaseVisitor<ContainerNode> {
34
35         private final ObjectMapper mapper = new ObjectMapper();
36         private ObjectNode doc;
37         private Deque<ContainerNode> nodeStore = new ArrayDeque<>();
38         
39         @Override
40         public ContainerNode visitDoc(ASDCLoggingParser.DocContext ctx) {
41                 doc  = mapper.createObjectNode();
42                 nodeStore.addFirst(doc);
43                 this.visitChildren(ctx);
44                 return doc;
45         }
46         
47         @Override
48         public ContainerNode visitValue(ASDCLoggingParser.ValueContext ctx) {
49                 
50                 return this.visitChildren(ctx);
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 }