Cleanup bgp-openconfig-api helpers 70/78770/4
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 13 Dec 2018 20:14:30 +0000 (21:14 +0100)
committerRobert Varga <nite@hq.sk>
Fri, 14 Dec 2018 06:28:08 +0000 (06:28 +0000)
This cleans up the custom-coded helpers to work more efficiently,
fixing eclipse warnings at the same time.

Change-Id: I3724a478b73e27382bc1f49766603e16a92396e5
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
bgp/openconfig-api/src/main/java/org/opendaylight/yang/gen/v1/http/openconfig/net/yang/bgp/policy/rev151009/BgpSetMedTypeBuilder.java
bgp/openconfig-api/src/main/java/org/opendaylight/yang/gen/v1/http/openconfig/net/yang/bgp/types/rev151009/BgpStdCommunityTypeBuilder.java
bgp/openconfig-api/src/main/java/org/opendaylight/yang/gen/v1/http/openconfig/net/yang/bgp/types/rev151009/RrClusterIdTypeBuilder.java
bgp/openconfig-api/src/main/java/org/opendaylight/yang/gen/v1/http/openconfig/net/yang/network/instance/types/rev151018/RouteDistinguisherBuilder.java

index ee8718937c98b2af4a1ec5013fef0c89ecdf86ea..265de3ca049657b7b662c26f15c6c40db193aa6d 100644 (file)
@@ -5,45 +5,36 @@
  * 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.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009;
 
-import com.google.common.primitives.UnsignedInts;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.policy.rev151009.BgpSetMedType.Enumeration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
- * The purpose of generated class in src/main/java for Union types is to create new instances of unions from a string representation.
- * In some cases it is very difficult to automate it since there can be unions such as (uint32 - uint16), or (string - uint32).
- *
- * The reason behind putting it under src/main/java is:
- * This class is generated in form of a stub and needs to be finished by the user. This class is generated only once to prevent
- * loss of user code.
- *
+ * Customized handler for instantiating {@link BgpSetMedType} from a String.
  */
