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; |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 22 | import com.google.common.annotations.Beta; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Sets; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onlab.packet.VlanId; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 27 | import org.onosproject.incubator.net.intf.Interface; |
| 28 | import org.onosproject.net.ConnectPoint; |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 29 | import org.onosproject.net.config.Config; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 30 | import org.onosproject.net.host.InterfaceIpAddress; |
| 31 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 32 | import java.util.Iterator; |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 33 | import java.util.List; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 34 | import java.util.Set; |
| 35 | |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 36 | import static com.google.common.base.Preconditions.checkArgument; |
| 37 | import static com.google.common.base.Preconditions.checkNotNull; |
| 38 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Configuration for interfaces. |
| 41 | */ |
Thomas Vachuska | 4c571ae | 2015-09-10 16:31:59 -0700 | [diff] [blame] | 42 | @Beta |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 43 | public class InterfaceConfig extends Config<ConnectPoint> { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 44 | public static final String NAME = "name"; |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 45 | public static final String IPS = "ips"; |
| 46 | public static final String MAC = "mac"; |
| 47 | public static final String VLAN = "vlan"; |
| 48 | |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 49 | private static final String CONFIG_VALUE_ERROR = "Error parsing config value"; |
| 50 | private static final String INTF_NULL_ERROR = "Interface cannot be null"; |
| 51 | private static final String INTF_NAME_ERROR = "Interface must have a valid name"; |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 52 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Retrieves all interfaces configured on this port. |
| 55 | * |
| 56 | * @return set of interfaces |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 57 | * @throws ConfigException if there is any error in the JSON config |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 58 | */ |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 59 | public Set<Interface> getInterfaces() throws ConfigException { |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 60 | Set<Interface> interfaces = Sets.newHashSet(); |
| 61 | |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 62 | try { |
Jonathan Hart | ed4fdcd | 2015-09-08 14:13:37 -0700 | [diff] [blame] | 63 | for (JsonNode intfNode : array) { |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 64 | String name = intfNode.path(NAME).asText(null); |
| 65 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 66 | List<InterfaceIpAddress> ips = getIps(intfNode); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 67 | |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 68 | String mac = intfNode.path(MAC).asText(); |
| 69 | MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 70 | |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 71 | VlanId vlan = getVlan(intfNode); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 72 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 73 | interfaces.add(new Interface(name, subject, ips, macAddr, vlan)); |
Jonathan Hart | 4cb3988 | 2015-08-12 23:50:55 -0400 | [diff] [blame] | 74 | } |
| 75 | } catch (IllegalArgumentException e) { |
| 76 | throw new ConfigException(CONFIG_VALUE_ERROR, e); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return interfaces; |
| 80 | } |
| 81 | |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Adds an interface to the config. |
| 84 | * |
| 85 | * @param intf interface to add |
| 86 | */ |
| 87 | public void addInterface(Interface intf) { |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 88 | checkNotNull(intf, INTF_NULL_ERROR); |
| 89 | checkArgument(!intf.name().equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR); |
| 90 | |
| 91 | // Remove old interface with this name if it exists |
| 92 | removeInterface(intf.name()); |
| 93 | |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 94 | ObjectNode intfNode = array.addObject(); |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 95 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 96 | intfNode.put(NAME, intf.name()); |
| 97 | |
Luca Prete | e3879f7 | 2015-10-16 11:19:53 +0200 | [diff] [blame] | 98 | if (intf.mac() != null) { |
| 99 | intfNode.put(MAC, intf.mac().toString()); |
| 100 | } |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 101 | |
| 102 | if (!intf.ipAddresses().isEmpty()) { |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 103 | intfNode.set(IPS, putIps(intf.ipAddressesList())); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (!intf.vlan().equals(VlanId.NONE)) { |
| 107 | intfNode.put(VLAN, intf.vlan().toString()); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Removes an interface from the config. |
| 113 | * |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 114 | * @param name name of the interface to remove |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 115 | */ |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 116 | public void removeInterface(String name) { |
Jonathan Hart | 4b5c6dc | 2015-10-13 14:27:48 -0700 | [diff] [blame] | 117 | checkNotNull(name, INTF_NULL_ERROR); |
| 118 | checkArgument(!name.equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR); |
| 119 | |
Jonathan Hart | 7dbe629 | 2015-11-10 08:33:17 -0800 | [diff] [blame] | 120 | Iterator<JsonNode> it = array.iterator(); |
| 121 | while (it.hasNext()) { |
| 122 | JsonNode node = it.next(); |
| 123 | if (node.path(NAME).asText().equals(name)) { |
| 124 | it.remove(); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | private VlanId getVlan(JsonNode node) { |
| 131 | VlanId vlan = VlanId.NONE; |
| 132 | if (!node.path(VLAN).isMissingNode()) { |
| 133 | vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText())); |
| 134 | } |
| 135 | return vlan; |
| 136 | } |
| 137 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 138 | private List<InterfaceIpAddress> getIps(JsonNode node) { |
| 139 | List<InterfaceIpAddress> ips = Lists.newArrayList(); |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 140 | |
| 141 | JsonNode ipsNode = node.get(IPS); |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 142 | if (ipsNode != null) { |
| 143 | ipsNode.forEach(jsonNode -> |
| 144 | ips.add(InterfaceIpAddress.valueOf(jsonNode.asText()))); |
| 145 | } |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 146 | |
| 147 | return ips; |
| 148 | } |
| 149 | |
Jonathan Hart | 00cddda | 2016-02-16 10:30:37 -0800 | [diff] [blame] | 150 | private ArrayNode putIps(List<InterfaceIpAddress> intfIpAddresses) { |
Jonathan Hart | 0bdf837 | 2015-10-08 14:30:36 -0700 | [diff] [blame] | 151 | ArrayNode ipArray = mapper.createArrayNode(); |
| 152 | |
| 153 | intfIpAddresses.forEach(i -> ipArray.add(i.toString())); |
| 154 | |
| 155 | return ipArray; |
| 156 | } |
| 157 | |
Jonathan Hart | eb8c947 | 2015-08-05 07:43:13 -0700 | [diff] [blame] | 158 | } |