blob: 2cfb35dcb3b0d1d92a7610d0a153ce9cb5e00dff [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
28import static com.google.common.base.Preconditions.checkArgument;
29
30/**
31 * Context for executing a single Buck task.
32 */
33public class BuckTaskContext {
34
35 private final String taskName;
36 private final ImmutableList<String> input;
Brian O'Connorbd0e7202017-11-27 18:40:08 -080037 private final List<String> output;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070038
Ray Milkey0e440122018-01-16 15:00:50 -080039 public static BuckTaskContext createBuckTaskContext(InputStream inputStream) throws IOException {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080040 ImmutableList<String> lines = slurpInput(inputStream);
Ray Milkey0e440122018-01-16 15:00:50 -080041 if (lines.size() == 0) {
42 return null;
43 } else {
44 return new BuckTaskContext(lines);
45 }
46 }
47
48 BuckTaskContext(ImmutableList<String> lines) {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080049 this.taskName = lines.get(0);
50 this.input = lines.subList(1, lines.size());
51 this.output = Lists.newArrayList();
52 }
53
54 /**
55 * Reads all input, line by line, from a stream until an empty line or EOF is encountered.
56 *
57 * @param stream input stream
58 * @return the lines of the input
59 * @throws IOException
60 */
61 private static ImmutableList<String> slurpInput(InputStream stream) throws IOException {
62 ImmutableList.Builder<String> lines = ImmutableList.builder();
63 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
Ray Milkey0e440122018-01-16 15:00:50 -080064 while (true) {
Brian O'Connorbd0e7202017-11-27 18:40:08 -080065 String line = bufferedReader.readLine();
66 if (line == null || line.trim().length() == 0) {
67 // Empty line or EOF
68 break;
69 }
70 lines.add(line);
Thomas Vachuska275d2e82016-07-14 17:41:34 -070071 }
Brian O'Connorbd0e7202017-11-27 18:40:08 -080072 return lines.build();
Thomas Vachuska275d2e82016-07-14 17:41:34 -070073 }
74
75 /**
76 * Returns the symbolic task name.
77 */
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}