blob: 91832dc820c1a4f1d22e59773769980cd380112e [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jonathan Harteb8c9472015-08-05 07:43:13 -07003 *
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.incubator.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070022import com.google.common.annotations.Beta;
Charles Chanbae2cb22017-01-18 19:35:20 -080023import com.google.common.collect.ImmutableSet;
Jonathan Hart00cddda2016-02-16 10:30:37 -080024import com.google.common.collect.Lists;
Jonathan Harteb8c9472015-08-05 07:43:13 -070025import com.google.common.collect.Sets;
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080026import org.onlab.packet.IpPrefix;
Jonathan Harteb8c9472015-08-05 07:43:13 -070027import org.onlab.packet.MacAddress;
28import org.onlab.packet.VlanId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070029import org.onosproject.incubator.net.intf.Interface;
30import org.onosproject.net.ConnectPoint;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070031import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070032import org.onosproject.net.host.InterfaceIpAddress;
Charles Chanbae2cb22017-01-18 19:35:20 -080033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jonathan Harteb8c9472015-08-05 07:43:13 -070035
Jonathan Hart7dbe6292015-11-10 08:33:17 -080036import java.util.Iterator;
Jonathan Hart00cddda2016-02-16 10:30:37 -080037import java.util.List;
Jonathan Harteb8c9472015-08-05 07:43:13 -070038import java.util.Set;
39
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070040import static com.google.common.base.Preconditions.checkArgument;
41import static com.google.common.base.Preconditions.checkNotNull;
42
Jonathan Harteb8c9472015-08-05 07:43:13 -070043/**
44 * Configuration for interfaces.
45 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070046@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070047public class InterfaceConfig extends Config<ConnectPoint> {
Charles Chanbae2cb22017-01-18 19:35:20 -080048 private static Logger log = LoggerFactory.getLogger(InterfaceConfig.class);
49
Jonathan Hart7dbe6292015-11-10 08:33:17 -080050 public static final String NAME = "name";
Jonathan Harteb8c9472015-08-05 07:43:13 -070051 public static final String IPS = "ips";
52 public static final String MAC = "mac";
53 public static final String VLAN = "vlan";
Charles Chanbae2cb22017-01-18 19:35:20 -080054 public static final String VLAN_UNTAGGED = "vlan-untagged";
55 public static final String VLAN_TAGGED = "vlan-tagged";
56 public static final String VLAN_NATIVE = "vlan-native";
Jonathan Harteb8c9472015-08-05 07:43:13 -070057
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070058 private static final String CONFIG_VALUE_ERROR = "Error parsing config value";
59 private static final String INTF_NULL_ERROR = "Interface cannot be null";
60 private static final String INTF_NAME_ERROR = "Interface must have a valid name";
Jonathan Hart4cb39882015-08-12 23:50:55 -040061
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080062 @Override
63 public boolean isValid() {
64 for (JsonNode node : array) {
Charles Chanbae2cb22017-01-18 19:35:20 -080065 if (!hasOnlyFields((ObjectNode) node, NAME, IPS, MAC, VLAN,
66 VLAN_UNTAGGED, VLAN_TAGGED, VLAN_NATIVE)) {
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080067 return false;
68 }
69
70 ObjectNode obj = (ObjectNode) node;
71
72 if (!(isString(obj, NAME, FieldPresence.OPTIONAL) &&
73 isMacAddress(obj, MAC, FieldPresence.OPTIONAL) &&
Charles Chanbae2cb22017-01-18 19:35:20 -080074 isIntegralNumber(obj, VLAN, FieldPresence.OPTIONAL, 0, VlanId.MAX_VLAN) &&
75 isIntegralNumber(obj, VLAN_UNTAGGED, FieldPresence.OPTIONAL, 0, VlanId.MAX_VLAN) &&
76 isIntegralNumber(obj, VLAN_NATIVE, FieldPresence.OPTIONAL, 0, VlanId.MAX_VLAN))) {
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080077 return false;
78 }
79
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080080 for (JsonNode ipNode : node.path(IPS)) {
81 if (!ipNode.isTextual() || IpPrefix.valueOf(ipNode.asText()) == null) {
82 return false;
83 }
84 }
Charles Chanbae2cb22017-01-18 19:35:20 -080085
86 checkArgument(!hasField(obj, VLAN_TAGGED) ||
87 (node.path(VLAN_TAGGED).isArray() && node.path(VLAN_TAGGED).size() >= 1),
88 "%s must be an array with at least one element", VLAN_TAGGED);
89
90 for (JsonNode vlanNode : node.path(VLAN_TAGGED)) {
91 checkArgument(vlanNode.isInt() &&
92 vlanNode.intValue() >= 0 && vlanNode.intValue() <= VlanId.MAX_VLAN,
93 "Invalid VLAN ID %s in %s", vlanNode.intValue(), VLAN_TAGGED);
94 }
95
96 checkArgument(!hasField(obj, VLAN_UNTAGGED) ||
97 !(hasField(obj, VLAN_TAGGED) || hasField(obj, VLAN_NATIVE)),
98 "%s and %s should not be used when %s is set", VLAN_TAGGED, VLAN_NATIVE, VLAN_UNTAGGED);
99
100 checkArgument(!hasField(obj, VLAN_TAGGED) || !hasField(obj, VLAN_UNTAGGED),
101 "%s should not be used when %s is set", VLAN_UNTAGGED, VLAN_TAGGED);
102
103 checkArgument(!hasField(obj, VLAN_NATIVE) || hasField(obj, VLAN_TAGGED),
104 "%s should not be used alone without %s", VLAN_NATIVE, VLAN_TAGGED);
105
106 checkArgument(!hasField(obj, VLAN_NATIVE) || !hasField(obj, VLAN_TAGGED) ||
107 !getVlans(obj, VLAN_TAGGED).contains(getVlan(obj, VLAN_NATIVE)),
108 "%s cannot be one of the VLANs configured in %s", VLAN_NATIVE, VLAN_TAGGED);
Jonathan Hart9eb45bb2016-02-12 10:59:11 -0800109 }
110 return true;
111 }
112
Jonathan Harteb8c9472015-08-05 07:43:13 -0700113 /**
114 * Retrieves all interfaces configured on this port.
115 *
116 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -0400117 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -0700118 */
Jonathan Hart4cb39882015-08-12 23:50:55 -0400119 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -0700120 Set<Interface> interfaces = Sets.newHashSet();
121
Jonathan Hart4cb39882015-08-12 23:50:55 -0400122 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -0700123 for (JsonNode intfNode : array) {
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800124 String name = intfNode.path(NAME).asText(null);
125
Jonathan Hart00cddda2016-02-16 10:30:37 -0800126 List<InterfaceIpAddress> ips = getIps(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400127
Luca Pretee3879f72015-10-16 11:19:53 +0200128 String mac = intfNode.path(MAC).asText();
129 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400130
Charles Chanbae2cb22017-01-18 19:35:20 -0800131 VlanId vlan = getVlan(intfNode, VLAN);
132 VlanId vlanUntagged = getVlan(intfNode, VLAN_UNTAGGED);
133 Set<VlanId> vlanTagged = getVlans(intfNode, VLAN_TAGGED);
134 VlanId vlanNative = getVlan(intfNode, VLAN_NATIVE);
Jonathan Hart4cb39882015-08-12 23:50:55 -0400135
Charles Chanbae2cb22017-01-18 19:35:20 -0800136 interfaces.add(new Interface(name, subject, ips, macAddr, vlan,
137 vlanUntagged, vlanTagged, vlanNative));
Jonathan Hart4cb39882015-08-12 23:50:55 -0400138 }
139 } catch (IllegalArgumentException e) {
140 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700141 }
142
143 return interfaces;
144 }
145
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700146 /**
147 * Adds an interface to the config.
148 *
149 * @param intf interface to add
150 */
151 public void addInterface(Interface intf) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700152 checkNotNull(intf, INTF_NULL_ERROR);
153 checkArgument(!intf.name().equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
154
155 // Remove old interface with this name if it exists
156 removeInterface(intf.name());
157
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700158 ObjectNode intfNode = array.addObject();
Luca Pretee3879f72015-10-16 11:19:53 +0200159
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800160 intfNode.put(NAME, intf.name());
161
Luca Pretee3879f72015-10-16 11:19:53 +0200162 if (intf.mac() != null) {
163 intfNode.put(MAC, intf.mac().toString());
164 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700165
Charles Chanbae2cb22017-01-18 19:35:20 -0800166 if (!intf.ipAddressesList().isEmpty()) {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800167 intfNode.set(IPS, putIps(intf.ipAddressesList()));
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700168 }
169
170 if (!intf.vlan().equals(VlanId.NONE)) {
171 intfNode.put(VLAN, intf.vlan().toString());
172 }
Charles Chanbae2cb22017-01-18 19:35:20 -0800173
174 if (!intf.vlanUntagged().equals(VlanId.NONE)) {
175 intfNode.put(VLAN_UNTAGGED, intf.vlanUntagged().toString());
176 }
177
178 if (!intf.vlanTagged().isEmpty()) {
179 intfNode.set(VLAN_UNTAGGED, putVlans(intf.vlanTagged()));
180 }
181
182 if (!intf.vlanNative().equals(VlanId.NONE)) {
183 intfNode.put(VLAN_NATIVE, intf.vlanNative().toString());
184 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700185 }
186
187 /**
188 * Removes an interface from the config.
189 *
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800190 * @param name name of the interface to remove
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700191 */
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800192 public void removeInterface(String name) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700193 checkNotNull(name, INTF_NULL_ERROR);
194 checkArgument(!name.equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
195
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800196 Iterator<JsonNode> it = array.iterator();
197 while (it.hasNext()) {
198 JsonNode node = it.next();
199 if (node.path(NAME).asText().equals(name)) {
200 it.remove();
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700201 break;
202 }
203 }
204 }
205
Charles Chanbae2cb22017-01-18 19:35:20 -0800206 /**
207 * Extracts VLAN ID from given path of given json node.
208 *
209 * @param node JSON node
210 * @param path path
211 * @return VLAN ID
212 */
213 private VlanId getVlan(JsonNode node, String path) {
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700214 VlanId vlan = VlanId.NONE;
Charles Chanbae2cb22017-01-18 19:35:20 -0800215 if (!node.path(path).isMissingNode()) {
216 vlan = VlanId.vlanId(Short.valueOf(node.path(path).asText()));
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700217 }
218 return vlan;
219 }
220
Charles Chanbae2cb22017-01-18 19:35:20 -0800221 /**
222 * Extracts a set of VLAN ID from given path of given json node.
223 *
224 * @param node JSON node
225 * @param path path
226 * @return a set of VLAN ID
227 */
228 private Set<VlanId> getVlans(JsonNode node, String path) {
229 ImmutableSet.Builder<VlanId> vlanIdBuilder = ImmutableSet.builder();
230
231 JsonNode vlansNode = node.get(path);
232 if (vlansNode != null) {
233 vlansNode.forEach(vlanNode ->
234 vlanIdBuilder.add(VlanId.vlanId(vlanNode.shortValue())));
235 }
236
237 return vlanIdBuilder.build();
238 }
239
240 private ArrayNode putVlans(Set<VlanId> vlans) {
241 ArrayNode vlanArray = mapper.createArrayNode();
242
243 vlans.forEach(vlan -> vlanArray.add(vlan.toShort()));
244
245 return vlanArray;
246 }
247
Jonathan Hart00cddda2016-02-16 10:30:37 -0800248 private List<InterfaceIpAddress> getIps(JsonNode node) {
249 List<InterfaceIpAddress> ips = Lists.newArrayList();
Jonathan Harteb8c9472015-08-05 07:43:13 -0700250
251 JsonNode ipsNode = node.get(IPS);
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700252 if (ipsNode != null) {
253 ipsNode.forEach(jsonNode ->
254 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
255 }
Jonathan Harteb8c9472015-08-05 07:43:13 -0700256
257 return ips;
258 }
259
Jonathan Hart00cddda2016-02-16 10:30:37 -0800260 private ArrayNode putIps(List<InterfaceIpAddress> intfIpAddresses) {
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700261 ArrayNode ipArray = mapper.createArrayNode();
262
263 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
264
265 return ipArray;
266 }
267
Jonathan Harteb8c9472015-08-05 07:43:13 -0700268}