blob: 113b2717cbf7a66fca227c03de0f6df70275d17d [file] [log] [blame]
Ray Milkey1aaff022018-10-22 16:45:20 -07001/*
2 * Copyright 2017-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 */
16package org.onosproject.cli;
17
18import org.apache.karaf.shell.api.action.Command;
19import org.apache.karaf.shell.api.action.lifecycle.Service;
20import org.osgi.service.component.runtime.ServiceComponentRuntime;
21import org.osgi.service.component.runtime.dto.ComponentConfigurationDTO;
22
23import java.util.Formatter;
24
25/**
26 * CLI command to dump out the SCR components list in the old karaf format.
27 */
28@Service
29@Command(scope = "onos", name = "scr-list",
30 description = "List components")
31public class ScrListCommand extends AbstractShellCommand {
32
33 @Override
34 protected void doExecute() {
35 StringBuilder output = new StringBuilder();
36 Formatter formatter = new Formatter(output);
37 ServiceComponentRuntime scrService = get(ServiceComponentRuntime.class);
38
39 scrService.getComponentDescriptionDTOs()
40 .forEach(componentDto -> {
Ray Milkey9d984df2019-01-04 11:27:51 -080041 try {
42 scrService.getComponentConfigurationDTOs(componentDto)
43 .forEach(configurationDto -> {
44 String state;
45 switch (configurationDto.state) {
46 case ComponentConfigurationDTO.ACTIVE: {
47 state = "ACTIVE";
48 break;
49 }
50 case ComponentConfigurationDTO.SATISFIED: {
51 state = "SATISFIED";
52 break;
53 }
54 default: {
55 state = "UNKNOWN";
56 }
Ray Milkey1aaff022018-10-22 16:45:20 -070057 }
Ray Milkey9d984df2019-01-04 11:27:51 -080058 formatter.format("%3d | %9s | %s\n", configurationDto.id, state, componentDto.name);
59 });
60 } catch (NullPointerException npe) {
61 // Work around for a race condition inside of the SCR runtime.
62 // In certain conditions, the SCR code gets an NPE due to a
63 // null bundle pointer
64 // Nothing we can do with the data, skip this entry
65 }
66 });
Ray Milkey1aaff022018-10-22 16:45:20 -070067 print(output.toString());
68 }
69
70}