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