aboutsummaryrefslogtreecommitdiffstats
path: root/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util
diff options
context:
space:
mode:
Diffstat (limited to 'resource-assignment/provider/src/main/java/org/openecomp/sdnc/util')
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/db/DataSourceWrap.java98
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/expr/ExpressionEvaluator.java207
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/speed/SpeedUtil.java41
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/str/StrUtil.java305
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VpnParam.java30
-rw-r--r--resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VrfUtil.java76
6 files changed, 757 insertions, 0 deletions
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/db/DataSourceWrap.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/db/DataSourceWrap.java
new file mode 100644
index 0000000..0ed2a72
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/db/DataSourceWrap.java
@@ -0,0 +1,98 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.db;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+
+import javax.sql.DataSource;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DataSourceWrap implements DataSource {
+
+ private static final Logger log = LoggerFactory.getLogger(DataSourceWrap.class);
+
+ private DataSource dataSource;
+
+ @Override
+ public PrintWriter getLogWriter() throws SQLException {
+ return dataSource.getLogWriter();
+ }
+
+ @Override
+ public void setLogWriter(PrintWriter out) throws SQLException {
+ dataSource.setLogWriter(out);
+ }
+
+ @Override
+ public void setLoginTimeout(int seconds) throws SQLException {
+ dataSource.setLoginTimeout(seconds);
+ }
+
+ @Override
+ public int getLoginTimeout() throws SQLException {
+ return dataSource.getLoginTimeout();
+ }
+
+ @Override
+ public java.util.logging.Logger getParentLogger() throws SQLFeatureNotSupportedException {
+ return dataSource.getParentLogger();
+ }
+
+ @Override
+ public <T> T unwrap(Class<T> iface) throws SQLException {
+ return dataSource.unwrap(iface);
+ }
+
+ @Override
+ public boolean isWrapperFor(Class<?> iface) throws SQLException {
+ return dataSource.isWrapperFor(iface);
+ }
+
+ @Override
+ public Connection getConnection() throws SQLException {
+ Connection c = dataSource.getConnection();
+
+ log.debug("getConnection: " + c.getClass().getName());
+
+ c.setAutoCommit(true);
+ return c;
+ }
+
+ @Override
+ public Connection getConnection(String username, String password) throws SQLException {
+ Connection c = dataSource.getConnection(username, password);
+
+ log.debug("getConnection: " + c.getClass().getName());
+
+ c.setAutoCommit(true);
+ return c;
+ }
+
+ public void setDataSource(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+}
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/expr/ExpressionEvaluator.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/expr/ExpressionEvaluator.java
new file mode 100644
index 0000000..a6e8902
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/expr/ExpressionEvaluator.java
@@ -0,0 +1,207 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.expr;
+
+import java.util.Map;
+
+public class ExpressionEvaluator {
+
+ public static long evalLong(String expr, Map<String, Object> vars) {
+ return (long) evalFloat(expr, vars);
+ }
+
+ public static float evalFloat(String expr, Map<String, Object> vars) {
+ expr = expr.trim();
+ int sl = expr.length();
+ if (sl == 0)
+ throw new IllegalArgumentException("Cannot interpret empty string.");
+
+ // Remove parentheses if any
+ if (expr.charAt(0) == '(' && expr.charAt(sl - 1) == ')')
+ return evalFloat(expr.substring(1, sl - 1), vars);
+
+ // Look for operators in the order of least priority
+ String[] sss = findOperator(expr, "-", true);
+ if (sss != null)
+ return evalFloat(sss[0], vars) - evalFloat(sss[1], vars);
+
+ sss = findOperator(expr, "+", true);
+ if (sss != null)
+ return evalFloat(sss[0], vars) + evalFloat(sss[1], vars);
+
+ sss = findOperator(expr, "/", true);
+ if (sss != null)
+ return evalFloat(sss[0], vars) / evalFloat(sss[1], vars);
+
+ sss = findOperator(expr, "*", true);
+ if (sss != null)
+ return evalFloat(sss[0], vars) * evalFloat(sss[1], vars);
+
+ // Check if expr is a number
+ try {
+ return Float.valueOf(expr);
+ } catch (Exception e) {
+ }
+
+ // Must be a variable
+ Object v = vars.get(expr);
+ if (v != null) {
+ if (v instanceof Float)
+ return (Float) v;
+ if (v instanceof Long)
+ return (Long) v;
+ if (v instanceof Integer)
+ return (Integer) v;
+ }
+ return 0;
+ }
+
+ public static boolean evalBoolean(String expr, Map<String, Object> vars) {
+ expr = expr.trim();
+ int sl = expr.length();
+ if (sl == 0)
+ throw new IllegalArgumentException("Cannot interpret empty string.");
+
+ if (expr.equalsIgnoreCase("true"))
+ return true;
+
+ if (expr.equalsIgnoreCase("false"))
+ return false;
+
+ // Remove parentheses if any
+ if (expr.charAt(0) == '(' && expr.charAt(sl - 1) == ')')
+ return evalBoolean(expr.substring(1, sl - 1), vars);
+
+ // Look for operators in the order of least priority
+ String[] sss = findOperator(expr, "or", true);
+ if (sss != null)
+ return evalBoolean(sss[0], vars) || evalBoolean(sss[1], vars);
+
+ sss = findOperator(expr, "and", true);
+ if (sss != null)
+ return evalBoolean(sss[0], vars) && evalBoolean(sss[1], vars);
+
+ sss = findOperator(expr, "not", true);
+ if (sss != null)
+ return !evalBoolean(sss[1], vars);
+
+ sss = findOperator(expr, "!=", false);
+ if (sss == null)
+ sss = findOperator(expr, "<>", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) != evalLong(sss[1], vars);
+
+ sss = findOperator(expr, "==", false);
+ if (sss == null)
+ sss = findOperator(expr, "=", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) == evalLong(sss[1], vars);
+
+ sss = findOperator(expr, ">=", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) >= evalLong(sss[1], vars);
+
+ sss = findOperator(expr, ">", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) > evalLong(sss[1], vars);
+
+ sss = findOperator(expr, "<=", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) <= evalLong(sss[1], vars);
+
+ sss = findOperator(expr, "<", false);
+ if (sss != null)
+ return evalLong(sss[0], vars) < evalLong(sss[1], vars);
+
+ throw new IllegalArgumentException("Cannot interpret '" + expr + "': Invalid expression.");
+ }
+
+ private static String[] findOperator(String s, String op, boolean delimiterRequired) {
+ int opl = op.length();
+ int sl = s.length();
+ String delimiters = " \0\t\r\n()";
+ int pcount = 0, qcount = 0;
+ for (int i = 0; i < sl; i++) {
+ char c = s.charAt(i);
+ if (c == '(' && qcount == 0)
+ pcount++;
+ else if (c == ')' && qcount == 0) {
+ pcount--;
+ if (pcount < 0)
+ throw new IllegalArgumentException("Cannot interpret '" + s + "': Parentheses do not match.");
+ } else if (c == '\'')
+ qcount = (qcount + 1) % 2;
+ else if (i <= sl - opl && pcount == 0 && qcount == 0) {
+ String ss = s.substring(i, i + opl);
+ if (ss.equalsIgnoreCase(op)) {
+ boolean found = true;
+ if (delimiterRequired) {
+ // Check for delimiter before and after to make sure it is not part of another word
+ char chbefore = '\0';
+ if (i > 0)
+ chbefore = s.charAt(i - 1);
+ char chafter = '\0';
+ if (i < sl - opl)
+ chafter = s.charAt(i + opl);
+ found = delimiters.indexOf(chbefore) >= 0 && delimiters.indexOf(chafter) >= 0;
+ }
+ if (found) {
+ // We've found the operator, split the string
+ String[] sss = new String[2];
+ sss[0] = s.substring(0, i);
+ sss[1] = s.substring(i + opl);
+ return sss;
+ }
+ }
+ }
+ }
+ if (pcount > 0)
+ throw new IllegalArgumentException("Cannot interpret '" + s + "': Parentheses do not match.");
+ if (qcount > 0)
+ throw new IllegalArgumentException("Cannot interpret '" + s + "': No closing '.");
+ return null;
+ }
+
+ private static Object parseObject(String s) {
+ s = s.trim();
+ int sl = s.length();
+ if (sl == 0)
+ throw new IllegalArgumentException("Cannot interpret empty string.");
+ if (s.equalsIgnoreCase("null"))
+ return null;
+ if (s.charAt(0) == '\'') {
+ if (sl < 2 || s.charAt(sl - 1) != '\'')
+ throw new IllegalArgumentException("Cannot interpret '" + s + "': No closing '.");
+ return s.substring(1, sl - 1);
+ }
+ // Not in quotes - must be a number
+ try {
+ return Long.valueOf(s);
+ } catch (Exception e) {
+ }
+ try {
+ return Double.valueOf(s);
+ } catch (Exception e) {
+ throw new IllegalArgumentException("Cannot interpret '" + s + "': Invalid number.");
+ }
+ }
+}
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/speed/SpeedUtil.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/speed/SpeedUtil.java
new file mode 100644
index 0000000..b4c800e
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/speed/SpeedUtil.java
@@ -0,0 +1,41 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.speed;
+
+public class SpeedUtil {
+
+ private long unitFactor = 1000;
+
+ public long convertToKbps(long maxSpeed, String unit) {
+ if (unit.equalsIgnoreCase("kbps"))
+ return maxSpeed;
+ if (unit.equalsIgnoreCase("Mbps"))
+ return maxSpeed * unitFactor;
+ if (unit.equalsIgnoreCase("Gbps"))
+ return maxSpeed * unitFactor * unitFactor;
+ return 0;
+ }
+
+ public void setUnitFactor(long unitFactor) {
+ this.unitFactor = unitFactor;
+ }
+}
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/str/StrUtil.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/str/StrUtil.java
new file mode 100644
index 0000000..7d7f013
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/str/StrUtil.java
@@ -0,0 +1,305 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.str;
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.SortedSet;
+import java.util.TreeSet;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class StrUtil {
+
+ private static final Logger log = LoggerFactory.getLogger(StrUtil.class);
+
+ public static final String INDENT_STR = " ";
+
+ public static void indent(StringBuilder ss, int ind) {
+ for (int i = 0; i < ind; i++)
+ ss.append(INDENT_STR);
+ }
+
+ public static void info(Logger log, Object o) {
+ if (log.isInfoEnabled()) {
+ StringBuilder ss = new StringBuilder();
+ struct(ss, o);
+ log.info(ss.toString());
+ }
+ }
+
+ public static void debug(Logger log, Object o) {
+ if (log.isDebugEnabled()) {
+ StringBuilder ss = new StringBuilder();
+ struct(ss, o);
+ log.debug(ss.toString());
+ }
+ }
+
+ public static void struct(StringBuilder ss, Object o) {
+ struct(ss, o, 0);
+ }
+
+ public static void struct(StringBuilder ss, Object o, int ind) {
+ if (o == null) {
+ ss.append("null");
+ return;
+ }
+
+ if (isSimple(o)) {
+ ss.append(o);
+ return;
+ }
+
+ Class<? extends Object> cls = o.getClass();
+
+ if (cls.isEnum()) {
+ ss.append(o);
+ return;
+ }
+
+ if (cls.isArray()) {
+ int n = Array.getLength(o);
+ if (n == 0) {
+ ss.append("[]");
+ return;
+ }
+
+ Object o1 = Array.get(o, 0);
+ if (isSimple(o1)) {
+ ss.append('[').append(o1);
+ for (int i = 1; i < n; i++) {
+ o1 = Array.get(o, i);
+ ss.append(", ").append(o1);
+ }
+ ss.append(']');
+ return;
+ }
+
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append('[');
+ struct(ss, o1, ind + 1);
+ for (int i = 1; i < n; i++) {
+ o1 = Array.get(o, i);
+ struct(ss, o1, ind + 1);
+ }
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append(']');
+ return;
+ }
+
+ if (o instanceof Collection<?>) {
+ Collection<?> ll = (Collection<?>) o;
+
+ int n = ll.size();
+ if (n == 0) {
+ ss.append("[]");
+ return;
+ }
+
+ Iterator<?> ii = ll.iterator();
+ Object o1 = ii.next();
+ if (isSimple(o1)) {
+ ss.append('[').append(o1);
+ while (ii.hasNext()) {
+ o1 = ii.next();
+ ss.append(", ").append(o1);
+ }
+ ss.append(']');
+ return;
+ }
+
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append('[');
+ struct(ss, o1, ind + 1);
+ while (ii.hasNext()) {
+ o1 = ii.next();
+ struct(ss, o1, ind + 1);
+ }
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append(']');
+ return;
+
+ }
+
+ if (o instanceof Map<?, ?>) {
+ Map<?, ?> mm = (Map<?, ?>) o;
+
+ int n = mm.size();
+ if (n == 0) {
+ ss.append("{}");
+ return;
+ }
+
+ ss.append('{');
+
+ for (Object k : mm.keySet()) {
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append(k).append(": ");
+
+ Object o1 = mm.get(k);
+ struct(ss, o1, ind + 2);
+ }
+
+ ss.append('\n');
+ indent(ss, ind);
+ ss.append('}');
+
+ return;
+ }
+
+ Field[] fields = cls.getFields();
+
+ if (fields.length == 0) {
+ ss.append(o);
+ return;
+ }
+
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append('<').append(cls.getSimpleName()).append("> {");
+ for (Field f : fields) {
+ ss.append('\n');
+ indent(ss, ind + 2);
+ ss.append(f.getName()).append(": ");
+ Object v = null;
+ try {
+ v = f.get(o);
+ } catch (IllegalAccessException e) {
+ v = "*** Cannot obtain value *** : " + e.getMessage();
+ }
+ struct(ss, v, ind + 2);
+ }
+ ss.append('\n');
+ indent(ss, ind + 1);
+ ss.append('}');
+ }
+
+ public static SortedSet<Integer> listInt(String ss, String warning) {
+ if (ss == null || ss.length() == 0)
+ return null;
+
+ SortedSet<Integer> ll = new TreeSet<Integer>();
+ String[] str = ss.split(",");
+ for (String s : str) {
+ try {
+ int i1 = s.indexOf('-');
+ int start, end;
+ if (i1 > 0) {
+ String s1 = s.substring(0, i1);
+ String s2 = s.substring(i1 + 1);
+ start = Integer.parseInt(s1);
+ end = Integer.parseInt(s2);
+ } else
+ start = end = Integer.parseInt(s);
+ for (int i = start; i <= end; i++)
+ ll.add(i);
+ } catch (NumberFormatException e) {
+ // Skip this - bad data in DB
+ log.warn(warning + " [" + s + "].", e);
+ }
+ }
+ return ll;
+ }
+
+ public static String listInt(SortedSet<Integer> ll) {
+ if (ll == null || ll.size() == 0)
+ return null;
+
+ StringBuilder sb = new StringBuilder(2000);
+ Iterator<Integer> i = ll.iterator();
+ int n = i.next();
+ int start = n;
+ int end = n;
+ boolean first = true;
+ while (i.hasNext()) {
+ n = i.next();
+ if (n != end + 1) {
+ if (!first)
+ sb.append(',');
+ first = false;
+
+ if (start == end)
+ sb.append(start);
+ else if (start == end - 1)
+ sb.append(start).append(',').append(end);
+ else
+ sb.append(start).append('-').append(end);
+
+ start = n;
+ }
+ end = n;
+ }
+
+ if (!first)
+ sb.append(',');
+
+ if (start == end)
+ sb.append(start);
+ else if (start == end - 1)
+ sb.append(start).append(',').append(end);
+ else
+ sb.append(start).append('-').append(end);
+
+ return sb.toString();
+ }
+
+ public static List<String> listStr(String s) {
+ if (s == null || s.length() == 0)
+ return null;
+ String[] ss = s.split(",");
+ return Arrays.asList(ss);
+ }
+
+ public static String listStr(Collection<String> ll) {
+ if (ll == null || ll.isEmpty())
+ return null;
+ StringBuilder ss = new StringBuilder(1000);
+ Iterator<String> i = ll.iterator();
+ ss.append(i.next());
+ while (i.hasNext())
+ ss.append(',').append(i.next());
+ return ss.toString();
+ }
+
+ private static boolean isSimple(Object o) {
+ if (o == null)
+ return true;
+
+ if (o instanceof Number || o instanceof String || o instanceof Boolean || o instanceof Date)
+ return true;
+
+ return false;
+ }
+}
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VpnParam.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VpnParam.java
new file mode 100644
index 0000000..0731cc0
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VpnParam.java
@@ -0,0 +1,30 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.vrf;
+
+public class VpnParam {
+
+ public String vpnId;
+ public String siteType;
+ public String spokeServiceInstanceId;
+ public String routeGroupName;
+}
diff --git a/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VrfUtil.java b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VrfUtil.java
new file mode 100644
index 0000000..0ce7206
--- /dev/null
+++ b/resource-assignment/provider/src/main/java/org/openecomp/sdnc/util/vrf/VrfUtil.java
@@ -0,0 +1,76 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-C
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdnc.util.vrf;
+
+public class VrfUtil {
+
+ public static String createVrfInstanceName(
+ String serviceInstanceId,
+ String vpnId,
+ String siteType,
+ String routeGroup) {
+ if (vpnId == null || vpnId.trim().length() == 0)
+ return null;
+
+ String ss = "VPN-" + vpnId;
+ if (siteType != null && siteType.equalsIgnoreCase("hub"))
+ ss += "-HUB";
+ if (siteType != null && siteType.equalsIgnoreCase("spoke"))
+ ss += "-SP-" + serviceInstanceId;
+ if (routeGroup != null && routeGroup.trim().length() > 0)
+ ss += "-RG-" + routeGroup;
+
+ return ss;
+ }
+
+ public static VpnParam parseVrfInstanceName(String vrfInstanceName) {
+ VpnParam vpnParam = new VpnParam();
+
+ int i1 = vrfInstanceName.indexOf("-HUB");
+ if (i1 > 0)
+ vpnParam.siteType = "HUB";
+
+ int i2 = vrfInstanceName.indexOf("-SP-");
+ if (i2 > 0)
+ vpnParam.siteType = "SPOKE";
+
+ int i3 = vrfInstanceName.indexOf("-RG-");
+ if (i3 > 0)
+ vpnParam.routeGroupName = vrfInstanceName.substring(i3 + 4);
+
+ int i4 = vrfInstanceName.length();
+ if (i1 > 0)
+ i4 = i1;
+ else if (i2 > 0)
+ i4 = i2;
+ else if (i3 > 0)
+ i4 = i3;
+ vpnParam.vpnId = vrfInstanceName.substring(4, i4);
+
+ if (i2 > 0 && i3 < 0)
+ vpnParam.spokeServiceInstanceId = vrfInstanceName.substring(i2 + 4);
+ if (i2 > 0 && i3 > 0)
+ vpnParam.spokeServiceInstanceId = vrfInstanceName.substring(i2 + 4, i3);
+
+ return vpnParam;
+ }
+}