blob: 600fe3de5b1c6ab1194c6af613964f3e66050d02 [file] [log] [blame]
Jonathan Harteb8c9472015-08-05 07:43:13 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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.incubator.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.google.common.collect.Sets;
21import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
Ray Milkeya4122362015-08-18 15:19:08 -070023import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070024import org.onosproject.incubator.net.intf.Interface;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.host.InterfaceIpAddress;
27
28import java.util.Set;
29
30/**
31 * Configuration for interfaces.
32 */
33public class InterfaceConfig extends Config<ConnectPoint> {
34 public static final String INTERFACES = "interfaces";
35 public static final String IPS = "ips";
36 public static final String MAC = "mac";
37 public static final String VLAN = "vlan";
38
Jonathan Hart4cb39882015-08-12 23:50:55 -040039 public static final String IP_MISSING_ERROR = "Must have at least one IP address";
40 public static final String MAC_MISSING_ERROR = "Must have a MAC address for each interface";
41 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
42
Jonathan Harteb8c9472015-08-05 07:43:13 -070043 /**
44 * Retrieves all interfaces configured on this port.
45 *
46 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040047 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070048 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040049 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070050 Set<Interface> interfaces = Sets.newHashSet();
51
Jonathan Hart4cb39882015-08-12 23:50:55 -040052 try {
53 for (JsonNode intfNode : node.path(INTERFACES)) {
54 Set<InterfaceIpAddress> ips = getIps(intfNode);
55 if (ips.isEmpty()) {
56 throw new ConfigException(IP_MISSING_ERROR);
57 }
58
59 if (intfNode.path(MAC).isMissingNode()) {
60 throw new ConfigException(MAC_MISSING_ERROR);
61 }
62
63 MacAddress mac = MacAddress.valueOf(intfNode.path(MAC).asText());
64
65 VlanId vlan = VlanId.NONE;
66 if (!intfNode.path(VLAN).isMissingNode()) {
67 vlan = VlanId.vlanId(Short.valueOf(intfNode.path(VLAN).asText()));
68 }
69
70 interfaces.add(new Interface(subject, ips, mac, vlan));
71 }
72 } catch (IllegalArgumentException e) {
73 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -070074 }
75
76 return interfaces;
77 }
78
79 private Set<InterfaceIpAddress> getIps(JsonNode node) {
80 Set<InterfaceIpAddress> ips = Sets.newHashSet();
81
82 JsonNode ipsNode = node.get(IPS);
83 ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
84
85 return ips;
86 }
87
88}