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