blob: 8903521947920157ff10596c9ac990422b259673 [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;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070020import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070022import com.google.common.annotations.Beta;
Jonathan Hart00cddda2016-02-16 10:30:37 -080023import com.google.common.collect.Lists;
Jonathan Harteb8c9472015-08-05 07:43:13 -070024import com.google.common.collect.Sets;
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080025import org.onlab.packet.IpPrefix;
Jonathan Harteb8c9472015-08-05 07:43:13 -070026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070028import org.onosproject.incubator.net.intf.Interface;
29import org.onosproject.net.ConnectPoint;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070030import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070031import org.onosproject.net.host.InterfaceIpAddress;
32
Jonathan Hart7dbe6292015-11-10 08:33:17 -080033import java.util.Iterator;
Jonathan Hart00cddda2016-02-16 10:30:37 -080034import java.util.List;
Jonathan Harteb8c9472015-08-05 07:43:13 -070035import java.util.Set;
36
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070037import static com.google.common.base.Preconditions.checkArgument;
38import static com.google.common.base.Preconditions.checkNotNull;
39
Jonathan Harteb8c9472015-08-05 07:43:13 -070040/**
41 * Configuration for interfaces.
42 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070043@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070044public class InterfaceConfig extends Config<ConnectPoint> {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080045 public static final String NAME = "name";
Jonathan Harteb8c9472015-08-05 07:43:13 -070046 public static final String IPS = "ips";
47 public static final String MAC = "mac";
48 public static final String VLAN = "vlan";
49
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070050 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 Hart4cb39882015-08-12 23:50:55 -040053
Jonathan Hart9eb45bb2016-02-12 10:59:11 -080054 @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 Harteb8c9472015-08-05 07:43:13 -070079 /**
80 * Retrieves all interfaces configured on this port.
81 *
82 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040083 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070084 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040085 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070086 Set<Interface> interfaces = Sets.newHashSet();
87
Jonathan Hart4cb39882015-08-12 23:50:55 -040088 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -070089 for (JsonNode intfNode : array) {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080090 String name = intfNode.path(NAME).asText(null);
91
Jonathan Hart00cddda2016-02-16 10:30:37 -080092 List<InterfaceIpAddress> ips = getIps(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040093
Luca Pretee3879f72015-10-16 11:19:53 +020094 String mac = intfNode.path(MAC).asText();
95 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
Jonathan Hart4cb39882015-08-12 23:50:55 -040096
Jonathan Hart0bdf8372015-10-08 14:30:36 -070097 VlanId vlan = getVlan(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040098
Jonathan Hart7dbe6292015-11-10 08:33:17 -080099 interfaces.add(new Interface(name, subject, ips, macAddr, vlan));
Jonathan Hart4cb39882015-08-12 23:50:55 -0400100 }
101 } catch (IllegalArgumentException e) {
102 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -0700103 }
104
105 return interfaces;
106 }
107
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700108 /**
109 * Adds an interface to the config.
110 *
111 * @param intf interface to add
112 */
113 public void addInterface(Interface intf) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700114 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 Hart0bdf8372015-10-08 14:30:36 -0700120 ObjectNode intfNode = array.addObject();
Luca Pretee3879f72015-10-16 11:19:53 +0200121
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800122 intfNode.put(NAME, intf.name());
123
Luca Pretee3879f72015-10-16 11:19:53 +0200124 if (intf.mac() != null) {
125 intfNode.put(MAC, intf.mac().toString());
126 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700127
128 if (!intf.ipAddresses().isEmpty()) {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800129 intfNode.set(IPS, putIps(intf.ipAddressesList()));
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700130 }
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 Hart7dbe6292015-11-10 08:33:17 -0800140 * @param name name of the interface to remove
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700141 */
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800142 public void removeInterface(String name) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700143 checkNotNull(name, INTF_NULL_ERROR);
144 checkArgument(!name.equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
145
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800146 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 Hart0bdf8372015-10-08 14:30:36 -0700151 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 Hart00cddda2016-02-16 10:30:37 -0800164 private List<InterfaceIpAddress> getIps(JsonNode node) {
165 List<InterfaceIpAddress> ips = Lists.newArrayList();
Jonathan Harteb8c9472015-08-05 07:43:13 -0700166
167 JsonNode ipsNode = node.get(IPS);
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700168 if (ipsNode != null) {
169 ipsNode.forEach(jsonNode ->
170 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
171 }
Jonathan Harteb8c9472015-08-05 07:43:13 -0700172
173 return ips;
174 }
175
Jonathan Hart00cddda2016-02-16 10:30:37 -0800176 private ArrayNode putIps(List<InterfaceIpAddress> intfIpAddresses) {
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700177 ArrayNode ipArray = mapper.createArrayNode();
178
179 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
180
181 return ipArray;
182 }
183
Jonathan Harteb8c9472015-08-05 07:43:13 -0700184}