blob: c33536954783ddffa2fa8b8ef0e37833682d7ca6 [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.lifecycle.Service;
Luca Pretedce16f82016-11-22 13:11:56 -080019import org.onosproject.cli.AbstractChoicesCompleter;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.onosproject.net.EncapsulationType;
Ray Milkeyfacf2862017-08-03 11:58:29 -070021import org.onosproject.net.intf.Interface;
22import org.onosproject.net.intf.InterfaceService;
Yi Tsengf4e13e32017-03-30 15:38:39 -070023import org.onosproject.vpls.api.Vpls;
24import org.onosproject.vpls.api.VplsData;
Luca Pretedce16f82016-11-22 13:11:56 -080025import org.onosproject.vpls.cli.VplsCommandEnum;
Luca Pretedce16f82016-11-22 13:11:56 -080026
27import java.util.Arrays;
Yi Tsengf4e13e32017-03-30 15:38:39 -070028import java.util.Collection;
Luca Pretedce16f82016-11-22 13:11:56 -080029import java.util.Collections;
30import java.util.List;
31import java.util.Set;
32import java.util.stream.Collectors;
33
34import static org.onosproject.cli.AbstractShellCommand.get;
35
36/**
37 * VPLS optional argument completer.
38 */
Ray Milkey86ad7bb2018-09-27 12:32:28 -070039@Service
Luca Pretedce16f82016-11-22 13:11:56 -080040public class VplsOptArgCompleter extends AbstractChoicesCompleter {
Ray Milkey06297ed2018-01-22 17:13:41 -080041 protected Vpls vpls;
42 protected 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 }
Ray Milkey86ad7bb2018-09-27 12:32:28 -070049 String[] argList = commandLine.getArguments();
50 if (argList == null) {
Yi Tsengf4e13e32017-03-30 15:38:39 -070051 return Collections.emptyList();
52 }
Ray Milkey86ad7bb2018-09-27 12:32:28 -070053 String argOne = argList[1];
Luca Pretedce16f82016-11-22 13:11:56 -080054 VplsCommandEnum vplsCommandEnum = VplsCommandEnum.enumFromString(argOne);
55 if (vplsCommandEnum != null) {
56 switch (vplsCommandEnum) {
57 case ADD_IFACE:
Yi Tsengf4e13e32017-03-30 15:38:39 -070058 return availableIfaces();
Luca Pretedce16f82016-11-22 13:11:56 -080059 case SET_ENCAP:
60 return encap();
61 case REMOVE_IFACE:
Yi Tsengf4e13e32017-03-30 15:38:39 -070062 return vplsIfaces();
Luca Pretedce16f82016-11-22 13:11:56 -080063 default:
64 return Collections.emptyList();
65 }
66 }
67 return Collections.emptyList();
68 }
69
70 /**
71 * Returns the list of interfaces not yet assigned to any VPLS.
72 *
73 * @return the list of interfaces not yet assigned to any VPLS
74 */
Yi Tsengf4e13e32017-03-30 15:38:39 -070075 private List<String> availableIfaces() {
76 if (interfaceService == null) {
77 interfaceService = get(InterfaceService.class);
78 }
Luca Pretedce16f82016-11-22 13:11:56 -080079
Yi Tsengf4e13e32017-03-30 15:38:39 -070080 Set<Interface> allIfaces = interfaceService.getInterfaces();
81 Set<Interface> usedIfaces = vpls.getAllVpls().stream()
82 .map(VplsData::interfaces)
83 .flatMap(Collection::stream)
84 .collect(Collectors.toSet());
Luca Pretedce16f82016-11-22 13:11:56 -080085
Yi Tsengf4e13e32017-03-30 15:38:39 -070086 return allIfaces.stream()
87 .filter(iface -> !usedIfaces.contains(iface))
88 .map(Interface::name)
89 .collect(Collectors.toList());
Luca Pretedce16f82016-11-22 13:11:56 -080090 }
91
92 /**
93 * Returns the list of supported encapsulation types.
94 *
Yi Tsengf4e13e32017-03-30 15:38:39 -070095 * @return the list of supported encapsulation types
Luca Pretedce16f82016-11-22 13:11:56 -080096 */
97 private List<String> encap() {
98 return Arrays.stream(EncapsulationType.values())
99 .map(Enum::toString)
100 .collect(Collectors.toList());
101 }
102
103 /**
104 * Returns the list of interfaces associated to a VPLS.
105 *
106 * @return the list of interfaces associated to a VPLS
107 */
Yi Tsengf4e13e32017-03-30 15:38:39 -0700108 private List<String> vplsIfaces() {
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700109 String vplsName = commandLine.getArguments()[2];
Yi Tsengf4e13e32017-03-30 15:38:39 -0700110 VplsData vplsData = vpls.getVpls(vplsName);
111 return vplsData.interfaces().stream()
112 .map(Interface::name)
113 .collect(Collectors.toList());
Luca Pretedce16f82016-11-22 13:11:56 -0800114 }
115}