blob: c44ef40e7a178c24ecf585ee05682b5dbeb95f74 [file] [log] [blame]
Harold Huang652ea832017-03-18 22:59:44 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Harold Huang652ea832017-03-18 22:59:44 +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
17package org.onosproject.cli.net.vnet;
18
19import org.apache.karaf.shell.console.completer.ArgumentCompleter.ArgumentList;
20import org.onosproject.cli.AbstractChoicesCompleter;
21import org.onosproject.incubator.net.virtual.NetworkId;
22import org.onosproject.incubator.net.virtual.VirtualHost;
23import org.onosproject.incubator.net.virtual.VirtualNetworkService;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.List;
28import java.util.stream.Collectors;
29
30import static org.onlab.osgi.DefaultServiceDirectory.getService;
31
32/**
33 * Virtual host completer.
34 *
35 * Assumes the first argument which can be parsed to a number is network id.
36 */
37public class VirtualHostCompleter extends AbstractChoicesCompleter {
38 @Override
39 protected List<String> choices() {
40 ArgumentList args = getArgumentList();
41 //parse argument list for network id
42 String[] argsArray = args.getArguments();
43 for (String str : argsArray) {
44 if (str.matches("[0-9]+")) {
45 long networkId = Long.valueOf(str);
46 return getSortedVirtualHosts(networkId).stream()
47 .map(virtualHost -> virtualHost.id().toString())
48 .collect(Collectors.toList());
49 }
50 }
51 return Collections.singletonList("Missing network id");
52 }
53
54 /**
55 * Returns the list of virtual hosts sorted using the host identifier.
56 *
57 * @param networkId network id
58 * @return virtual host list
59 */
60 private List<VirtualHost> getSortedVirtualHosts(long networkId) {
61 VirtualNetworkService service = getService(VirtualNetworkService.class);
62
63 List<VirtualHost> virtualHosts = new ArrayList<>();
64 virtualHosts.addAll(service.getVirtualHosts(NetworkId.networkId(networkId)));
65 return virtualHosts;
66 }
67
68}