blob: 08aaa4876316afcf179033150efe5d02d2bc15bf [file] [log] [blame]
alshabibe0559672016-02-21 14:49:51 -08001/*
Brian O'Connord6a135a2017-08-03 22:46:05 -07002 * Copyright 2016-present Open Networking Foundation
alshabibe0559672016-02-21 14:49:51 -08003 *
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
alshabib36a4d732016-06-01 16:03:59 -070017package org.opencord.olt.cli;
alshabibe0559672016-02-21 14:49:51 -080018
Andrea Campanellad3cad472020-03-27 10:24:13 +010019import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ArrayNode;
22import com.fasterxml.jackson.databind.node.ObjectNode;
Carmelo Casconeca931162019-07-15 18:22:24 -070023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
alshabibe0559672016-02-21 14:49:51 -080025import org.onosproject.cli.AbstractShellCommand;
Andrea Campanellad3cad472020-03-27 10:24:13 +010026import org.onosproject.net.DeviceId;
Amit Ghosh1ed9aef2018-07-17 17:08:16 +010027import org.opencord.olt.AccessDeviceService;
alshabibe0559672016-02-21 14:49:51 -080028
Andrea Campanellad3cad472020-03-27 10:24:13 +010029import java.util.List;
30
alshabibe0559672016-02-21 14:49:51 -080031/**
Jonathan Hartfd6c1b32016-03-08 14:09:09 -080032 * Shows configured OLTs.
alshabibe0559672016-02-21 14:49:51 -080033 */
Carmelo Casconeca931162019-07-15 18:22:24 -070034@Service
Saurav Das82b8e6d2018-10-04 15:25:12 -070035@Command(scope = "onos", name = "volt-olts",
36 description = "Shows vOLTs connected to ONOS")
alshabibe0559672016-02-21 14:49:51 -080037public class ShowOltCommand extends AbstractShellCommand {
38
alshabibe0559672016-02-21 14:49:51 -080039 @Override
Carmelo Casconeca931162019-07-15 18:22:24 -070040 protected void doExecute() {
alshabibe0559672016-02-21 14:49:51 -080041 AccessDeviceService service = AbstractShellCommand.get(AccessDeviceService.class);
Andrea Campanellad3cad472020-03-27 10:24:13 +010042 if (outputJson()) {
43 print("%s", json(service.fetchOlts()));
44 } else {
45 service.fetchOlts().forEach(did -> print("OLT %s", did));
46 }
47
48 }
49
50 /**
51 * Returns JSON node representing the specified olts.
52 *
53 * @param olts collection of olts
54 * @return JSON node
55 */
56 private JsonNode json(List<DeviceId> olts) {
57 ObjectMapper mapper = new ObjectMapper();
58 ObjectNode node = mapper.createObjectNode();
59 ArrayNode result = node.putArray("olts");
60 for (DeviceId olt : olts) {
61 result.add(olt.toString());
62 }
63 return node;
alshabibe0559672016-02-21 14:49:51 -080064 }
65}