Thomas Vachuska | 275d2e8 | 2016-07-14 17:41:34 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
| 20 | import com.google.common.io.ByteStreams; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | import java.io.InputStream; |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | |
| 29 | /** |
| 30 | * Context for executing a single Buck task. |
| 31 | */ |
| 32 | public class BuckTaskContext { |
| 33 | |
| 34 | private final String taskName; |
| 35 | private final ImmutableList<String> input; |
| 36 | private final List<String> output = new ArrayList<>(); |
| 37 | |
| 38 | BuckTaskContext(InputStream inputString) throws IOException { |
| 39 | String[] split = new String(ByteStreams.toByteArray(inputString)).split("\n"); |
| 40 | checkArgument(split.length >= 1, "Request must contain at least task type"); |
| 41 | this.taskName = split[0]; |
| 42 | ImmutableList.Builder<String> builder = ImmutableList.builder(); |
| 43 | for (int i = 1; i < split.length; i++) { |
| 44 | builder.add(split[i]); |
| 45 | } |
| 46 | input = builder.build(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the symbolic task name. |
| 51 | */ |
| 52 | public String taskName() { |
| 53 | return taskName; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the input data a list of strings. |
| 58 | * |
| 59 | * @return input data |
| 60 | */ |
| 61 | public List<String> input() { |
| 62 | return ImmutableList.copyOf(input); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the output data a list of strings. |
| 67 | * |
| 68 | * @return output data |
| 69 | */ |
| 70 | List<String> output() { |
| 71 | return ImmutableList.copyOf(output); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Adds a line to the output data. |
| 76 | * |
| 77 | * @param line line of output data |
| 78 | */ |
| 79 | public void output(String line) { |
| 80 | output.add(line); |
| 81 | } |
| 82 | |
| 83 | } |