2 * Copyright 2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.cli.fw.output;
19 import org.onap.cli.fw.conf.Constants;
20 import org.onap.cli.fw.error.OnapCommandException;
21 import org.onap.cli.fw.error.OnapCommandOutputFormatNotsupported;
22 import org.onap.cli.fw.error.OnapCommandOutputPrintingFailed;
23 import org.onap.cli.fw.input.ParameterType;
24 import org.onap.cli.fw.output.print.OnapCommandPrint;
25 import org.onap.cli.fw.utils.OnapCommandUtils;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
33 * Onap Command result holds the final output of the command.
36 public class OnapCommandResult {
39 * if type=JSON, then JSON response of the command from back-end Onap service, by default all the command would
40 * set this value once the back-end call returns, which would be useful to print the output in JSON format, returned
41 * from the back-end service.
43 * if type=TEXT, then it holds the result in text format such as help message
45 private Object output;
48 * Type requested by user
50 private ResultType type = ResultType.TABLE;
53 * Scope requested by user
55 private OnapCommandResultAttributeScope scope = OnapCommandResultAttributeScope.SHORT;
58 * if type=TABLE, then List of result records, which could be printed on the CLI console, loaded from schema file
60 private List<OnapCommandResultAttribute> records = new ArrayList<>();
63 * Print horizontally or vertically, Mostly for show command, horizontal table while for list commands , it will be
64 * vertically printed. Respective command should set appropriately.
66 * loaded from schema file
68 private PrintDirection printDirection = PrintDirection.LANDSCAPE;
70 private String debugInfo = "";
75 private boolean includeTitle = true;
80 private boolean includeSeparator = true;
85 private boolean isDebug = false;
87 public PrintDirection getPrintDirection() {
88 return printDirection;
91 public void setPrintDirection(PrintDirection printDirection) {
92 this.printDirection = printDirection;
95 public Object getOutput() {
99 public void setOutput(Object output) {
100 this.output = output;
103 public List<OnapCommandResultAttribute> getRecords() {
107 public void setRecords(List<OnapCommandResultAttribute> records) {
108 this.records = records;
116 public Map<String, OnapCommandResultAttribute> getRecordsMap() {
117 Map<String, OnapCommandResultAttribute> recordMap = new HashMap<>();
119 for (OnapCommandResultAttribute record : this.getRecords()) {
120 recordMap.put(record.getName(), record);
126 public ResultType getType() {
130 public void setType(ResultType type) {
134 public OnapCommandResultAttributeScope getScope() {
138 public void setScope(OnapCommandResultAttributeScope scope) {
142 public boolean isIncludeTitle() {
146 public void setIncludeTitle(boolean includeTitle) {
147 this.includeTitle = includeTitle;
150 public boolean isIncludeSeparator() {
151 return includeSeparator;
154 public void setIncludeSeparator(boolean includeSeparator) {
155 this.includeSeparator = includeSeparator;
158 public String getDebugInfo() {
162 public void setDebugInfo(String debugInfo) {
163 this.debugInfo = debugInfo;
166 public boolean isDebug() {
170 public void setDebug(boolean isDebug) {
171 this.isDebug = isDebug;
175 * Helps to print the result based on the type.
178 * @throws OnapCommandOutputFormatNotsupported
180 * @throws OnapCommandOutputPrintingFailed
183 public String print() throws OnapCommandException {
184 String printOutput = "";
186 if (this.getRecords().isEmpty()) {
190 OnapCommandPrint print = new OnapCommandPrint();
191 print.setPrintTitle(this.isIncludeTitle());
192 if (this.getPrintDirection().equals(PrintDirection.LANDSCAPE)) {
193 for (OnapCommandResultAttribute record : this.getScopedRecords()) {
194 if (record.getType().equals(ParameterType.JSON)) {
195 print.addColumn(record.getName(), OnapCommandUtils.jsonFlatten(record.getValues()));
197 print.addColumn(record.getName(), record.getValues());
201 // Add property column
202 OnapCommandResultAttribute prp = new OnapCommandResultAttribute();
203 prp.setName(Constants.PORTRAINT_COLUMN_NAME_PROPERTY);
204 prp.setScope(OnapCommandResultAttributeScope.SHORT);
206 OnapCommandResultAttribute val = new OnapCommandResultAttribute();
207 val.setName(Constants.PORTRAINT_COLUMN_NAME_VALUE);
208 val.setScope(OnapCommandResultAttributeScope.SHORT);
210 for (OnapCommandResultAttribute record : this.getScopedRecords()) {
211 prp.getValues().add(record.getName());
212 if (record.getValues().size() == 1) {
213 val.getValues().add(record.getValues().get(0));
215 val.getValues().add(record.getValues().toString());
219 print.addColumn(prp.getName(), prp.getValues());
220 print.addColumn(val.getName(), val.getValues());
223 if (this.isDebug()) {
224 printOutput = this.getDebugInfo() + "\n";
227 if (this.getType().equals(ResultType.JSON)) {
228 return printOutput + print.printJson();
229 } else if (this.getType().equals(ResultType.TABLE)) {
230 return printOutput + print.printTable(this.isIncludeSeparator());
231 } else if (this.getType().equals(ResultType.CSV)) {
232 return printOutput + print.printCsv();
235 throw new OnapCommandOutputFormatNotsupported(this.getType().name());
238 private List<OnapCommandResultAttribute> getScopedRecords() {
239 List<OnapCommandResultAttribute> recordList = new ArrayList<>();
240 for (OnapCommandResultAttribute record : this.getRecords()) {
241 if (record.getScope().ordinal() > this.getScope().ordinal()) {
244 recordList.add(record);