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