Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 1 | /* |
Pier Luigi Ventre | 0a023f4 | 2016-04-30 11:03:15 +0200 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 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.sdxl2; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
| 20 | import org.onlab.packet.MacAddress; |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | |
| 24 | import java.util.ArrayList; |
| 25 | import java.util.List; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.Preconditions.*; |
| 29 | |
| 30 | /** |
| 31 | * Sdx-l2 connection point expressed as composition of a: |
| 32 | * ConnectPoint; Set of Vlanid; Customer edge MAC address (optional). |
| 33 | */ |
| 34 | public class SdxL2ConnectionPoint { |
| 35 | |
| 36 | private String name; |
| 37 | private final ConnectPoint cPoint; |
| 38 | private final List<VlanId> vlanIds; |
| 39 | private final MacAddress ceMac; |
| 40 | |
| 41 | /** |
| 42 | * Creates a new SDXL2 connection point. |
| 43 | * |
| 44 | * @param name connection point name |
| 45 | * @param cPoint connection point |
| 46 | * @param vlans the customer edge vlans |
| 47 | * @param ceMac the customer edge router mac address |
| 48 | */ |
| 49 | public SdxL2ConnectionPoint(String name, ConnectPoint cPoint, List<VlanId> vlans, MacAddress ceMac) { |
| 50 | this.name = name; |
| 51 | this.cPoint = cPoint; |
| 52 | this.vlanIds = vlans; |
| 53 | this.ceMac = ceMac; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return the name of SDXL2 connection point. |
| 58 | * |
| 59 | * @return a string representing the name of connection point |
| 60 | */ |
| 61 | public String name() { |
| 62 | return name; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the connection point. |
| 67 | * |
| 68 | * @return connection point object |
| 69 | */ |
| 70 | public ConnectPoint connectPoint() { |
| 71 | return cPoint; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the set of vlans that are used by the customer edge. |
| 76 | * |
| 77 | * @return a set of vlan ids |
| 78 | */ |
| 79 | public List<VlanId> vlanIds() { |
| 80 | return vlanIds; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns the customer edge mac address. |
| 85 | * |
| 86 | * @return a mac address object |
| 87 | */ |
| 88 | public MacAddress macAddress() { |
| 89 | return ceMac; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Parse a device connect point from a string, set of vlans from a string |
| 94 | * and mac from a string. |
| 95 | * The connect point should be in the format "deviceUri/portNumber". |
| 96 | * The vlans should be in the format "vlan1,vlan2,vlan3" |
| 97 | * The mac address should be in hex |
| 98 | * |
| 99 | * @param name name of the SDXL2 cp |
| 100 | * @param connectPoint connection point to parse |
| 101 | * @param vlans vlanIds to parse |
| 102 | * @param mac mac address to parse |
| 103 | * @return a ConnectPoint based on the information in the string. |
| 104 | * |
| 105 | */ |
| 106 | public static SdxL2ConnectionPoint |
| 107 | sdxl2ConnectionPoint(String name, String connectPoint, String vlans, String mac) { |
| 108 | checkNotNull(connectPoint); |
| 109 | checkNotNull(vlans); |
| 110 | checkState(!(name.contains(",") || |
| 111 | name.contains("-") || |
| 112 | name.contains("vlanid=") || |
| 113 | name.contains("ConnectPoint{") || |
| 114 | name.contains("elementId=") || |
| 115 | name.contains("portNumber=") || |
| 116 | name.contains("{") || |
| 117 | name.contains("}") || |
| 118 | name.contains("|")), "Names cannot contain some special characters"); |
| 119 | checkNotNull(mac); |
| 120 | ConnectPoint connectionPoint = ConnectPoint.deviceConnectPoint(connectPoint); |
| 121 | String[] splitted = vlans.split(","); |
| 122 | checkArgument(splitted.length != 0, "At least '-1' or '1' as value"); |
| 123 | List<VlanId> vlanslist = new ArrayList<>(); |
| 124 | for (String vlan : splitted) { |
| 125 | if (!vlanslist.contains(VlanId.vlanId(Short.parseShort(vlan))) && |
| 126 | Short.parseShort(vlan) != -1 && |
| 127 | Short.parseShort(vlan) != 1) { |
| 128 | vlanslist.add(VlanId.vlanId(Short.parseShort(vlan))); |
| 129 | } |
| 130 | } |
| 131 | MacAddress macAddress = MacAddress.valueOf(mac); |
| 132 | return new SdxL2ConnectionPoint(name, connectionPoint, vlanslist, macAddress); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Parse a device connect point from a string and set of vlans from a string. |
| 137 | * The connect point should be in the format "deviceUri/portNumber". |
| 138 | * The vlans should be in the format "vlan1,vlan2,vlan3" |
| 139 | * |
| 140 | * @param name name of the SDXL2 cp |
| 141 | * @param connectPoint connection point to parse |
| 142 | * @param vlans vlanIds to parse |
| 143 | * @return a ConnectPoint based on the information in the string. |
| 144 | * |
| 145 | */ |
| 146 | public static SdxL2ConnectionPoint sdxl2ConnectionPoint(String name, String connectPoint, String vlans) { |
| 147 | checkNotNull(connectPoint); |
| 148 | checkNotNull(vlans); |
| 149 | checkState(!(name.contains(",") || |
| 150 | name.contains("-") || |
| 151 | name.contains("vlanid=") || |
| 152 | name.contains("ConnectPoint{") || |
| 153 | name.contains("elementId=") || |
| 154 | name.contains("portNumber=") || |
| 155 | name.contains("{") || |
| 156 | name.contains("}") || |
| 157 | name.contains("|")), "Names cannot contain some special characters"); |
| 158 | ConnectPoint connectionPoint = ConnectPoint.deviceConnectPoint(connectPoint); |
| 159 | String[] splitted = vlans.split(","); |
| 160 | checkArgument(splitted.length != 0, "At least '-1' or '1' as value"); |
| 161 | List<VlanId> vlanslist = new ArrayList<>(); |
| 162 | for (String vlan : splitted) { |
| 163 | if (!vlanslist.contains(VlanId.vlanId(Short.parseShort(vlan))) && |
| 164 | Short.parseShort(vlan) != -1 && |
| 165 | Short.parseShort(vlan) != 1) { |
| 166 | vlanslist.add(VlanId.vlanId(Short.parseShort(vlan))); |
| 167 | } |
| 168 | } |
| 169 | MacAddress macAddress = MacAddress.ZERO; |
| 170 | return new SdxL2ConnectionPoint(name, connectionPoint, vlanslist, macAddress); |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public int hashCode() { |
| 175 | return Objects.hash(name, cPoint, vlanIds, ceMac); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public boolean equals(Object obj) { |
| 180 | if (this == obj) { |
| 181 | return true; |
| 182 | } |
| 183 | if (obj instanceof SdxL2ConnectionPoint) { |
| 184 | final SdxL2ConnectionPoint other = (SdxL2ConnectionPoint) obj; |
| 185 | return Objects.equals(this.name, other.name) && |
| 186 | Objects.equals(this.cPoint, other.cPoint) && |
| 187 | Objects.equals(this.vlanIds, other.vlanIds) && |
| 188 | Objects.equals(this.ceMac, other.ceMac); |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | @Override |
| 194 | public String toString() { |
| 195 | return MoreObjects.toStringHelper(this) |
| 196 | .add("name", name) |
| 197 | .add("connectionPoint", cPoint) |
| 198 | .add("vlanIds", vlanIds) |
| 199 | .add("ceMac", ceMac) |
| 200 | .toString(); |
| 201 | } |
| 202 | } |