blob: dec2a350bca1a20b1ecd344315315f764f116ad9 [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
Harold Huang652ea832017-03-18 22:59:44 +080019import org.onosproject.cli.AbstractChoicesCompleter;
20import org.onosproject.incubator.net.virtual.NetworkId;
21import org.onosproject.incubator.net.virtual.VirtualHost;
22import org.onosproject.incubator.net.virtual.VirtualNetworkService;
23
24import java.util.ArrayList;
25import java.util.Collections;
26import java.util.List;
27import java.util.stream.Collectors;
28
29import static org.onlab.osgi.DefaultServiceDirectory.getService;
30
31/**
32 * Virtual host completer.
33 *
34 * Assumes the first argument which can be parsed to a number is network id.
35 */
36public class VirtualHostCompleter extends AbstractChoicesCompleter {
37 @Override
38 protected List<String> choices() {
Harold Huang652ea832017-03-18 22:59:44 +080039 //parse argument list for network id
Ray Milkeyd84f89b2018-08-17 14:54:17 -070040 String[] argsArray = commandLine.getArguments();
Harold Huang652ea832017-03-18 22:59:44 +080041 for (String str : argsArray) {
42 if (str.matches("[0-9]+")) {
43 long networkId = Long.valueOf(str);
44 return getSortedVirtualHosts(networkId).stream()
45 .map(virtualHost -> virtualHost.id().toString())
46 .collect(Collectors.toList());
47 }
48 }
49 return Collections.singletonList("Missing network id");
50 }
51
52 /**
53 * Returns the list of virtual hosts sorted using the host identifier.
54 *
55 * @param networkId network id
56 * @return virtual host list
57 */
58 private List<VirtualHost> getSortedVirtualHosts(long networkId) {
59 VirtualNetworkService service = getService(VirtualNetworkService.class);
60
61 List<VirtualHost> virtualHosts = new ArrayList<>();
62 virtualHosts.addAll(service.getVirtualHosts(NetworkId.networkId(networkId)));
63 return virtualHosts;
64 }
65
66}