blob: f5f68498ad3b7ebfda313688ae6a75d0b09890cc [file] [log] [blame]
Pier Luigi Ventred1173a12016-03-30 15:51:03 +02001/*
Pier Luigi Ventre0a023f42016-04-30 11:03:15 +02002 * Copyright 2016-present Open Networking Laboratory
Pier Luigi Ventred1173a12016-03-30 15:51:03 +02003 *
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.sdxl2;
18
19import com.google.common.base.MoreObjects;
20import org.onlab.packet.MacAddress;
21import org.onlab.packet.VlanId;
22import org.onosproject.net.ConnectPoint;
23
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Objects;
27
28import static com.google.common.base.Preconditions.*;
29
30/**
pierventre3849e562016-05-11 11:47:32 +020031 * SDX-L2 connection point expressed as composition of a:
32 * connect point; set of VLAN id; MAC address (optional).
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020033 */
34public 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 /**
pierventre3849e562016-05-11 11:47:32 +020042 * Creates a new SDX-L2 connection point.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020043 *
pierventre3849e562016-05-11 11:47:32 +020044 * @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 Ventred1173a12016-03-30 15:51:03 +020048 */
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 /**
pierventre3849e562016-05-11 11:47:32 +020057 * Returns the name of SDX-L2 connection point.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020058 *
59 * @return a string representing the name of connection point
60 */
61 public String name() {
62 return name;
63 }
64
65 /**
pierventre3849e562016-05-11 11:47:32 +020066 * Returns the connect point.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020067 *
pierventre3849e562016-05-11 11:47:32 +020068 * @return connect point object
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020069 */
70 public ConnectPoint connectPoint() {
71 return cPoint;
72 }
73
74 /**
pierventre3849e562016-05-11 11:47:32 +020075 * Returns the set of VLANs that are used by the customer edge.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020076 *
pierventre3849e562016-05-11 11:47:32 +020077 * @return a set of VLAN ids
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020078 */
79 public List<VlanId> vlanIds() {
80 return vlanIds;
81 }
82
83 /**
pierventre3849e562016-05-11 11:47:32 +020084 * Returns the customer edge MAC address.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020085 *
pierventre3849e562016-05-11 11:47:32 +020086 * @return a MAC address object
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020087 */
88 public MacAddress macAddress() {
89 return ceMac;
90 }
91
92 /**
pierventre3849e562016-05-11 11:47:32 +020093 * Parse a device connect point from a string, set of VLANs from a string
94 * and MAC from a string.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020095 * The connect point should be in the format "deviceUri/portNumber".
pierventre3849e562016-05-11 11:47:32 +020096 * The VLANs should be in the format "vlan1,vlan2,vlan3"
97 * The MAC address should be in hex
Pier Luigi Ventred1173a12016-03-30 15:51:03 +020098 *
pierventre3849e562016-05-11 11:47:32 +020099 * @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 Ventred1173a12016-03-30 15:51:03 +0200104 *
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 /**
pierventre3849e562016-05-11 11:47:32 +0200136 * Parse a device connect point from a string and set of VLANs from a string.
Pier Luigi Ventred1173a12016-03-30 15:51:03 +0200137 * The connect point should be in the format "deviceUri/portNumber".
pierventre3849e562016-05-11 11:47:32 +0200138 * The VLANs should be in the format "vlan1,vlan2,vlan3"
Pier Luigi Ventred1173a12016-03-30 15:51:03 +0200139 *
pierventre3849e562016-05-11 11:47:32 +0200140 * @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 Ventred1173a12016-03-30 15:51:03 +0200144 *
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}