5c88e8e42ecfb0986ff0cfcd798fbe8ee999446c
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.aaiclient.client.graphinventory.entities;
22
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;
31
32 public abstract class DSLNodeBase<T extends DSLNodeBase<?>> implements QueryStep {
33
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;
39
40     public DSLNodeBase() {
41         this.nodeName = "";
42         this.nodeKeys = new ArrayList<>();
43         this.query = new StringBuilder();
44         this.fields = new LinkedHashSet<>();
45
46     }
47
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);
54     }
55
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);
62     }
63
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;
70     }
71
72     public DSLOutputNode output() {
73         this.output = true;
74
75         return new DSLOutputNode(this);
76     }
77
78     public DSLOutputNode output(String... fields) {
79         this.output = true;
80         this.fields.addAll(Arrays.asList(fields));
81         return new DSLOutputNode(this);
82     }
83
84     public T and(DSLNodeKey... key) {
85         this.nodeKeys.addAll(Arrays.asList(key));
86
87         return (T) this;
88     }
89
90     @Override
91     public String build() {
92         StringBuilder result = new StringBuilder(query);
93         if (output) {
94             if (fields.isEmpty()) {
95                 result.append("*");
96             } else {
97                 String items =
98                         fields.stream().map(item -> String.format("'%s'", item)).collect(Collectors.joining(", "));
99                 result.append("{").append(items).append("}");
100             }
101         }
102         for (DSLNodeKey key : nodeKeys) {
103             result.append(key.build());
104         }
105
106         return result.toString();
107     }
108 }