Provide single point for enforcing config naming convention
[controller.git] / opendaylight / configuration / api / src / main / java / org / opendaylight / controller / configuration / ConfigurationObject.java
diff --git a/opendaylight/configuration/api/src/main/java/org/opendaylight/controller/configuration/ConfigurationObject.java b/opendaylight/configuration/api/src/main/java/org/opendaylight/controller/configuration/ConfigurationObject.java
new file mode 100644 (file)
index 0000000..34542de
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.configuration;
+
+import java.io.Serializable;
+
+public abstract class ConfigurationObject implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private static final String DEFAULT_REGEX = "^[\\w-\\+\\*\\/\\.\\(\\)\\[\\]\\@]{1,256}$";
+    private static final String REGEX_PROP_NAME = "resourceNameRegularExpression";
+    private static String regex;
+
+    static {
+        String customRegex = System.getProperty(REGEX_PROP_NAME);
+        regex = (customRegex != null) ? customRegex : DEFAULT_REGEX;
+    }
+
+    /**
+     * Checks if the provided resource name matches the controller resource name
+     * regular expression
+     *
+     * @param name
+     *            The resource name to test
+     * @return true if the resource name is not null and matches the controller
+     *         resource name regular expression, false otherwise
+     */
+    protected boolean isValidResourceName(String name) {
+        return (name != null) ? name.matches(regex) : false;
+    }
+
+    /**
+     * Return the regular expression currently in use for testing the controller
+     * resource names
+     *
+     * @return The regular expression
+     */
+    public static String getRegularExpression() {
+        return regex;
+    }
+}