Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 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.onosproject.buckdaemon; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 20 | import com.google.common.collect.Lists; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 21 | |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 22 | import java.io.BufferedReader; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 25 | import java.io.InputStreamReader; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 26 | import java.util.List; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.checkArgument; |
| 29 | |
| 30 | /** |
| 31 | * Context for executing a single Buck task. |
| 32 | */ |
| 33 | public class BuckTaskContext { |
| 34 | |
| 35 | private final String taskName; |
| 36 | private final ImmutableList<String> input; |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 37 | private final List<String> output; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 38 | |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 39 | 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 Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 65 | } |
Brian O'Connor | b999a81 | 2017-11-27 18:40:08 -0800 | [diff] [blame^] | 66 | return lines.build(); |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 67 | } |
| 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 | } |