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 java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
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;
30 * Oclip Command result holds the final output of the command.
33 public class OnapCommandResult {
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.
40 * if type=TEXT, then it holds the result in text format such as help message
42 private Object output = "";
45 * Type requested by user
47 private OnapCommandResultType type = OnapCommandResultType.TABLE;
50 * Scope requested by user
52 private OnapCommandResultAttributeScope scope = OnapCommandResultAttributeScope.SHORT;
55 * if type=TABLE, then List of result records, which could be printed on the CLI console, loaded from schema file
57 private List<OnapCommandResultAttribute> records = new ArrayList<>();
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.
63 * loaded from schema file
65 private OnapCommandPrintDirection printDirection = OnapCommandPrintDirection.LANDSCAPE;
67 private String debugInfo = "";
72 private boolean includeTitle = true;
77 private boolean includeSeparator = true;
82 private boolean isDebug = false;
85 * Command passed/failed
89 private boolean passed = true;
91 public OnapCommandPrintDirection getPrintDirection() {
92 return printDirection;
95 public void setPrintDirection(OnapCommandPrintDirection printDirection) {
96 this.printDirection = printDirection;
99 public Object getOutput() {
103 public void setOutput(Object output) {
104 this.output = output;
107 public List<OnapCommandResultAttribute> getRecords() {
111 public void setRecords(List<OnapCommandResultAttribute> records) {
112 this.records = records;
120 public Map<String, OnapCommandResultAttribute> getRecordsMap() {
121 Map<String, OnapCommandResultAttribute> recordMap = new HashMap<>();
123 for (OnapCommandResultAttribute record : this.getRecords()) {
124 recordMap.put(record.getName(), record);
130 public OnapCommandResultType getType() {
134 public void setType(OnapCommandResultType type) {
138 public OnapCommandResultAttributeScope getScope() {
142 public void setScope(OnapCommandResultAttributeScope scope) {
146 public boolean isIncludeTitle() {
150 public void setIncludeTitle(boolean includeTitle) {
151 this.includeTitle = includeTitle;
154 public boolean isIncludeSeparator() {
155 return includeSeparator;
158 public void setIncludeSeparator(boolean includeSeparator) {
159 this.includeSeparator = includeSeparator;
162 public String getDebugInfo() {
166 public void setDebugInfo(String debugInfo) {
167 this.debugInfo = debugInfo;
170 public boolean isDebug() {
174 public void setDebug(boolean isDebug) {
175 this.isDebug = isDebug;
178 public int getNumberOfRows() {
181 for (OnapCommandResultAttribute cols : this.records) {
182 if (cols != null && noOfRecords < cols.getValues().size()) {
183 noOfRecords = cols.getValues().size();
190 public OnapCommandPrint createAndLoadPrint() {
191 OnapCommandPrint print = new OnapCommandPrint();
192 print.setPrintTitle(this.isIncludeTitle());
193 print.setDirection(this.printDirection);
195 if (!this.getRecords().isEmpty()) {
196 if (this.getPrintDirection().equals(OnapCommandPrintDirection.LANDSCAPE)) {
197 for (OnapCommandResultAttribute record : this.getScopedRecords()) {
198 print.addColumn(record.getName(), record.getValues());
201 // Add property column
202 OnapCommandResultAttribute prp = new OnapCommandResultAttribute();
203 prp.setName(OnapCommandConstants.PORTRAINT_COLUMN_NAME_PROPERTY);
204 prp.setScope(OnapCommandResultAttributeScope.SHORT);
206 OnapCommandResultAttribute val = new OnapCommandResultAttribute();
207 val.setName(OnapCommandConstants.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());
227 * Helps to print the result based on the type.
230 * @throws OnapCommandOutputFormatNotsupported
232 * @throws OnapCommandOutputPrintingFailed
235 public String print() throws OnapCommandException {
236 if (this.getType().equals(OnapCommandResultType.TEXT)) {
237 return this.getOutput().toString();
240 OnapCommandPrint print = createAndLoadPrint();
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();
252 throw new OnapCommandOutputFormatNotsupported(this.getType().name());
255 private List<OnapCommandResultAttribute> getScopedRecords() {
256 List<OnapCommandResultAttribute> recordList = new ArrayList<>();
257 for (OnapCommandResultAttribute record : this.getRecords()) {
258 if (record.getScope().ordinal() > this.getScope().ordinal()) {
261 recordList.add(record);
267 public boolean isPassed() {
271 public void setPassed(boolean passed) {
272 this.passed = passed;