Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 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 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.server; |
| 18 | |
| 19 | import org.onlab.packet.VlanId; |
| 20 | import org.onosproject.drivers.server.devices.RestServerSBDevice; |
| 21 | import org.onosproject.drivers.server.devices.nic.NicDevice; |
| 22 | import org.onosproject.drivers.server.devices.nic.NicRxFilter.RxFilter; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.behaviour.InterfaceConfig; |
| 25 | import org.onosproject.net.behaviour.PatchDescription; |
| 26 | import org.onosproject.net.behaviour.TunnelDescription; |
| 27 | import org.onosproject.net.device.DefaultDeviceInterfaceDescription; |
| 28 | import org.onosproject.net.device.DeviceInterfaceDescription; |
| 29 | |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import com.google.common.collect.Lists; |
| 33 | import com.google.common.collect.ImmutableList; |
| 34 | |
| 35 | import java.util.Collections; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | import java.util.TreeSet; |
| 39 | |
| 40 | import static com.google.common.base.Preconditions.checkNotNull; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 41 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL; |
Georgios Katsikas | 1a64c3a | 2020-06-03 17:30:43 +0200 | [diff] [blame^] | 42 | import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL; |
Georgios Katsikas | 13ccba6 | 2020-03-18 12:05:03 +0100 | [diff] [blame] | 43 | import static org.onosproject.net.device.DeviceInterfaceDescription.Mode; |
| 44 | import static org.slf4j.LoggerFactory.getLogger; |
| 45 | |
| 46 | /** |
| 47 | * Implementation of interface config behaviour for server devices. |
| 48 | */ |
| 49 | public class ServerInterfaceConfig |
| 50 | extends BasicServerDriver |
| 51 | implements InterfaceConfig { |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | private static final boolean RATE_LIMIT_STATUS = false; |
| 56 | private static final short NO_LIMIT = -1; |
| 57 | |
| 58 | public ServerInterfaceConfig() { |
| 59 | super(); |
| 60 | log.debug("Started"); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean addTunnelMode(String ifaceName, TunnelDescription tunnelDesc) { |
| 65 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean removeTunnelMode(String ifaceName) { |
| 70 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean addAccessMode(String ifaceName, VlanId vlanId) { |
| 75 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean removeAccessMode(String ifaceName) { |
| 80 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean addPatchMode(String ifaceName, PatchDescription patchDesc) { |
| 85 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean removePatchMode(String ifaceName) { |
| 90 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean addTrunkMode(String ifaceName, List<VlanId> vlanIds) { |
| 95 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public boolean removeTrunkMode(String ifaceName) { |
| 100 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public boolean addRateLimit(String ifaceName, short limit) { |
| 105 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public boolean removeRateLimit(String ifaceName) { |
| 110 | throw new UnsupportedOperationException("Interface operation not supported"); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public List<DeviceInterfaceDescription> getInterfaces() { |
| 115 | // Retrieve the device ID |
| 116 | DeviceId deviceId = getDeviceId(); |
| 117 | checkNotNull(deviceId, MSG_DEVICE_ID_NULL); |
| 118 | |
| 119 | // .. and the device itself |
| 120 | RestServerSBDevice device = null; |
| 121 | try { |
| 122 | device = (RestServerSBDevice) getDevice(deviceId); |
| 123 | } catch (ClassCastException ccEx) { |
| 124 | log.error("Failed to get interfaces for device {}", deviceId); |
| 125 | return Collections.EMPTY_LIST; |
| 126 | } |
| 127 | |
| 128 | if (device == null) { |
| 129 | log.error("No device with ID {} is available for interface discovery", deviceId); |
| 130 | return Collections.EMPTY_LIST; |
| 131 | } |
| 132 | if ((device.nics() == null) || (device.nics().size() == 0)) { |
| 133 | log.error("No interfaces available on {}", deviceId); |
| 134 | return Collections.EMPTY_LIST; |
| 135 | } |
| 136 | |
| 137 | // List of port descriptions to return |
| 138 | List<DeviceInterfaceDescription> intfDescriptions = Lists.newArrayList(); |
| 139 | |
| 140 | // Sorted list of NIC ports |
| 141 | Set<NicDevice> nics = new TreeSet(device.nics()); |
| 142 | |
| 143 | // Iterate through the NICs of this device to populate the list |
| 144 | for (NicDevice nic : nics) { |
| 145 | List<VlanId> devVlanIds = getVlanIdListForDevice(nic); |
| 146 | Mode devMode = getDeviceMode(devVlanIds); |
| 147 | |
| 148 | // Create an interface description and add it to the list |
| 149 | intfDescriptions.add( |
| 150 | new DefaultDeviceInterfaceDescription( |
| 151 | nic.name(), devMode, devVlanIds, RATE_LIMIT_STATUS, NO_LIMIT)); |
| 152 | } |
| 153 | |
| 154 | return ImmutableList.copyOf(intfDescriptions); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Returns a list of VLAN IDs associated with a NIC device. |
| 159 | * |
| 160 | * @param nic the NIC device to be queried |
| 161 | * @return a list of VLAN IDs (if any) |
| 162 | */ |
| 163 | private List<VlanId> getVlanIdListForDevice(NicDevice nic) { |
| 164 | checkNotNull(nic, MSG_DEVICE_NULL); |
| 165 | List<VlanId> vlanIds = Lists.newArrayList(); |
| 166 | |
| 167 | short vlanIdValue = 0; |
| 168 | for (RxFilter rxFilter : nic.rxFilterMechanisms().rxFilters()) { |
| 169 | if (rxFilter == RxFilter.VLAN) { |
| 170 | vlanIds.add(VlanId.vlanId(vlanIdValue++)); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return ImmutableList.copyOf(vlanIds); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the interface mode of a NIC device, by looking at its VLAN IDs. |
| 179 | * |
| 180 | * @param vlanIds a list of VLAN IDs associated with the device |
| 181 | * @return interface mode |
| 182 | */ |
| 183 | private Mode getDeviceMode(List<VlanId> vlanIds) { |
| 184 | if (vlanIds.size() == 1) { |
| 185 | return Mode.ACCESS; |
| 186 | } else if (vlanIds.size() > 1) { |
| 187 | return Mode.TRUNK; |
| 188 | } |
| 189 | |
| 190 | return Mode.NORMAL; |
| 191 | } |
| 192 | |
| 193 | } |