2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 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.aaiclient.client.graphinventory.entities;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import org.onap.aaiclient.client.aai.entities.QueryStep;
30 import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName;
32 public abstract class DSLNodeBase<T extends DSLNodeBase<?>> implements QueryStep {
34 protected final String nodeName;
35 protected final Collection<String> fields;
36 protected final List<DSLNodeKey> nodeKeys;
37 protected final StringBuilder query;
38 protected boolean output = false;
40 public DSLNodeBase() {
42 this.nodeKeys = new ArrayList<>();
43 this.query = new StringBuilder();
44 this.fields = new LinkedHashSet<>();
48 public DSLNodeBase(GraphInventoryObjectName name) {
49 this.nodeName = name.typeName();
50 this.nodeKeys = new ArrayList<>();
51 this.query = new StringBuilder();
52 this.fields = new LinkedHashSet<>();
53 query.append(nodeName);
56 public DSLNodeBase(GraphInventoryObjectName name, DSLNodeKey... key) {
57 this.nodeName = name.typeName();
58 this.nodeKeys = Arrays.asList(key);
59 this.query = new StringBuilder();
60 this.fields = new LinkedHashSet<>();
61 query.append(nodeName);
64 public DSLNodeBase(DSLNodeBase<?> copy) {
65 this.nodeName = copy.nodeName;
66 this.nodeKeys = copy.nodeKeys;
67 this.query = new StringBuilder(copy.query);
68 this.fields = copy.fields;
69 this.output = copy.output;
72 public DSLOutputNode output() {
75 return new DSLOutputNode(this);
78 public DSLOutputNode output(String... fields) {
80 this.fields.addAll(Arrays.asList(fields));
81 return new DSLOutputNode(this);
84 public T and(DSLNodeKey... key) {
85 this.nodeKeys.addAll(Arrays.asList(key));
91 public String build() {
92 StringBuilder result = new StringBuilder(query);
94 if (fields.isEmpty()) {
98 fields.stream().map(item -> String.format("'%s'", item)).collect(Collectors.joining(", "));
99 result.append("{").append(items).append("}");
102 for (DSLNodeKey key : nodeKeys) {
103 result.append(key.build());
106 return result.toString();