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 CONFIG_VALUE_ERROR = "Error parsing config value";
45 * Retrieves all interfaces configured on this port.
47 * @return set of interfaces
48 * @throws ConfigException if there is any error in the JSON config
50 public Set<Interface> getInterfaces() throws ConfigException {
51 Set<Interface> interfaces = Sets.newHashSet();
54 for (JsonNode intfNode : array) {
55 Set<InterfaceIpAddress> ips = getIps(intfNode);
57 String mac = intfNode.path(MAC).asText();
58 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
60 VlanId vlan = getVlan(intfNode);
62 interfaces.add(new Interface(subject, ips, macAddr, vlan));
64 } catch (IllegalArgumentException e) {
65 throw new ConfigException(CONFIG_VALUE_ERROR, e);
72 * Adds an interface to the config.
74 * @param intf interface to add
76 public void addInterface(Interface intf) {
77 ObjectNode intfNode = array.addObject();
79 if (intf.mac() != null) {
80 intfNode.put(MAC, intf.mac().toString());
83 if (!intf.ipAddresses().isEmpty()) {
84 intfNode.set(IPS, putIps(intf.ipAddresses()));
87 if (!intf.vlan().equals(VlanId.NONE)) {
88 intfNode.put(VLAN, intf.vlan().toString());
93 * Removes an interface from the config.
95 * @param intf interface to remove
97 public void removeInterface(Interface intf) {
98 for (int i = 0; i < array.size(); i++) {
99 if (intf.vlan().equals(getVlan(node))) {
106 private VlanId getVlan(JsonNode node) {
107 VlanId vlan = VlanId.NONE;
108 if (!node.path(VLAN).isMissingNode()) {
109 vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText()));
114 private Set<InterfaceIpAddress> getIps(JsonNode node) {
115 Set<InterfaceIpAddress> ips = Sets.newHashSet();
117 JsonNode ipsNode = node.get(IPS);
118 if (ipsNode != null) {
119 ipsNode.forEach(jsonNode ->
120 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
126 private ArrayNode putIps(Set<InterfaceIpAddress> intfIpAddresses) {
127 ArrayNode ipArray = mapper.createArrayNode();
129 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));