-public class BgpSetMedTypeBuilder {
+public final class BgpSetMedTypeBuilder {
+    private static final Logger LOG = LoggerFactory.getLogger(BgpSetMedTypeBuilder.class);
+    private static final Pattern MED_TYPE_STRING_PATTERN = Pattern.compile("^[+-][0-9]+$");
 
-    private static final Pattern MED_TYPE_STRING_PATTERN = Pattern.compile("^^[+-][0-9]+$");
-
-    public static BgpSetMedType getDefaultInstance(final java.lang.String defaultValue) {
-        final Matcher ipv4Matcher = MED_TYPE_STRING_PATTERN.matcher(defaultValue);
+    private BgpSetMedTypeBuilder() {
+        // Hidden
+    }
 
-        if (ipv4Matcher.matches()) {
+    public static BgpSetMedType getDefaultInstance(final String defaultValue) {
+        if (MED_TYPE_STRING_PATTERN.matcher(defaultValue).matches()) {
             return new BgpSetMedType(defaultValue);
-        } else {
-            try {
-                final long parseUnsignedInt = UnsignedInts.parseUnsignedInt(defaultValue);
-                return new BgpSetMedType(parseUnsignedInt);
-            } catch (final NumberFormatException e) {
-                try {
-                    final Enumeration medTypeEnum = BgpSetMedType.Enumeration.valueOf(defaultValue.toUpperCase());
-                    return new BgpSetMedType(medTypeEnum);
-                } catch(final IllegalArgumentException e1) {
-                    throw new IllegalArgumentException("Cannot create BgpSetMedType from " + defaultValue);
-                }
-            }
         }
-    }
 
+        try {
+            return new BgpSetMedType(Integer.toUnsignedLong(Integer.parseUnsignedInt(defaultValue)));
+        } catch (final NumberFormatException e) {
+            LOG.debug("Could not interpret \"{}\" as an unsinged integer", defaultValue, e);
+        }
+
+        return new BgpSetMedType(Enumeration.forName(defaultValue.toUpperCase())
+                .orElseThrow(() -> new IllegalArgumentException("Invalid BgpSetMedType " + defaultValue)));
+    }
 }
index 14e67e9d9bd885583183253351d749492d13be2a..6dde0be93f5fe4c61df1ea36ed2d0e1eda4c5857 100644 (file)
@@ -5,39 +5,26 @@
  * 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.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009;
 
-import com.google.common.primitives.UnsignedInts;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
 /**
- * The purpose of generated class in src/main/java for Union types is to create new instances of unions from a string representation.
- * In some cases it is very difficult to automate it since there can be unions such as (uint32 - uint16), or (string - uint32).
- *
- * The reason behind putting it under src/main/java is:
- * This class is generated in form of a stub and needs to be finished by the user. This class is generated only once to prevent
- * loss of user code.
- *
+ * Customized handler for instantiating {@link BgpStdCommunityType} from a String.
  */
-public class BgpStdCommunityTypeBuilder {
-
-    private static final Pattern COMM_TYPE_PATTERN = Pattern.compile("^([0-9]+:[0-9]+)$");
-
-    public static BgpStdCommunityType getDefaultInstance(final java.lang.String defaultValue) {
-        final Matcher commMatcher = COMM_TYPE_PATTERN.matcher(defaultValue);
+public final class BgpStdCommunityTypeBuilder {
+    private BgpStdCommunityTypeBuilder() {
+        // Hidden
+    }
 
-        if (commMatcher.matches()) {
+    public static BgpStdCommunityType getDefaultInstance(final String defaultValue) {
+        // uints cannot have a ':', which is allowed for four-octets
+        if (defaultValue.indexOf(':') != -1) {
             return new BgpStdCommunityType(defaultValue);
-        } else {
-            try {
-                final long parseUnsignedInt = UnsignedInts.parseUnsignedInt(defaultValue);
-                return new BgpStdCommunityType(parseUnsignedInt);
-            } catch (final NumberFormatException e) {
-                throw new IllegalArgumentException("Cannot create BgpStdCommunityType from " + defaultValue);
-            }
         }
-    }
 
+        try {
+            return new BgpStdCommunityType(Integer.toUnsignedLong(Integer.parseUnsignedInt(defaultValue)));
+        } catch (final NumberFormatException e) {
+            throw new IllegalArgumentException("Cannot create BgpStdCommunityType from " + defaultValue, e);
+        }
+    }
 }
index 63e4cd6833460ab74eb8e200dcdfc3bfdbab9181..5002fb5042baf3f7c34c319688d7198c0a09265e 100644 (file)
@@ -5,40 +5,28 @@
  * 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.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009;
 
-import com.google.common.primitives.UnsignedInts;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
 
 /**
- * The purpose of generated class in src/main/java for Union types is to create new instances of unions from a string representation.
- * In some cases it is very difficult to automate it since there can be unions such as (uint32 - uint16), or (string - uint32).
- *
- * The reason behind putting it under src/main/java is:
- * This class is generated in form of a stub and needs to be finished by the user. This class is generated only once to prevent
- * loss of user code.
- *
+ * Customized handler for instantiating {@link RrClusterIdType} from a String.
  */
-public class RrClusterIdTypeBuilder {
-
-    private static final Pattern IPV4_PATTERN = Pattern.compile("^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(%[\\p{N}\\p{L}]+)?$");
+public final class RrClusterIdTypeBuilder {
+    private RrClusterIdTypeBuilder() {
+        // Hidden
+    }
 
-    public static RrClusterIdType getDefaultInstance(final java.lang.String defaultValue) {
-        final Matcher ipv4Matcher = IPV4_PATTERN.matcher(defaultValue);
-        if (ipv4Matcher.matches()) {
+    public static RrClusterIdType getDefaultInstance(final String defaultValue) {
+        // IPv4 has to have a dot in it
+        if (defaultValue.indexOf('.') != -1) {
             return new RrClusterIdType(new Ipv4Address(defaultValue));
-        } else {
-            try {
-                final long parseUnsignedInt = UnsignedInts.parseUnsignedInt(defaultValue);
-                return new RrClusterIdType(parseUnsignedInt);
-            } catch (final NumberFormatException e) {
-                throw new IllegalArgumentException("Cannot create RrClusterIdType from " + defaultValue);
-            }
         }
-    }
 
+        try {
+            return new RrClusterIdType(Integer.toUnsignedLong(Integer.parseUnsignedInt(defaultValue)));
+        } catch (final NumberFormatException e) {
+            throw new IllegalArgumentException("Cannot create RrClusterIdType from " + defaultValue, e);
+        }
+    }
 }
index d8b092d00d26fa989089c651104f9b29a1d3ba0a..0e6739331ebfeac3aba92a996f23378be6614757 100644 (file)
@@ -5,40 +5,26 @@
  * 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.yang.gen.v1.http.openconfig.net.yang.network.instance.types.rev151018;
 
 import java.util.regex.Pattern;
+import org.opendaylight.yangtools.yang.binding.CodeHelpers;
 
 /**
- *
  * Helper builder utility for {@code RouteDistinguisher} union type.
- *
  */
 public final class RouteDistinguisherBuilder {
+    private static final Pattern[] PATTERNS = CodeHelpers.compilePatterns(RouteDistinguisher.PATTERN_CONSTANTS);
 
     private RouteDistinguisherBuilder() {
         throw new UnsupportedOperationException();
     }
 
-    private static final Pattern[] PATTERNS;
-
-    static {
-        final Pattern a[] = new Pattern[RouteDistinguisher.PATTERN_CONSTANTS.size()];
-        int i = 0;
-        for (final String regEx : RouteDistinguisher.PATTERN_CONSTANTS) {
-            a[i++] = Pattern.compile(regEx);
-        }
-
-        PATTERNS = a;
-    }
-
     public static RouteDistinguisher getDefaultInstance(final String defaultValue) {
         if (anyMatch(defaultValue)) {
             return new RouteDistinguisher(defaultValue);
-        } else {
-            throw new IllegalArgumentException("Cannot create RouteDistinguisher from " + defaultValue);
         }
+        throw new IllegalArgumentException("Cannot create RouteDistinguisher from " + defaultValue);
     }
 
     private static boolean anyMatch(final String defaultValue) {