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 | bd0e720 | 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 | bd0e720 | 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 | bd0e720 | 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 | |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 28 | /** |
| 29 | * Context for executing a single Buck task. |
| 30 | */ |
| 31 | public class BuckTaskContext { |
| 32 | |
| 33 | private final String taskName; |
| 34 | private final ImmutableList<String> input; |
Brian O'Connor | bd0e720 | 2017-11-27 18:40:08 -0800 | [diff] [blame] | 35 | private final List<String> output; |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 36 | |
Ray Milkey | 0e44012 | 2018-01-16 15:00:50 -0800 | [diff] [blame] | 37 | public static BuckTaskContext createBuckTaskContext(InputStream inputStream) throws IOException { |
Brian O'Connor | bd0e720 | 2017-11-27 18:40:08 -0800 | [diff] [blame] | 38 | ImmutableList<String> lines = slurpInput(inputStream); |
Ray Milkey | 0e44012 | 2018-01-16 15:00:50 -0800 | [diff] [blame] | 39 | if (lines.size() == 0) { |
| 40 | return null; |
| 41 | } else { |
| 42 | return new BuckTaskContext(lines); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | BuckTaskContext(ImmutableList<String> lines) { |
Brian O'Connor | bd0e720 | 2017-11-27 18:40:08 -0800 | [diff] [blame] | 47 | 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 Milkey | 0e44012 | 2018-01-16 15:00:50 -0800 | [diff] [blame] | 62 | while (true) { |
Brian O'Connor | bd0e720 | 2017-11-27 18:40:08 -0800 | [diff] [blame] | 63 | 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 Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 69 | } |
Brian O'Connor | bd0e720 | 2017-11-27 18:40:08 -0800 | [diff] [blame] | 70 | return lines.build(); |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns the symbolic task name. |
Yuta HIGUCHI | 42cc140 | 2018-05-21 12:08:03 -0700 | [diff] [blame] | 75 | * |
| 76 | * @return symbolic task name |
Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 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 | } |