Fix derived types missing pattern restrictions 49/70749/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 10 Apr 2018 17:14:19 +0000 (19:14 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Apr 2018 08:38:08 +0000 (10:38 +0200)
We have missed a place where constants to support restrictions need
to be generated, which meant that Ipv4AddressNoZone and similar
constructs were not doing correct enforcement.

JIRA: MDSAL-335
Change-Id: Id659362038e001207eab9ca5f1618c376aa0ea5a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit a2713a39a1b1aac6a2337a09559f930240523311)

binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/yang/types/TypeProviderImpl.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal335Test.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal335.yang [new file with mode: 0644]

index b391190fe0dc67c40a258f8f4d52dff1952a383b..d6fbc761a9645a6a3946e081b2cd48dac875a69f 100644 (file)
@@ -1285,6 +1285,8 @@ public final class TypeProviderImpl implements TypeProvider {
         genTOBuilder.setTypedef(true);
         final Restrictions r = BindingGeneratorUtil.getRestrictions(typedef);
         genTOBuilder.setRestrictions(r);
+        addStringRegExAsConstant(genTOBuilder, resolveRegExpressionsFromTypedef(typedef));
+
         if (typedef.getStatus() == Status.DEPRECATED) {
             genTOBuilder.addAnnotation("", "Deprecated");
         }
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal335Test.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal335Test.java
new file mode 100644 (file)
index 0000000..6e8f084
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.mdsal.binding.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.FileNotFoundException;
+import java.net.URISyntaxException;
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.GeneratedType;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal335Test {
+
+    @Test
+    public void mdsal335Test() throws FileNotFoundException, ReactorException, URISyntaxException {
+        final SchemaContext context = YangParserTestUtils.parseYangSource("/mdsal335.yang");
+
+        final List<Type> generateTypes = new BindingGeneratorImpl(false).generateTypes(context);
+        assertNotNull(generateTypes);
+        assertEquals(2, generateTypes.size());
+
+        final Type nozoneType = generateTypes.stream().filter(type -> type.getFullyQualifiedName()
+            .equals("org.opendaylight.yang.gen.v1.mdsal335.rev700101.Ipv4AddressNoZone")).findFirst().get();
+
+        assertTrue(nozoneType instanceof GeneratedType);
+        final GeneratedType gen = (GeneratedType) nozoneType;
+        assertEquals(1, gen.getConstantDefinitions().size());
+    }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal335.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal335.yang
new file mode 100644 (file)
index 0000000..c6a320a
--- /dev/null
@@ -0,0 +1,20 @@
+module mdsal335 {
+    namespace "mdsal335";
+    prefix "mdsal335";
+
+    typedef ipv4-address {
+        type string {
+            pattern
+                '(([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}]+)?';
+        }
+    }
+
+    typedef ipv4-address-no-zone {
+        type mdsal335:ipv4-address {
+            pattern '[0-9\.]*';
+        }
+    }
+}
+