Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.incubator.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.incubator.net.config.Config; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.host.InterfaceIpAddress; |
| 27 | |
| 28 | import java.util.Iterator; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | /** |
| 32 | * Basic configuration for a port on a device. |
| 33 | */ |
| 34 | public class BasicPortConfig extends Config<ConnectPoint> { |
| 35 | public static final String IPS = "ips"; |
| 36 | public static final String MAC = "mac"; |
| 37 | public static final String VLAN = "vlan"; |
| 38 | |
| 39 | /** |
| 40 | * Returns the set of IP addresses assigned to the port. |
| 41 | * |
| 42 | * @return set ip IP addresses |
| 43 | */ |
| 44 | public Set<InterfaceIpAddress> ips() { |
| 45 | Set<InterfaceIpAddress> ips = Sets.newHashSet(); |
| 46 | |
| 47 | JsonNode ipsNode = node.get(IPS); |
| 48 | ipsNode.forEach(jsonNode -> ips.add(InterfaceIpAddress.valueOf(jsonNode.asText()))); |
| 49 | |
| 50 | return ips; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Adds an IP address to configuration of the port. |
| 55 | * |
| 56 | * @param ip ip address to add |
| 57 | * @return this |
| 58 | */ |
| 59 | public BasicPortConfig addIp(InterfaceIpAddress ip) { |
| 60 | ArrayNode ipsNode = (ArrayNode) node.get(IPS); |
| 61 | if (ipsNode == null) { |
| 62 | ipsNode = node.putArray(IPS); |
| 63 | } |
| 64 | |
| 65 | // Check if the value is already there |
| 66 | if (ipsNode.findValue(ip.toString()) != null) { |
| 67 | ipsNode.add(ip.toString()); |
| 68 | } |
| 69 | |
| 70 | return this; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Removes an IP address from the configuration of the port. |
| 75 | * |
| 76 | * @param ip ip address to remove |
| 77 | * @return this |
| 78 | */ |
| 79 | public BasicPortConfig removeIp(InterfaceIpAddress ip) { |
| 80 | ArrayNode ipsNode = (ArrayNode) node.get(IPS); |
| 81 | |
| 82 | if (ipsNode != null) { |
| 83 | if (ipsNode.size() == 1) { |
| 84 | node.remove(IPS); |
| 85 | } else { |
| 86 | Iterator<JsonNode> it = ipsNode.iterator(); |
| 87 | while (it.hasNext()) { |
| 88 | if (it.next().asText().equals(ip.toString())) { |
| 89 | it.remove(); |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Clear all IP addresses from the configuration. |
| 101 | * |
| 102 | * @return this |
| 103 | */ |
| 104 | public BasicPortConfig clearIps() { |
| 105 | node.remove(IPS); |
| 106 | return this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the MAC address configured on the port. |
| 111 | * |
| 112 | * @return MAC address |
| 113 | */ |
| 114 | public MacAddress mac() { |
| 115 | JsonNode macNode = node.get(MAC); |
| 116 | if (macNode == null) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return MacAddress.valueOf(macNode.asText()); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Sets the MAC address configured on the port. |
| 125 | * |
| 126 | * @param mac MAC address |
| 127 | * @return this |
| 128 | */ |
| 129 | public BasicPortConfig mac(MacAddress mac) { |
| 130 | String macString = (mac == null) ? null : mac.toString(); |
| 131 | return (BasicPortConfig) setOrClear(MAC, macString); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the VLAN configured on the port. |
| 136 | * |
| 137 | * @return VLAN ID |
| 138 | */ |
| 139 | public VlanId vlan() { |
| 140 | JsonNode macNode = node.get(VLAN); |
| 141 | if (macNode == null) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | return VlanId.vlanId(Short.parseShort(macNode.asText())); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets the VLAN configured on the port. |
| 150 | * |
| 151 | * @param vlan VLAN ID |
| 152 | * @return this |
| 153 | */ |
| 154 | public BasicPortConfig vlan(VlanId vlan) { |
| 155 | Integer vlanId = (vlan == null) ? null : Integer.valueOf(vlan.toShort()); |
| 156 | return (BasicPortConfig) setOrClear(VLAN, vlanId); |
| 157 | } |
| 158 | } |