blob: 8753a682892efab42a102ee79a0a078dc59db3c2 [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
Ray Milkey956bb162018-10-26 10:53:44 -070020import org.apache.karaf.shell.api.action.Command;
21import org.apache.karaf.shell.api.action.Option;
Carmelo Casconea46f5542018-12-12 23:41:01 -080022import org.apache.karaf.shell.api.action.lifecycle.Service;
ghj05045208b252d22018-10-18 10:16:22 -070023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.driver.Behaviour;
25import org.onosproject.net.pi.model.PiPipeconf;
26import org.onosproject.net.pi.service.PiPipeconfService;
27
28import java.util.ArrayList;
29import java.util.Collection;
30
31
32/**
33 * Query registered pipeconfs.
34 */
Carmelo Casconea46f5542018-12-12 23:41:01 -080035@Service
ghj05045208b252d22018-10-18 10:16:22 -070036@Command(scope = "onos", name = "pipeconfs",
Carmelo Casconea46f5542018-12-12 23:41:01 -080037 description = "List registered pipeconfs")
ghj05045208b252d22018-10-18 10:16:22 -070038public 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 Milkey956bb162018-10-26 10:53:44 -070048 protected void doExecute() {
ghj05045208b252d22018-10-18 10:16:22 -070049 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}