blob: 9f2d4105e21e8911ed6e6132eb83a33d6c53c8af [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
31import java.util.Set;
32
33/**
34 * Configuration for interfaces.
35 */
Thomas Vachuska4c571ae2015-09-10 16:31:59 -070036@Beta
Jonathan Harteb8c9472015-08-05 07:43:13 -070037public class InterfaceConfig extends Config<ConnectPoint> {
Jonathan Harteb8c9472015-08-05 07:43:13 -070038 public static final String IPS = "ips";
39 public static final String MAC = "mac";
40 public static final String VLAN = "vlan";
41
Jonathan Hart4cb39882015-08-12 23:50:55 -040042 public static final String CONFIG_VALUE_ERROR = "Error parsing config value";
43
Jonathan Harteb8c9472015-08-05 07:43:13 -070044 /**
45 * Retrieves all interfaces configured on this port.
46 *
47 * @return set of interfaces
Jonathan Hart4cb39882015-08-12 23:50:55 -040048 * @throws ConfigException if there is any error in the JSON config
Jonathan Harteb8c9472015-08-05 07:43:13 -070049 */
Jonathan Hart4cb39882015-08-12 23:50:55 -040050 public Set<Interface> getInterfaces() throws ConfigException {
Jonathan Harteb8c9472015-08-05 07:43:13 -070051 Set<Interface> interfaces = Sets.newHashSet();
52
Jonathan Hart4cb39882015-08-12 23:50:55 -040053 try {
Jonathan Harted4fdcd2015-09-08 14:13:37 -070054 for (JsonNode intfNode : array) {
Jonathan Hart4cb39882015-08-12 23:50:55 -040055 Set<InterfaceIpAddress> ips = getIps(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040056
Luca Pretee3879f72015-10-16 11:19:53 +020057 String mac = intfNode.path(MAC).asText();
58 MacAddress macAddr = mac.isEmpty() ? null : MacAddress.valueOf(mac);
Jonathan Hart4cb39882015-08-12 23:50:55 -040059
Jonathan Hart0bdf8372015-10-08 14:30:36 -070060 VlanId vlan = getVlan(intfNode);
Jonathan Hart4cb39882015-08-12 23:50:55 -040061
Luca Pretee3879f72015-10-16 11:19:53 +020062 interfaces.add(new Interface(subject, ips, macAddr, vlan));
Jonathan Hart4cb39882015-08-12 23:50:55 -040063 }
64 } catch (IllegalArgumentException e) {
65 throw new ConfigException(CONFIG_VALUE_ERROR, e);
Jonathan Harteb8c9472015-08-05 07:43:13 -070066 }
67
68 return interfaces;
69 }
70
Jonathan Hart0bdf8372015-10-08 14:30:36 -070071 /**
72 * Adds an interface to the config.
73 *
74 * @param intf interface to add
75 */
76 public void addInterface(Interface intf) {
77 ObjectNode intfNode = array.addObject();
Luca Pretee3879f72015-10-16 11:19:53 +020078
79 if (intf.mac() != null) {
80 intfNode.put(MAC, intf.mac().toString());
81 }
Jonathan Hart0bdf8372015-10-08 14:30:36 -070082
83 if (!intf.ipAddresses().isEmpty()) {
84 intfNode.set(IPS, putIps(intf.ipAddresses()));
85 }
86
87 if (!intf.vlan().equals(VlanId.NONE)) {
88 intfNode.put(VLAN, intf.vlan().toString());
89 }
90 }
91
92 /**
93 * Removes an interface from the config.
94 *
95 * @param intf interface to remove
96 */
97 public void removeInterface(Interface intf) {
98 for (int i = 0; i < array.size(); i++) {
99 if (intf.vlan().equals(getVlan(node))) {
100 array.remove(i);
101 break;
102 }
103 }
104 }
105
106 private VlanId getVlan(JsonNode node) {
107 VlanId vlan = VlanId.NONE;
108 if (!node.path(VLAN).isMissingNode()) {
109 vlan = VlanId.vlanId(Short.valueOf(node.path(VLAN).asText()));
110 }
111 return vlan;
112 }
113
Jonathan Harteb8c9472015-08-05 07:43:13 -0700114 private Set<InterfaceIpAddress> getIps(JsonNode node) {
115 Set<InterfaceIpAddress> ips = Sets.newHashSet();
116
117 JsonNode ipsNode = node.get(IPS);
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700118 if (ipsNode != null) {
119 ipsNode.forEach(jsonNode ->
120 ips.add(InterfaceIpAddress.valueOf(jsonNode.asText())));
121 }
Jonathan Harteb8c9472015-08-05 07:43:13 -0700122
123 return ips;
124 }
125
Jonathan Hart0bdf8372015-10-08 14:30:36 -0700126 private ArrayNode putIps(Set<InterfaceIpAddress> intfIpAddresses) {
127 ArrayNode ipArray = mapper.createArrayNode();
128
129 intfIpAddresses.forEach(i -> ipArray.add(i.toString()));
130
131 return ipArray;
132 }
133
Jonathan Harteb8c9472015-08-05 07:43:13 -0700134}