blob: af2b47d82be119a31c082e4fa6d2a10ff7310807 [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;
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070020import com.google.common.annotations.Beta;
Jonathan Harteb8c9472015-08-05 07:43:13 -070021import com.google.common.collect.Sets;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
Ray Milkeya4122362015-08-18 15:19:08 -070024import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070025import org.onosproject.incubator.net.intf.Interface;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.host.InterfaceIpAddress;
28
29import java.util.Set;
30
31/**
32 * Configuration for interfaces.
33 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070034@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070035public class InterfaceConfig extends Config<ConnectPoint> {
Jonathan Harteb8c9472015-08-05 07:43:13 -070036 public static final String IPS = "ips";
37 public static final String MAC = "mac";
38 public static final String VLAN = "vlan";
39
Jonathan Hart4cb39882015-08-12 23:50:55 -040040 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";
43
Jonathan Harteb8c9472015-08-05 07:43:13 -070044 /**
45 * Retrieves all interfaces configured on this port.
46 *
47 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040048 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070049 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040050 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070051 Set<Interface> interfaces = Sets.newHashSet();
52
Jonathan Hart4cb39882015-08-12 23:50:55 -040053 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -070054 for (JsonNode intfNode : array) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040055 Set<InterfaceIpAddress> ips = getIps(intfNode);
56 if (ips.isEmpty()) {
57 throw new ConfigException(IP_MISSING_ERROR);
58 }
59
60 if (intfNode.path(MAC).isMissingNode()) {
61 throw new ConfigException(MAC_MISSING_ERROR);
62 }
63
64 MacAddress mac = MacAddress.valueOf(intfNode.path(MAC).asText());
65
66 VlanId vlan = VlanId.NONE;
67 if (!intfNode.path(VLAN).isMissingNode()) {
68 vlan = VlanId.vlanId(Short.valueOf(intfNode.path(VLAN).asText()));
69 }
70
71 interfaces.add(new Interface(subject, ips, mac, vlan));
72 }
73 } catch (IllegalArgumentException e) {
74 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -070075 }
76
77 return interfaces;
78 }
79
80 private Set<InterfaceIpAddress> getIps(JsonNode node) {
81 Set<InterfaceIpAddress> ips = Sets.newHashSet();
82
83 JsonNode ipsNode = node.get(IPS);
84 ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
85
86 return ips;
87 }
88
89}