Add seed code from Open-O
[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 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;
26
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * Onap Command result holds the final output of the command.
34  *
35  */
36 public class OnapCommandResult {
37
38     /*
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.
42      *
43      * if type=TEXT, then it holds the result in text format such as help message
44      */
45     private Object output;
46
47     /*
48      * Type requested by user
49      */
50     private ResultType type = ResultType.TABLE;
51
52     /*
53      * Scope requested by user
54      */
55     private OnapCommandResultAttributeScope scope = OnapCommandResultAttributeScope.SHORT;
56
57     /*
58      * if type=TABLE, then List of result records, which could be printed on the CLI console, loaded from schema file
59      */
60     private List<OnapCommandResultAttribute> records = new ArrayList<>();
61
62     /*
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.
65      *
66      * loaded from schema file
67      */
68     private PrintDirection printDirection = PrintDirection.LANDSCAPE;
69
70     private String debugInfo = "";
71
72     /**
73      * Requested by user.
74      */
75     private boolean includeTitle = true;
76
77     /**
78      * Requested by user.
79      */
80     private boolean includeSeparator = true;
81
82     /**
83      * Requested by user.
84      */
85     private boolean isDebug = false;
86
87     public PrintDirection getPrintDirection() {
88         return printDirection;
89     }
90
91     public void setPrintDirection(PrintDirection printDirection) {
92         this.printDirection = printDirection;
93     }
94
95     public Object getOutput() {
96         return output;
97     }
98
99     public void setOutput(Object output) {
100         this.output = output;
101     }
102
103     public List<OnapCommandResultAttribute> getRecords() {
104         return records;
105     }
106
107     public void setRecords(List<OnapCommandResultAttribute> records) {
108         this.records = records;
109     }
110
111     /**
112      * Record mapping.
113      *
114      * @return attributes
115      */
116     public Map<String, OnapCommandResultAttribute> getRecordsMap() {
117         Map<String, OnapCommandResultAttribute> recordMap = new HashMap<>();
118
119         for (OnapCommandResultAttribute record : this.getRecords()) {
120             recordMap.put(record.getName(), record);
121         }
122
123         return recordMap;
124     }
125
126     public ResultType getType() {
127         return type;
128     }
129
130     public void setType(ResultType type) {
131         this.type = type;
132     }
133
134     public OnapCommandResultAttributeScope getScope() {
135         return scope;
136     }
137
138     public void setScope(OnapCommandResultAttributeScope scope) {
139         this.scope = scope;
140     }
141
142     public boolean isIncludeTitle() {
143         return includeTitle;
144     }
145
146     public void setIncludeTitle(boolean includeTitle) {
147         this.includeTitle = includeTitle;
148     }
149
150     public boolean isIncludeSeparator() {
151         return includeSeparator;
152     }
153
154     public void setIncludeSeparator(boolean includeSeparator) {
155         this.includeSeparator = includeSeparator;
156     }
157
158     public String getDebugInfo() {
159         return debugInfo;
160     }
161
162     public void setDebugInfo(String debugInfo) {
163         this.debugInfo = debugInfo;
164     }
165
166     public boolean isDebug() {
167         return isDebug;
168     }
169
170     public void setDebug(boolean isDebug) {
171         this.isDebug = isDebug;
172     }
173
174     /**
175      * Helps to print the result based on the type.
176      *
177      * @return string
178      * @throws OnapCommandOutputFormatNotsupported
179      *             excpetion
180      * @throws OnapCommandOutputPrintingFailed
181      *             exception
182      */
183     public String print() throws OnapCommandException {
184         String printOutput = "";
185
186         if (this.getRecords().isEmpty()) {
187             return printOutput;
188         }
189
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()));
196                 } else {
197                     print.addColumn(record.getName(), record.getValues());
198                 }
199             }
200         } else {
201             // Add property column
202             OnapCommandResultAttribute prp = new OnapCommandResultAttribute();
203             prp.setName(Constants.PORTRAINT_COLUMN_NAME_PROPERTY);
204             prp.setScope(OnapCommandResultAttributeScope.SHORT);
205             // Add value column
206             OnapCommandResultAttribute val = new OnapCommandResultAttribute();
207             val.setName(Constants.PORTRAINT_COLUMN_NAME_VALUE);
208             val.setScope(OnapCommandResultAttributeScope.SHORT);
209
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));
214                 } else {
215                     val.getValues().add(record.getValues().toString());
216                 }
217             }
218
219             print.addColumn(prp.getName(), prp.getValues());
220             print.addColumn(val.getName(), val.getValues());
221         }
222
223         if (this.isDebug()) {
224             printOutput = this.getDebugInfo() + "\n";
225         }
226
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();
233         }
234
235         throw new OnapCommandOutputFormatNotsupported(this.getType().name());
236     }
237
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()) {
242                 continue;
243             }
244             recordList.add(record);
245         }
246
247         return recordList;
248     }
249 }