blob: e37a34d19127d4e45fef5a99c70888e57ec931ce [file] [log] [blame]
Ray Milkeyc3aceff2016-11-18 12:58:23 -08001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
Luca Prete092e8952016-10-26 16:25:56 +020016package org.onosproject.vpls.cli;
17
18import com.google.common.collect.SetMultimap;
19import com.google.common.collect.Sets;
20import org.onosproject.incubator.net.intf.Interface;
21import org.onosproject.incubator.net.intf.InterfaceService;
22import org.onosproject.vpls.config.VplsConfigurationService;
23
24import java.util.Map;
25import java.util.Optional;
26import java.util.Set;
27
28import static org.onlab.osgi.DefaultServiceDirectory.getService;
29
30/**
31 * Util class for VPLS Commands.
32 */
33public final class VplsCommandUtils {
34
35 protected static final String VPLS_NAME = "VPLS name %s: ";
36 protected static final String VPLS_DISPLAY = "VPLS name: %s, associated " +
37 "interfaces: %s, encapsulation: %s";
38 protected static final String VPLS_NOT_FOUND = "VPLS %s not found.";
39 protected static final String IFACE_NOT_FOUND = "Interface %s not found.";
40 protected static final String VPLS_ALREADY_EXISTS = "VPLS %s already exists.";
41 protected static final String IFACE_ALREADY_ASSOCIATED =
42 "Interface %s already associated to VPLS %s.";
43 protected static final String IFACE_NOT_ASSOCIATED =
44 "Interface %s is associated to VPLS %s.";
45
46 private static VplsConfigurationService vplsConfigService =
47 getService(VplsConfigurationService.class);
48 private static InterfaceService interfaceService =
49 getService(InterfaceService.class);
50
51 private VplsCommandUtils() {}
52
53 /**
54 * States if a VPLS exists or not.
55 *
56 * @param vplsName the name of the VPLS
57 * @return true if the VPLS exists; false otherwise
58 */
59 protected static boolean vplsExists(String vplsName) {
60 return vplsConfigService.vplsNames().contains(vplsName);
61 }
62
63 /**
64 * States if an interface is defined or not in the system.
65 *
66 * @param ifaceName the name of the interface
67 * @return true if the interface is defined; false otherwise
68 */
69 protected static boolean ifaceExists(String ifaceName) {
70 return interfaceService.getInterfaces()
71 .stream()
72 .anyMatch(iface -> iface.name().equals(ifaceName));
73 }
74
75 /**
76 * States if an interface is already associated or not to a VPLS.
77 *
78 * @param ifaceName the name of the interface
79 * @return true if the interface is already associated to a VPLS; false
80 * otherwise
81 */
82 protected static boolean ifaceAlreadyAssociated(String ifaceName) {
83 return vplsConfigService.allIfaces()
84 .stream()
85 .anyMatch(iface -> iface.name().equals(ifaceName));
86 }
87
88 /**
89 * Returns the name of a VPLS, given the name of an interface associated to
90 * it.
91 *
92 * @param ifaceName the name of the interface
93 * @return the name of the VPLS that has the interface configured; null if
94 * the interface does not exist or is not associated to any VPLS
95 */
96 protected static String vplsNameFromIfaceName(String ifaceName) {
97 String vplsName = null;
98
99 Optional<String> optVplsName = vplsConfigService.ifacesByVplsName()
100 .entries()
101 .stream()
102 .filter(iface -> iface.getValue().name().equals(ifaceName))
103 .map(Map.Entry::getKey)
104 .findFirst();
105
106 if (optVplsName.isPresent()) {
107 vplsName = optVplsName.get();
108 }
109
110 return vplsName;
111 }
112
113 /**
114 * Returns a list of interfaces associated to a VPLS, given a VPLS name.
115 *
116 * @param vplsName the name of the VPLS
117 * @return the set of interfaces associated to the given VPLS; null if the
118 * VPLS is not found
119 */
120 protected static Set<String> ifacesFromVplsName(String vplsName) {
121 if (!vplsExists(vplsName)) {
122 return null;
123 }
124
125 SetMultimap<String, Interface> ifacesByVplsName =
126 vplsConfigService.ifacesByVplsName();
127 Set<String> ifaceNames = Sets.newHashSet();
128
129 ifacesByVplsName.get(vplsName).forEach(iface -> ifaceNames.add(iface.name()));
130
131 return ifaceNames;
132 }
133}