2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.asdc.utils;
23 import java.util.ArrayDeque;
24 import java.util.Deque;
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;
33 public class ASDCLoggingVisitorImpl extends ASDCLoggingBaseVisitor<ContainerNode> {
35 private final ObjectMapper mapper = new ObjectMapper();
36 private ObjectNode doc;
37 private Deque<ContainerNode> nodeStore = new ArrayDeque<>();
40 public ContainerNode visitDoc(ASDCLoggingParser.DocContext ctx) {
41 doc = mapper.createObjectNode();
42 nodeStore.addFirst(doc);
43 this.visitChildren(ctx);
48 public ContainerNode visitValue(ASDCLoggingParser.ValueContext ctx) {
50 return this.visitChildren(ctx);
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());
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();
68 nodeStore.addFirst(node);
70 nodeStore.addFirst(((ObjectNode)nodeStore.peekFirst()).putObject(this.toLowerCamel(ctx.key().getText())));
72 this.visitChildren(ctx);
73 return nodeStore.removeFirst();
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();
85 private String keyMapper(String key) {
86 if ("Service Artifacts List".equals(key)) {
87 return "serviceArtifacts";
88 } else if ("Resource Instances List".equals(key)) {
90 } else if ("Resource Artifacts List".equals(key)) {
97 private String toLowerCamel(String key) {
98 String result = key.replaceAll("\\s", "");
99 if ("ServiceArtifactsInfo".equals(result)) {
100 return "artifactInfo";
102 return CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, result);