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.google.common.annotations.Beta;
21 import com.google.common.collect.Sets;
22 import org.onlab.packet.MacAddress;
23 import org.onlab.packet.VlanId;
24 import org.onosproject.net.config.Config;
25 import org.onosproject.incubator.net.intf.Interface;
26 import org.onosproject.net.ConnectPoint;
27 import org.onosproject.net.host.InterfaceIpAddress;
32 * Configuration for interfaces.
35 public class InterfaceConfig extends Config<ConnectPoint> {
36 public static final String IPS = "ips";
37 public static final String MAC = "mac";
38 public static final String VLAN = "vlan";
40 public static final String IP_MISSING_ERROR = "Must have at least one IP address";
41 public static final String MAC_MISSING_ERROR = "Must have a MAC address for each interface";
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 throw new ConfigException(IP_MISSING_ERROR);
60 if (intfNode.path(MAC).isMissingNode()) {
61 throw new ConfigException(MAC_MISSING_ERROR);
64 MacAddress mac = MacAddress.valueOf(intfNode.path(MAC).asText());
66 VlanId vlan = VlanId.NONE;
67 if (!intfNode.path(VLAN).isMissingNode()) {
68 vlan = VlanId.vlanId(Short.valueOf(intfNode.path(VLAN).asText()));
71 interfaces.add(new Interface(subject, ips, mac, vlan));
73 } catch (IllegalArgumentException e) {
74 throw new ConfigException(CONFIG_VALUE_ERROR, e);
80 private Set<InterfaceIpAddress> getIps(JsonNode node) {
81 Set<InterfaceIpAddress> ips = Sets.newHashSet();
83 JsonNode ipsNode = node.get(IPS);
84 ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));