blob: 52ede6d16a8418906cc690de51f1468b21c25a74 [file] [log] [blame]
Sean Condonfae8e662016-12-15 10:25:13 +00001/*
2 * Copyright 2017-present 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 */
16package org.onosproject.drivers.microsemi.yang.utils;
17
18import java.util.Arrays;
19import java.util.Set;
20import java.util.TreeSet;
21
22import org.apache.commons.lang.ArrayUtils;
23
24/**
25 * A set of static utilities that allow a ce-vlan-map to be decomposed.
26 *
27 * This is an implementation specific to Microsemi that encodes ce-vlan-map
28 * in a similar way to Section 7.9 in the MEF 10.2 specification. That specification
29 * suggests comma delimited lists of VIDs - this implementation adds on colons to
30 * specify a contiguous range of IDs
31 */
32public final class CeVlanMapUtils {
33
34 private CeVlanMapUtils() {
35 //Do not allow this utility class to be instantiated
36 }
37
38 /**
39 * Calculate the ceVlanMap in to a Set of values.
40 *
41 * From Yang description
42 * "This object indicates the CE-VLANs associated with the specific
43 * EVC on a UNI. CE-VLAN IDs have value of 0 to 4095. The CE-VLAN ID
44 * list can be a single value or multiple values separated by a delimiter.
45 * Some valid values are: '100', '1:10', '10,20,30', '1:4095'. In the
46 * first example only CE-VLAN ID 100 is associated with the VLAN map.
47 * In the second example the CE-VLAN map includes CE-VLAN IDs 1 through
48 * 10 (range of values). The third example indicates three separate values
49 * that make up the CE-VLAN map. The last example indicates all CE-VLAN IDs
50 * are included in the map (range of values). ";
51 * reference
52 * "[MEF 6.1] 6.1; [MEF 7.2] 6.2.1.3";
53 * @param ceVlanMap A list of vlan id's in the format described above
54 * @return A set of vlan ids
55 */
56 public static Short[] getVlanSet(String ceVlanMap) {
57 if (ceVlanMap == null || ceVlanMap.isEmpty()) {
58 return new Short[0];
59 }
60 Set<Short> ceVlanSet = new TreeSet<Short>();
61
62 String[] ceVlanMapCommas = ceVlanMap.split(",");
63 for (String ceVlanMapComma:ceVlanMapCommas) {
64 String[] ceVlanMapColon = ceVlanMapComma.split(":");
65 if (ceVlanMapColon.length == 1) {
66 ceVlanSet.add(Short.decode(ceVlanMapColon[0]));
67 } else {
68 short start = Short.decode(ceVlanMapColon[0]);
69 short end = Short.decode(ceVlanMapColon[1]);
70 if ((start < 0 || end > 4095)) {
71 return null;
72 } else {
73 for (short i = start; i <= end; i++) {
74 ceVlanSet.add(i);
75 }
76 }
77 }
78 }
79
80 return ceVlanSet.toArray(new Short[ceVlanSet.size()]);
81 }
82
83 /**
84 * Convert an array of vlan ids in to a string representation.
85 * @param vlanArray An array of vlan ids
86 * @return A string representation delimited by commas and colons
87 */
88 public static String vlanListAsString(Short[] vlanArray) {
89 boolean colonPending = false;
90 StringBuilder ceVlanMapBuilder = new StringBuilder();
91 if (vlanArray.length == 0) {
92 return "";
93 } else if (vlanArray.length == 1 && vlanArray[0] == 0) {
94 return "0";
95 }
96
97 //To ensure that there are no repeated or out-of-order elements we must convert to TreeSet
98 TreeSet<Short> vlanSet = new TreeSet<>(Arrays.asList(vlanArray));
99
100 if (vlanSet.first() == 0) {
101 vlanSet.remove(vlanSet.first());
102 }
103 short prev = vlanSet.first();
104 for (short s:vlanSet) {
105 if (s == prev) {
106 ceVlanMapBuilder.append(Short.valueOf(s));
107 continue;
108 } else if (prev == (s - 1)) {
109 colonPending = true;
110 } else {
111 if (colonPending) {
112 ceVlanMapBuilder.append(":" + Short.valueOf(prev));
113 colonPending = false;
114 }
115 ceVlanMapBuilder.append("," + Short.valueOf(s));
116 }
117 prev = s;
118 }
119 if (colonPending) {
120 ceVlanMapBuilder.append(":" + Short.valueOf(prev));
121 }
122
123 return ceVlanMapBuilder.toString();
124 }
125
126 /**
127 * Add an additional vlan id to an existing string representation.
128 * @param existingMap An array of vlan ids
129 * @param newVlan The new vlan ID to add
130 * @return A string representation delimited by commas and colons
131 */
132 public static String addtoCeVlanMap(String existingMap, Short newVlan) {
133 Short[] vlanArray = getVlanSet(existingMap);
134 TreeSet<Short> vlanSet = new TreeSet<>();
135 for (Short vlan:vlanArray) {
136 vlanSet.add(vlan);
137 }
138
139 vlanSet.add(newVlan);
140
141 return vlanListAsString(vlanSet.toArray(new Short[vlanSet.size()]));
142 }
143
144 /**
145 * If a string representation contains a '0' then remove it.
146 *
147 * Zero is an invalid VLAN id, and is used here as a place holder for null. Null can't
148 * be used in the EA1000 device. Once any other vlan ids are added then the zero should
149 * be removed. It is safe to call this method even if no zero is present - the method will
150 * make no change in that case.
151 *
152 * @param existingMap An string representation of vlan ids, possibly containing a zero
153 * @return A string representation delimited by commas and colons without zero
154 */
155 public static String removeZeroIfPossible(String existingMap) {
156 if (existingMap == null || existingMap.isEmpty()) {
157 return "0";
158 } else if (existingMap == "0") {
159 return existingMap;
160 }
161 return removeFromCeVlanMap(existingMap, (short) 0);
162 }
163
164 /**
165 * Remove a vlan id from an existing string representation.
166 * @param existingMap An array of vlan ids
167 * @param vlanRemove The vlan ID to remove
168 * @return A string representation delimited by commas and colons
169 */
170 public static String removeFromCeVlanMap(String existingMap, Short vlanRemove) {
171 Short[] vlanArray = getVlanSet(existingMap);
172 TreeSet<Short> vlanSet = new TreeSet<>();
173 for (Short vlan:vlanArray) {
174 if (vlan.shortValue() != vlanRemove.shortValue()) {
175 vlanSet.add(vlan);
176 }
177 }
178
179 return vlanListAsString(vlanSet.toArray(new Short[vlanSet.size()]));
180 }
181
182 /**
183 * Combine vlan ids from two existing string representations.
184 *
185 * If there are overlapping elements and ranges, these are consolidated in to one.
186 *
187 * @param set1 A string containing a set of vlan ids
188 * @param set2 A string containing a set of vlan ids
189 * @return A string representation delimited by commas and colons
190 */
191 public static String combineVlanSets(String set1, String set2) {
192 Short[] set1Array = getVlanSet(set1);
193 Short[] set2Array = getVlanSet(set2);
194 return vlanListAsString((Short[]) ArrayUtils.addAll(set1Array, set2Array));
195 }
196}