Merge "Changed an instance-reference to a static reference."
[cli.git] / framework / src / main / java / org / onap / cli / fw / output / OnapCommandResult.java
1 /*
2  * Copyright 2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.cli.fw.output;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23 import org.onap.cli.fw.conf.OnapCommandConstants;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.error.OnapCommandOutputFormatNotsupported;
26 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
27 import org.onap.cli.fw.output.print.OnapCommandPrint;
28
29 /**
30  * Oclip Command result holds the final output of the command.
31  *
32  */
33 public class OnapCommandResult {
34
35     /*
36      * if type=JSON, then JSON response of the command from back-end Oclip service, by default all the command would
37      * set this value once the back-end call returns, which would be useful to print the output in JSON format, returned
38      * from the back-end service.
39      *
40      * if type=TEXT, then it holds the result in text format such as help message
41      */
42     private Object output = "";
43
44     /*
45      * Type requested by user
46      */
47     private OnapCommandResultType type = OnapCommandResultType.TABLE;
48
49     /*
50      * Scope requested by user
51      */
52     private OnapCommandResultAttributeScope scope = OnapCommandResultAttributeScope.SHORT;
53
54     /*
55      * if type=TABLE, then List of result records, which could be printed on the CLI console, loaded from schema file
56      */
57     private List<OnapCommandResultAttribute> records = new ArrayList<>();
58
59     /*
60      * Print horizontally or vertically, Mostly for show command, horizontal table while for list commands , it will be
61      * vertically printed. Respective command should set appropriately.
62      *
63      * loaded from schema file
64      */
65     private OnapCommandPrintDirection printDirection = OnapCommandPrintDirection.LANDSCAPE;
66
67     private String debugInfo = "";
68
69     /**
70      * Requested by user.
71      */
72     private boolean includeTitle = true;
73
74     /**
75      * Requested by user.
76      */
77     private boolean includeSeparator = true;
78
79     /**
80      * Requested by user.
81      */
82     private boolean isDebug = false;
83
84     /**
85      * Command passed/failed
86      * @return
87      */
88
89     private boolean passed = true;
90
91     public OnapCommandPrintDirection getPrintDirection() {
92         return printDirection;
93     }
94
95     public void setPrintDirection(OnapCommandPrintDirection printDirection) {
96         this.printDirection = printDirection;
97     }
98
99     public Object getOutput() {
100         return output;
101     }
102
103     public void setOutput(Object output) {
104         this.output = output;
105     }
106
107     public List<OnapCommandResultAttribute> getRecords() {
108         return records;
109     }
110
111     public void setRecords(List<OnapCommandResultAttribute> records) {
112         this.records = records;
113     }
114
115     /**
116      * Record mapping.
117      *
118      * @return attributes
119      */
120     public Map<String, OnapCommandResultAttribute> getRecordsMap() {
121         Map<String, OnapCommandResultAttribute> recordMap = new HashMap<>();
122
123         for (OnapCommandResultAttribute data : this.getRecords()) {
124             recordMap.put(data.getName(), data);
125         }
126
127         return recordMap;
128     }
129
130     public OnapCommandResultType getType() {
131         return type;
132     }
133
134     public void setType(OnapCommandResultType type) {
135         this.type = type;
136     }
137
138     public OnapCommandResultAttributeScope getScope() {
139         return scope;
140     }
141
142     public void setScope(OnapCommandResultAttributeScope scope) {
143         this.scope = scope;
144     }
145
146     public boolean isIncludeTitle() {
147         return includeTitle;
148     }
149
150     public void setIncludeTitle(boolean includeTitle) {
151         this.includeTitle = includeTitle;
152     }
153
154     public boolean isIncludeSeparator() {
155         return includeSeparator;
156     }
157
158     public void setIncludeSeparator(boolean includeSeparator) {
159         this.includeSeparator = includeSeparator;
160     }
161
162     public String getDebugInfo() {
163         return debugInfo;
164     }
165
166     public void setDebugInfo(String debugInfo) {
167         this.debugInfo = debugInfo;
168     }
169
170     public boolean isDebug() {
171         return isDebug;
172     }
173
174     public void setDebug(boolean isDebug) {
175         this.isDebug = isDebug;
176     }
177
178     public int getNumberOfRows() {
179         int noOfRecords = 0;
180
181         for (OnapCommandResultAttribute cols : this.records) {
182             if (cols != null && noOfRecords < cols.getValues().size()) {
183                 noOfRecords = cols.getValues().size();
184             }
185         }
186
187         return noOfRecords;
188     }
189
190     public OnapCommandPrint createAndLoadPrint() {
191         OnapCommandPrint print = new OnapCommandPrint();
192         print.setPrintTitle(this.isIncludeTitle());
193         print.setDirection(this.printDirection);
194
195         if (!this.getRecords().isEmpty()) {
196             if (this.getPrintDirection().equals(OnapCommandPrintDirection.LANDSCAPE)) {
197                 for (OnapCommandResultAttribute data : this.getScopedRecords()) {
198                     print.addColumn(data.getName(), data.getValues());
199                 }
200             } else {
201                 // Add property column
202                 OnapCommandResultAttribute prp = new OnapCommandResultAttribute();
203                 prp.setName(OnapCommandConstants.PORTRAINT_COLUMN_NAME_PROPERTY);
204                 prp.setScope(OnapCommandResultAttributeScope.SHORT);
205                 // Add value column
206                 OnapCommandResultAttribute val = new OnapCommandResultAttribute();
207                 val.setName(OnapCommandConstants.PORTRAINT_COLUMN_NAME_VALUE);
208                 val.setScope(OnapCommandResultAttributeScope.SHORT);
209
210                 for (OnapCommandResultAttribute data : this.getScopedRecords()) {
211                     prp.getValues().add(data.getName());
212                     if (data.getValues().size() == 1) {
213                         val.getValues().add(data.getValues().get(0));
214                     } else {
215                         val.getValues().add(data.getValues().toString());
216                     }
217                 }
218
219                 print.addColumn(prp.getName(), prp.getValues());
220                 print.addColumn(val.getName(), val.getValues());
221             }
222         }
223         return print;
224     }
225
226     /**
227      * Helps to print the result based on the type.
228      *
229      * @return string
230      * @throws OnapCommandOutputFormatNotsupported
231      *             excpetion
232      * @throws OnapCommandOutputPrintingFailed
233      *             exception
234      */
235     public String print() throws OnapCommandException {
236         if (this.getType().equals(OnapCommandResultType.TEXT)) {
237              return this.getOutput().toString();
238         }
239
240         OnapCommandPrint print = createAndLoadPrint();
241
242         if (this.getType().equals(OnapCommandResultType.JSON)) {
243             return print.printJson();
244         } else if (this.getType().equals(OnapCommandResultType.TABLE)) {
245             return print.printTable(this.isIncludeSeparator());
246         } else if (this.getType().equals(OnapCommandResultType.CSV)) {
247             return print.printCsv();
248         } else if (this.getType().equals(OnapCommandResultType.YAML)) {
249             return print.printYaml();
250         }
251
252         throw new OnapCommandOutputFormatNotsupported(this.getType().name());
253     }
254
255     private List<OnapCommandResultAttribute> getScopedRecords() {
256         List<OnapCommandResultAttribute> recordList = new ArrayList<>();
257         for (OnapCommandResultAttribute data : this.getRecords()) {
258             if (data.getScope().ordinal() > this.getScope().ordinal()) {
259                 continue;
260             }
261             recordList.add(data);
262         }
263
264         return recordList;
265     }
266
267     public boolean isPassed() {
268         return passed;
269     }
270
271     public void setPassed(boolean passed) {
272         this.passed = passed;
273     }
274 }