uni show and list commands
[unimgr.git] / cli / src / main / java / org / opendaylight / unimgr / cli / UniListShellCommand.java
1 /*
2  * Copyright (c) 2015 Inocybe and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.unimgr.cli;
10
11 import java.util.List;
12
13 import org.apache.karaf.shell.commands.Command;
14 import org.apache.karaf.shell.commands.Option;
15 import org.apache.karaf.shell.console.OsgiCommandSupport;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.unimgr.api.IUnimgrConsoleProvider;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.Uni;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
20
21 @Command(name = "uni-list", scope = "uni", description = "Lists all uni in the controller.")
22 public class UniListShellCommand extends OsgiCommandSupport {
23
24     protected IUnimgrConsoleProvider provider;
25
26     @Option(name = "-c",
27             aliases = { "--config" },
28             description = "List Configuration Data (optional).\n-c / --config <ENTER>",
29             required = false,
30             multiValued = false)
31     Boolean isConfigurationData = false;
32
33     public UniListShellCommand(IUnimgrConsoleProvider provider) {
34         this.provider = provider;
35     }
36
37     @Override
38     protected Object doExecute() throws Exception {
39
40         LogicalDatastoreType storeType = isConfigurationData ? LogicalDatastoreType.CONFIGURATION : LogicalDatastoreType.OPERATIONAL; 
41         List<UniAugmentation> listUnis = provider.listUnis(storeType);
42
43         if (listUnis.size() > 0) {
44             StringBuilder sb = new StringBuilder();
45             Integer counter = 1;
46             for (UniAugmentation uni : listUnis) {
47                 // TODO
48                 sb.append(String.format("#%d - IpAddress: %s\n", counter, uni.getIpAddress().getIpv4Address().getValue()));
49                 counter++;
50             }
51             return sb.toString();
52         } else {
53             return String.format("No uni found. Check the logs for more details.");
54         }
55     }
56 }