ghj0504520 | 8b252d2 | 2018-10-18 10:16:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.cli.net; |
| 18 | |
| 19 | |
Ray Milkey | 956bb16 | 2018-10-26 10:53:44 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Command; |
| 21 | import org.apache.karaf.shell.api.action.Option; |
Carmelo Cascone | a46f554 | 2018-12-12 23:41:01 -0800 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
ghj0504520 | 8b252d2 | 2018-10-18 10:16:22 -0700 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.driver.Behaviour; |
| 25 | import org.onosproject.net.pi.model.PiPipeconf; |
| 26 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Collection; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * Query registered pipeconfs. |
| 34 | */ |
Carmelo Cascone | a46f554 | 2018-12-12 23:41:01 -0800 | [diff] [blame] | 35 | @Service |
ghj0504520 | 8b252d2 | 2018-10-18 10:16:22 -0700 | [diff] [blame] | 36 | @Command(scope = "onos", name = "pipeconfs", |
Carmelo Cascone | a46f554 | 2018-12-12 23:41:01 -0800 | [diff] [blame] | 37 | description = "List registered pipeconfs") |
ghj0504520 | 8b252d2 | 2018-10-18 10:16:22 -0700 | [diff] [blame] | 38 | public class PipeconfCommand extends AbstractShellCommand { |
| 39 | |
| 40 | protected PiPipeconfService piPipeconfService; |
| 41 | |
| 42 | @Option(name = "-s", aliases = "--short", |
| 43 | description = "Print more succinct output for each pipeconf", |
| 44 | required = false, multiValued = false) |
| 45 | private boolean shortOutput = false; |
| 46 | |
| 47 | @Override |
Ray Milkey | 956bb16 | 2018-10-26 10:53:44 -0700 | [diff] [blame] | 48 | protected void doExecute() { |
ghj0504520 | 8b252d2 | 2018-10-18 10:16:22 -0700 | [diff] [blame] | 49 | piPipeconfService = get(PiPipeconfService.class); |
| 50 | |
| 51 | for (PiPipeconf piPipeconf : piPipeconfService.getPipeconfs()) { |
| 52 | if (shortOutput) { |
| 53 | print("id=%s", piPipeconf.id().toString()); |
| 54 | } else { |
| 55 | print("id=%s, behaviors=%s, extensions=%s", piPipeconf.id().toString(), |
| 56 | getBehaviors(piPipeconf), getExtensions(piPipeconf)); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Get all behaviour of a pipeconf and converts a list of behaviour name to string. |
| 63 | * |
| 64 | * @param piPipeconf query PiPipeconf |
| 65 | * |
| 66 | * @return string of behaviour name list |
| 67 | */ |
| 68 | private String getBehaviors(PiPipeconf piPipeconf) { |
| 69 | Collection<Class<? extends Behaviour>> behaviours = piPipeconf.behaviours(); |
| 70 | ArrayList<String> result = new ArrayList<>(); |
| 71 | |
| 72 | for (Class<? extends Behaviour> behaviour:behaviours) { |
| 73 | result.add(behaviour.getSimpleName()); |
| 74 | } |
| 75 | |
| 76 | return result.toString(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get all extension of a pipeconf and converts a list of extension |
| 81 | * name to string. |
| 82 | * |
| 83 | * @param piPipeconf query PiPipeconf |
| 84 | * |
| 85 | * @return string of extension name list |
| 86 | */ |
| 87 | private String getExtensions(PiPipeconf piPipeconf) { |
| 88 | ArrayList<String> result = new ArrayList<>(); |
| 89 | |
| 90 | for (PiPipeconf.ExtensionType extensionType : PiPipeconf.ExtensionType.values() |
| 91 | ) { |
| 92 | if (piPipeconf.extension(extensionType).isPresent()) { |
| 93 | result.add(extensionType.name()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return result.toString(); |
| 98 | } |
| 99 | } |