2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onosproject.incubator.net.config.basics;
19 import com.fasterxml.jackson.databind.JsonNode;
20 import com.fasterxml.jackson.databind.node.ArrayNode;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import com.google.common.annotations.Beta;
23 import com.google.common.collect.Sets;
24 import org.onlab.packet.MacAddress;
25 import org.onlab.packet.VlanId;
26 import org.onosproject.incubator.net.intf.Interface;
27 import org.onosproject.net.ConnectPoint;
28 import org.onosproject.net.config.Config;
29 import org.onosproject.net.host.InterfaceIpAddress;
34 * Configuration for interfaces.
37 public class InterfaceConfig extends Config<ConnectPoint> {
38 public static final String IPS = "ips";
39 public static final String MAC = "mac";
40 public static final String VLAN = "vlan";
42 public static final String MAC_MISSING_ERROR = "Must have a MAC address for each interface";
43 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
46 * Retrieves all interfaces configured on this port.
48 * @return set of interfaces
49 * @throws ConfigException if there is any error in the JSON config
51 public Set<Interface> getInterfaces() throws ConfigException {
52 Set<Interface> interfaces = Sets.newHashSet();
55 for (JsonNode intfNode : array) {
56 Set<InterfaceIpAddress> ips = getIps(intfNode);
58 if (intfNode.path(MAC).isMissingNode()) {
59 throw new ConfigException(MAC_MISSING_ERROR);
62 MacAddress mac = MacAddress.valueOf(intfNode.path(MAC).asText());
64 VlanId vlan = getVlan(intfNode);
66 interfaces.add(new Interface(subject, ips, mac, vlan));
68 } catch (IllegalArgumentException e) {
69 throw new ConfigException(CONFIG_VALUE_ERROR, e);
76 * Adds an interface to the config.
78 * @param intf interface to add
80 public void addInterface(Interface intf) {
81 ObjectNode intfNode = array.addObject();
82 intfNode.put(MAC, intf.mac().toString());
84 if (!intf.ipAddresses().isEmpty()) {
85 intfNode.set(IPS, putIps(intf.ipAddresses()));
88 if (!intf.vlan().equals(VlanId.NONE)) {
89 intfNode.put(VLAN, intf.vlan().toString());
94 * Removes an interface from the config.
96 * @param intf interface to remove
98 public void removeInterface(Interface intf) {
99 for (int i = 0; i < array.size(); i++) {
100 if (intf.vlan().equals(getVlan(node))) {
107 private VlanId getVlan(JsonNode node) {
108 VlanId vlan = VlanId.NONE;
109 if (!node.path(VLAN).isMissingNode()) {
110 vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText()));
115 private Set<InterfaceIpAddress> getIps(JsonNode node) {
116 Set<InterfaceIpAddress> ips = Sets.newHashSet();
118 JsonNode ipsNode = node.get(IPS);
119 if (ipsNode != null) {
120 ipsNode.forEach(jsonNode ->
121 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
127 private ArrayNode putIps(Set<InterfaceIpAddress> intfIpAddresses) {
128 ArrayNode ipArray = mapper.createArrayNode();
130 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));