blob: 5246f3132b76a63f3ee2d64f28f18967e0f87f55 [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 Harteb8c9472015-08-05 07:43:13 -070023import com.google.common.collect.Sets;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.VlanId;
Jonathan Harteb8c9472015-08-05 07:43:13 -070026import org.onosproject.incubator.net.intf.Interface;
27import org.onosproject.net.ConnectPoint;
Jonathan Hart0bdf8372015-10-08 14:30:36 -070028import org.onosproject.net.config.Config;
Jonathan Harteb8c9472015-08-05 07:43:13 -070029import org.onosproject.net.host.InterfaceIpAddress;
30
Jonathan Hart7dbe6292015-11-10 08:33:17 -080031import java.util.Iterator;
Jonathan Harteb8c9472015-08-05 07:43:13 -070032import java.util.Set;
33
34/**
35 * Configuration for interfaces.
36 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070037@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070038public class InterfaceConfig extends Config<ConnectPoint> {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080039 public static final String NAME = "name";
Jonathan Harteb8c9472015-08-05 07:43:13 -070040 public static final String IPS = "ips";
41 public static final String MAC = "mac";
42 public static final String VLAN = "vlan";
43
Jonathan Hart4cb39882015-08-12 23:50:55 -040044 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
45
Jonathan Harteb8c9472015-08-05 07:43:13 -070046 /**
47 * Retrieves all interfaces configured on this port.
48 *
49 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040050 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070051 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040052 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070053 Set<Interface> interfaces = Sets.newHashSet();
54
Jonathan Hart4cb39882015-08-12 23:50:55 -040055 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -070056 for (JsonNode intfNode : array) {
Jonathan Hart7dbe6292015-11-10 08:33:17 -080057 String name = intfNode.path(NAME).asText(null);
58
Jonathan Hart4cb39882015-08-12 23:50:55 -040059 Set<InterfaceIpAddress> ips = getIps(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040060
Luca Pretee3879f72015-10-16 11:19:53 +020061 String mac = intfNode.path(MAC).asText();
62 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
Jonathan Hart4cb39882015-08-12 23:50:55 -040063
Jonathan Hart0bdf8372015-10-08 14:30:36 -070064 VlanId vlan = getVlan(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040065
Jonathan Hart7dbe6292015-11-10 08:33:17 -080066 interfaces.add(new Interface(name, subject, ips, macAddr, vlan));
Jonathan Hart4cb39882015-08-12 23:50:55 -040067 }
68 } catch (IllegalArgumentException e) {
69 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -070070 }
71
72 return interfaces;
73 }
74
Jonathan Hart0bdf8372015-10-08 14:30:36 -070075 /**
76 * Adds an interface to the config.
77 *
78 * @param intf interface to add
79 */
80 public void addInterface(Interface intf) {
81 ObjectNode intfNode = array.addObject();
Luca Pretee3879f72015-10-16 11:19:53 +020082
Jonathan Hart7dbe6292015-11-10 08:33:17 -080083 intfNode.put(NAME, intf.name());
84
Luca Pretee3879f72015-10-16 11:19:53 +020085 if (intf.mac() != null) {
86 intfNode.put(MAC, intf.mac().toString());
87 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -070088
89 if (!intf.ipAddresses().isEmpty()) {
90 intfNode.set(IPS, putIps(intf.ipAddresses()));
91 }
92
93 if (!intf.vlan().equals(VlanId.NONE)) {
94 intfNode.put(VLAN, intf.vlan().toString());
95 }
96 }
97
98 /**
99 * Removes an interface from the config.
100 *
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800101 * @param name name of the interface to remove
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700102 */
Jonathan Hart7dbe6292015-11-10 08:33:17 -0800103 public void removeInterface(String name) {
104 Iterator<JsonNode> it = array.iterator();
105 while (it.hasNext()) {
106 JsonNode node = it.next();
107 if (node.path(NAME).asText().equals(name)) {
108 it.remove();
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700109 break;
110 }
111 }
112 }
113
114 private VlanId getVlan(JsonNode node) {
115 VlanId vlan = VlanId.NONE;
116 if (!node.path(VLAN).isMissingNode()) {
117 vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText()));
118 }
119 return vlan;
120 }
121
Jonathan Harteb8c9472015-08-05 07:43:13 -0700122 private Set<InterfaceIpAddress> getIps(JsonNode node) {
123 Set<InterfaceIpAddress> ips = Sets.newHashSet();
124
125 JsonNode ipsNode = node.get(IPS);
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700126 if (ipsNode != null) {
127 ipsNode.forEach(jsonNode ->
128 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
129 }
Jonathan Harteb8c9472015-08-05 07:43:13 -0700130
131 return ips;
132 }
133
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700134 private ArrayNode putIps(Set<InterfaceIpAddress> intfIpAddresses) {
135 ArrayNode ipArray = mapper.createArrayNode();
136
137 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
138
139 return ipArray;
140 }
141
Jonathan Harteb8c9472015-08-05 07:43:13 -0700142}