blob: 8d938ba17b341b5cbf0cfd38bff52a00c806bb9d [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;
25import org.onlab.packet.MacAddress;
26import org.onlab.packet.VlanId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070027import org.onosproject.incubator.net.intf.Interface;
28import org.onosproject.net.ConnectPoint;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070029import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070030import org.onosproject.net.host.InterfaceIpAddress;
31
Jonathan Hart7dbe6292015-11-10 08:33:17 -080032import java.util.Iterator;
Jonathan Hart00cddda2016-02-16 10:30:37 -080033import java.util.List;
Jonathan Harteb8c9472015-08-05 07:43:13 -070034import java.util.Set;
35
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070036import static com.google.common.base.Preconditions.checkArgument;
37import static com.google.common.base.Preconditions.checkNotNull;
38
Jonathan Harteb8c9472015-08-05 07:43:13 -070039/**
40 * Configuration for interfaces.
41 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070042@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070043public class InterfaceConfig extends Config<ConnectPoint> {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080044 public static final String NAME = "name";
Jonathan Harteb8c9472015-08-05 07:43:13 -070045 public static final String IPS = "ips";
46 public static final String MAC = "mac";
47 public static final String VLAN = "vlan";
48
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070049 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 Hart4cb39882015-08-12 23:50:55 -040052
Jonathan Harteb8c9472015-08-05 07:43:13 -070053 /**
54 * Retrieves all interfaces configured on this port.
55 *
56 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040057 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070058 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040059 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070060 Set<Interface> interfaces = Sets.newHashSet();
61
Jonathan Hart4cb39882015-08-12 23:50:55 -040062 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -070063 for (JsonNode intfNode : array) {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080064 String name = intfNode.path(NAME).asText(null);
65
Jonathan Hart00cddda2016-02-16 10:30:37 -080066 List<InterfaceIpAddress> ips = getIps(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040067
Luca Pretee3879f72015-10-16 11:19:53 +020068 String mac = intfNode.path(MAC).asText();
69 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
Jonathan Hart4cb39882015-08-12 23:50:55 -040070
Jonathan Hart0bdf8372015-10-08 14:30:36 -070071 VlanId vlan = getVlan(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040072
Jonathan Hart7dbe6292015-11-10 08:33:17 -080073 interfaces.add(new Interface(name, subject, ips, macAddr, vlan));
Jonathan Hart4cb39882015-08-12 23:50:55 -040074 }
75 } catch (IllegalArgumentException e) {
76 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -070077 }
78
79 return interfaces;
80 }
81
Jonathan Hart0bdf8372015-10-08 14:30:36 -070082 /**
83 * Adds an interface to the config.
84 *
85 * @param intf interface to add
86 */
87 public void addInterface(Interface intf) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -070088 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 Hart0bdf8372015-10-08 14:30:36 -070094 ObjectNode intfNode = array.addObject();
Luca Pretee3879f72015-10-16 11:19:53 +020095
Jonathan Hart7dbe6292015-11-10 08:33:17 -080096 intfNode.put(NAME, intf.name());
97
Luca Pretee3879f72015-10-16 11:19:53 +020098 if (intf.mac() != null) {
99 intfNode.put(MAC, intf.mac().toString());
100 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700101
102 if (!intf.ipAddresses().isEmpty()) {
Jonathan Hart00cddda2016-02-16 10:30:37 -0800103 intfNode.set(IPS, putIps(intf.ipAddressesList()));
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700104 }
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 Hart7dbe6292015-11-10 08:33:17 -0800114 * @param name name of the interface to remove
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700115 */
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800116 public void removeInterface(String name) {
Jonathan Hart4b5c6dc2015-10-13 14:27:48 -0700117 checkNotNull(name, INTF_NULL_ERROR);
118 checkArgument(!name.equals(Interface.NO_INTERFACE_NAME), INTF_NAME_ERROR);
119
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800120 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 Hart0bdf8372015-10-08 14:30:36 -0700125 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 Hart00cddda2016-02-16 10:30:37 -0800138 private List<InterfaceIpAddress> getIps(JsonNode node) {
139 List<InterfaceIpAddress> ips = Lists.newArrayList();
Jonathan Harteb8c9472015-08-05 07:43:13 -0700140
141 JsonNode ipsNode = node.get(IPS);
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700142 if (ipsNode != null) {
143 ipsNode.forEach(jsonNode ->
144 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
145 }
Jonathan Harteb8c9472015-08-05 07:43:13 -0700146
147 return ips;
148 }
149
Jonathan Hart00cddda2016-02-16 10:30:37 -0800150 private ArrayNode putIps(List<InterfaceIpAddress> intfIpAddresses) {
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700151 ArrayNode ipArray = mapper.createArrayNode();
152
153 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
154
155 return ipArray;
156 }
157
Jonathan Harteb8c9472015-08-05 07:43:13 -0700158}