blob: 8dabfd2d147d6de7238fee1833ae1bc78dab6dcf [file] [log] [blame]
Thomas Vachuska275d2e82016-07-14 17:41:34 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Thomas Vachuska275d2e82016-07-14 17:41:34 -07003 *
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
17package org.onosproject.buckdaemon;
18
19import com.google.common.collect.ImmutableList;
Brian O'Connorbd0e7202017-11-27 18:40:08 -080020import com.google.common.collect.Lists;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070021
Brian O'Connorbd0e7202017-11-27 18:40:08 -080022import java.io.BufferedReader;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070023import java.io.IOException;
24import java.io.InputStream;
Brian O'Connorbd0e7202017-11-27 18:40:08 -080025import java.io.InputStreamReader;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070026import java.util.List;
27
Thomas Vachuska275d2e82016-07-14 17:41:34 -070028/**
29 * Context for executing a single Buck task.
30 */
31public class BuckTaskContext {
32
33 private final String taskName;
34 private final ImmutableList<String> input;
Brian O'Connorbd0e7202017-11-27 18:40:08 -080035 private final List<String> output;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070036
Ray Milkey0e440122018-01-16 15:00:50 -080037 public static BuckTaskContext createBuckTaskContext(InputStream inputStream) throws IOException {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080038 ImmutableList<String> lines = slurpInput(inputStream);
Ray Milkey0e440122018-01-16 15:00:50 -080039 if (lines.size() == 0) {
40 return null;
41 } else {
42 return new BuckTaskContext(lines);
43 }
44 }
45
46 BuckTaskContext(ImmutableList<String> lines) {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080047 this.taskName = lines.get(0);
48 this.input = lines.subList(1, lines.size());
49 this.output = Lists.newArrayList();
50 }
51
52 /**
53 * Reads all input, line by line, from a stream until an empty line or EOF is encountered.
54 *
55 * @param stream input stream
56 * @return the lines of the input
57 * @throws IOException
58 */
59 private static ImmutableList<String> slurpInput(InputStream stream) throws IOException {
60 ImmutableList.Builder<String> lines = ImmutableList.builder();
61 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
Ray Milkey0e440122018-01-16 15:00:50 -080062 while (true) {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080063 String line = bufferedReader.readLine();
64 if (line == null || line.trim().length() == 0) {
65 // Empty line or EOF
66 break;
67 }
68 lines.add(line);
Thomas Vachuska275d2e82016-07-14 17:41:34 -070069 }
Brian O'Connorbd0e7202017-11-27 18:40:08 -080070 return lines.build();
Thomas Vachuska275d2e82016-07-14 17:41:34 -070071 }
72
73 /**
74 * Returns the symbolic task name.
Yuta HIGUCHI42cc1402018-05-21 12:08:03 -070075 *
76 * @return symbolic task name
Thomas Vachuska275d2e82016-07-14 17:41:34 -070077 */
78 public String taskName() {
79 return taskName;
80 }
81
82 /**
83 * Returns the input data a list of strings.
84 *
85 * @return input data
86 */
87 public List<String> input() {
88 return ImmutableList.copyOf(input);
89 }
90
91 /**
92 * Returns the output data a list of strings.
93 *
94 * @return output data
95 */
96 List<String> output() {
97 return ImmutableList.copyOf(output);
98 }
99
100 /**
101 * Adds a line to the output data.
102 *
103 * @param line line of output data
104 */
105 public void output(String line) {
106 output.add(line);
107 }
108
109}