blob: b7fd93f0fcc45d5988f0fb8186e4a1f7fc261efa [file] [log] [blame]
Georgios Katsikas13ccba62020-03-18 12:05:03 +01001/*
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
17package org.onosproject.drivers.server;
18
19import org.onlab.packet.VlanId;
20import org.onosproject.drivers.server.devices.RestServerSBDevice;
21import org.onosproject.drivers.server.devices.nic.NicDevice;
22import org.onosproject.drivers.server.devices.nic.NicRxFilter.RxFilter;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.behaviour.InterfaceConfig;
25import org.onosproject.net.behaviour.PatchDescription;
26import org.onosproject.net.behaviour.TunnelDescription;
27import org.onosproject.net.device.DefaultDeviceInterfaceDescription;
28import org.onosproject.net.device.DeviceInterfaceDescription;
29
30import org.slf4j.Logger;
31
32import com.google.common.collect.Lists;
33import com.google.common.collect.ImmutableList;
34
35import java.util.Collections;
36import java.util.List;
37import java.util.Set;
38import java.util.TreeSet;
39
40import static com.google.common.base.Preconditions.checkNotNull;
41import static org.onosproject.drivers.server.Constants.MSG_DEVICE_NULL;
42import static org.onosproject.drivers.server.Constants.MSG_DEVICE_ID_NULL;
43import static org.onosproject.net.device.DeviceInterfaceDescription.Mode;
44import static org.slf4j.LoggerFactory.getLogger;
45
46/**
47 * Implementation of interface config behaviour for server devices.
48 */
49public 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}