blob: b8d189c4d486aad2e72e5177d9b419c5f65f1d45 [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'Connorb999a812017-11-27 18:40:08 -080020import com.google.common.collect.Lists;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070021
Brian O'Connorb999a812017-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'Connorb999a812017-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'Connorb999a812017-11-27 18:40:08 -080037 private final List<String> output;
Thomas Vachuska275d2e82016-07-14 17:41:34 -070038
Brian O'Connorb999a812017-11-27 18:40:08 -080039 BuckTaskContext(InputStream inputStream) throws IOException {
40 ImmutableList<String> lines = slurpInput(inputStream);
41 checkArgument(lines.size() >= 1 && !lines.get(0).isEmpty(),
42 "Request must contain at least task type");
43 this.taskName = lines.get(0);
44 this.input = lines.subList(1, lines.size());
45 this.output = Lists.newArrayList();
46 }
47
48 /**
49 * Reads all input, line by line, from a stream until an empty line or EOF is encountered.
50 *
51 * @param stream input stream
52 * @return the lines of the input
53 * @throws IOException
54 */
55 private static ImmutableList<String> slurpInput(InputStream stream) throws IOException {
56 ImmutableList.Builder<String> lines = ImmutableList.builder();
57 BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
58 while(true) {
59 String line = bufferedReader.readLine();
60 if (line == null || line.trim().length() == 0) {
61 // Empty line or EOF
62 break;
63 }
64 lines.add(line);
Thomas Vachuska275d2e82016-07-14 17:41:34 -070065 }
Brian O'Connorb999a812017-11-27 18:40:08 -080066 return lines.build();
Thomas Vachuska275d2e82016-07-14 17:41:34 -070067 }
68
69 /**
70 * Returns the symbolic task name.
71 */
72 public String taskName() {
73 return taskName;
74 }
75
76 /**
77 * Returns the input data a list of strings.
78 *
79 * @return input data
80 */
81 public List<String> input() {
82 return ImmutableList.copyOf(input);
83 }
84
85 /**
86 * Returns the output data a list of strings.
87 *
88 * @return output data
89 */
90 List<String> output() {
91 return ImmutableList.copyOf(output);
92 }
93
94 /**
95 * Adds a line to the output data.
96 *
97 * @param line line of output data
98 */
99 public void output(String line) {
100 output.add(line);
101 }
102
103}