blob: 06240055ac3b44d5ed4d248d5a39926f131f9ba1 [file] [log] [blame]
Luca Pretedce16f82016-11-22 13:11:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Luca Pretedce16f82016-11-22 13:11:56 -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 */
16package org.onosproject.vpls.cli.completer;
17
18import com.google.common.collect.Lists;
Luca Pretedce16f82016-11-22 13:11:56 -080019import org.apache.karaf.shell.console.completer.ArgumentCompleter;
20import org.onosproject.cli.AbstractChoicesCompleter;
Ray Milkeyfacf2862017-08-03 11:58:29 -070021import org.onosproject.net.intf.Interface;
22import org.onosproject.net.intf.InterfaceService;
Luca Pretedce16f82016-11-22 13:11:56 -080023import org.onosproject.net.EncapsulationType;
Yi Tsengf4e13e32017-03-30 15:38:39 -070024import org.onosproject.vpls.api.Vpls;
25import org.onosproject.vpls.api.VplsData;
Luca Pretedce16f82016-11-22 13:11:56 -080026import org.onosproject.vpls.cli.VplsCommandEnum;
Luca Pretedce16f82016-11-22 13:11:56 -080027
28import java.util.Arrays;
Yi Tsengf4e13e32017-03-30 15:38:39 -070029import java.util.Collection;
Luca Pretedce16f82016-11-22 13:11:56 -080030import java.util.Collections;
31import java.util.List;
32import java.util.Set;
33import java.util.stream.Collectors;
34
35import static org.onosproject.cli.AbstractShellCommand.get;
36
37/**
38 * VPLS optional argument completer.
39 */
40public class VplsOptArgCompleter extends AbstractChoicesCompleter {
Yi Tsengf4e13e32017-03-30 15:38:39 -070041 protected static Vpls vpls;
42 protected static InterfaceService interfaceService;
Luca Pretedce16f82016-11-22 13:11:56 -080043
44 @Override
45 public List<String> choices() {
Yi Tsengf4e13e32017-03-30 15:38:39 -070046 if (vpls == null) {
47 vpls = get(Vpls.class);
48 }
49 ArgumentCompleter.ArgumentList argumentList = getArgumentList();
50 if (argumentList == null) {
51 return Collections.emptyList();
52 }
53 List<String> argList = Lists.newArrayList(argumentList.getArguments());
54 String argOne = argList.get(1);
Luca Pretedce16f82016-11-22 13:11:56 -080055 VplsCommandEnum vplsCommandEnum = VplsCommandEnum.enumFromString(argOne);
56 if (vplsCommandEnum != null) {
57 switch (vplsCommandEnum) {
58 case ADD_IFACE:
Yi Tsengf4e13e32017-03-30 15:38:39 -070059 return availableIfaces();
Luca Pretedce16f82016-11-22 13:11:56 -080060 case SET_ENCAP:
61 return encap();
62 case REMOVE_IFACE:
Yi Tsengf4e13e32017-03-30 15:38:39 -070063 return vplsIfaces();
Luca Pretedce16f82016-11-22 13:11:56 -080064 default:
65 return Collections.emptyList();
66 }
67 }
68 return Collections.emptyList();
69 }
70
71 /**
72 * Returns the list of interfaces not yet assigned to any VPLS.
73 *
74 * @return the list of interfaces not yet assigned to any VPLS
75 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070076 private List<String> availableIfaces() {
77 if (interfaceService == null) {
78 interfaceService = get(InterfaceService.class);
79 }
Luca Pretedce16f82016-11-22 13:11:56 -080080
Yi Tsengf4e13e32017-03-30 15:38:39 -070081 Set<Interface> allIfaces = interfaceService.getInterfaces();
82 Set<Interface> usedIfaces = vpls.getAllVpls().stream()
83 .map(VplsData::interfaces)
84 .flatMap(Collection::stream)
85 .collect(Collectors.toSet());
Luca Pretedce16f82016-11-22 13:11:56 -080086
Yi Tsengf4e13e32017-03-30 15:38:39 -070087 return allIfaces.stream()
88 .filter(iface -> !usedIfaces.contains(iface))
89 .map(Interface::name)
90 .collect(Collectors.toList());
Luca Pretedce16f82016-11-22 13:11:56 -080091 }
92
93 /**
94 * Returns the list of supported encapsulation types.
95 *
Yi Tsengf4e13e32017-03-30 15:38:39 -070096 * @return the list of supported encapsulation types
Luca Pretedce16f82016-11-22 13:11:56 -080097 */
98 private List<String> encap() {
99 return Arrays.stream(EncapsulationType.values())
100 .map(Enum::toString)
101 .collect(Collectors.toList());
102 }
103
104 /**
105 * Returns the list of interfaces associated to a VPLS.
106 *
107 * @return the list of interfaces associated to a VPLS
108 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700109 private List<String> vplsIfaces() {
Luca Pretedce16f82016-11-22 13:11:56 -0800110 ArgumentCompleter.ArgumentList list = getArgumentList();
111 String vplsName = list.getArguments()[2];
Yi Tsengf4e13e32017-03-30 15:38:39 -0700112 VplsData vplsData = vpls.getVpls(vplsName);
113 return vplsData.interfaces().stream()
114 .map(Interface::name)
115 .collect(Collectors.toList());
Luca Pretedce16f82016-11-22 13:11:56 -0800116 }
117}