alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | d6a135a | 2017-08-03 22:46:05 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 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 | |
alshabib | 36a4d73 | 2016-06-01 16:03:59 -0700 | [diff] [blame] | 17 | package org.opencord.olt.cli; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 18 | |
Andrea Campanella | d3cad47 | 2020-03-27 10:24:13 +0100 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 22 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Carmelo Cascone | ca93116 | 2019-07-15 18:22:24 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.Command; |
| 24 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
Andrea Campanella | d3cad47 | 2020-03-27 10:24:13 +0100 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
Amit Ghosh | 1ed9aef | 2018-07-17 17:08:16 +0100 | [diff] [blame] | 27 | import org.opencord.olt.AccessDeviceService; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 28 | |
Andrea Campanella | d3cad47 | 2020-03-27 10:24:13 +0100 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 31 | /** |
Jonathan Hart | fd6c1b3 | 2016-03-08 14:09:09 -0800 | [diff] [blame] | 32 | * Shows configured OLTs. |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 33 | */ |
Carmelo Cascone | ca93116 | 2019-07-15 18:22:24 -0700 | [diff] [blame] | 34 | @Service |
Saurav Das | 82b8e6d | 2018-10-04 15:25:12 -0700 | [diff] [blame] | 35 | @Command(scope = "onos", name = "volt-olts", |
| 36 | description = "Shows vOLTs connected to ONOS") |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 37 | public class ShowOltCommand extends AbstractShellCommand { |
| 38 | |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 39 | @Override |
Carmelo Cascone | ca93116 | 2019-07-15 18:22:24 -0700 | [diff] [blame] | 40 | protected void doExecute() { |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 41 | AccessDeviceService service = AbstractShellCommand.get(AccessDeviceService.class); |
Andrea Campanella | d3cad47 | 2020-03-27 10:24:13 +0100 | [diff] [blame] | 42 | 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; |
alshabib | e055967 | 2016-02-21 14:49:51 -0800 | [diff] [blame] | 64 | } |
| 65 | } |