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 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 31 | * SDX-L2 connection point expressed as composition of a: |
| 32 | * connect point; set of VLAN id; MAC address (optional). |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 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 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 42 | * Creates a new SDX-L2 connection point. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 43 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 44 | * @param name SDX-L2 connection point name |
| 45 | * @param cPoint connect point |
| 46 | * @param vlans the customer edge VLANs |
| 47 | * @param ceMac the customer edge router MAC address |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 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 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 57 | * Returns the name of SDX-L2 connection point. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 58 | * |
| 59 | * @return a string representing the name of connection point |
| 60 | */ |
| 61 | public String name() { |
| 62 | return name; |
| 63 | } |
| 64 | |
| 65 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 66 | * Returns the connect point. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 67 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 68 | * @return connect point object |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 69 | */ |
| 70 | public ConnectPoint connectPoint() { |
| 71 | return cPoint; |
| 72 | } |
| 73 | |
| 74 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 75 | * Returns the set of VLANs that are used by the customer edge. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 76 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 77 | * @return a set of VLAN ids |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 78 | */ |
| 79 | public List<VlanId> vlanIds() { |
| 80 | return vlanIds; |
| 81 | } |
| 82 | |
| 83 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 84 | * Returns the customer edge MAC address. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 85 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 86 | * @return a MAC address object |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 87 | */ |
| 88 | public MacAddress macAddress() { |
| 89 | return ceMac; |
| 90 | } |
| 91 | |
| 92 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 93 | * Parse a device connect point from a string, set of VLANs from a string |
| 94 | * and MAC from a string. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 95 | * The connect point should be in the format "deviceUri/portNumber". |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 96 | * The VLANs should be in the format "vlan1,vlan2,vlan3" |
| 97 | * The MAC address should be in hex |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 98 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 99 | * @param name name of the SDX-L2 connection point |
| 100 | * @param connectPoint connect point to parse |
| 101 | * @param vlans VLAN ids to parse |
| 102 | * @param mac MAC address to parse |
| 103 | * @return a SDX-L2 connection point based on the information in the string. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 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 | /** |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 136 | * Parse a device connect point from a string and set of VLANs from a string. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 137 | * The connect point should be in the format "deviceUri/portNumber". |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 138 | * The VLANs should be in the format "vlan1,vlan2,vlan3" |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 139 | * |
pierventre | 3849e56 | 2016-05-11 11:47:32 +0200 | [diff] [blame^] | 140 | * @param name name of the SDX-L2 connection point |
| 141 | * @param connectPoint connect point to parse |
| 142 | * @param vlans VLAN ids to parse |
| 143 | * @return a SDX-L2 connection point based on the information in the string. |
Pier Luigi Ventre | d1173a1 | 2016-03-30 15:51:03 +0200 | [diff] [blame] | 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 | } |