Add BitsTypeObject
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ClassTemplate.xtend
index 8b59b92460d8f80229f00bfd28d22793a54832fb..df7be75af6500637be2a86e50251c07440370c4a 100644 (file)
@@ -23,12 +23,13 @@ import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT32_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT64_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BaseYangTypes.UINT8_TYPE
 import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.SCALAR_TYPE_OBJECT
+import static org.opendaylight.mdsal.binding.model.ri.BindingTypes.BITS_TYPE_OBJECT
 import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
 import static extension org.apache.commons.text.StringEscapeUtils.escapeJava
-import static extension org.opendaylight.mdsal.binding.model.ri.BindingTypes.isBitsType
 
 import com.google.common.base.Preconditions
 import com.google.common.collect.ImmutableList
+import com.google.common.collect.ImmutableSet
 import com.google.common.collect.Lists
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
 import java.util.ArrayList
@@ -51,6 +52,7 @@ import org.opendaylight.mdsal.binding.model.ri.TypeConstants
 import org.opendaylight.mdsal.binding.model.ri.Types
 import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
 import org.opendaylight.yangtools.yang.common.Empty
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition
 
 /**
  * Template for generating JAVA class.
@@ -73,6 +75,11 @@ class ClassTemplate extends BaseTemplate {
      */
     static val BOOLEAN = JavaTypeName.create(Boolean)
 
+    /**
+     * {@code com.google.common.collect.ImmutableSet} as a JavaTypeName.
+     */
+    static val IMMUTABLE_SET = JavaTypeName.create(ImmutableSet)
+
     protected val List<GeneratedProperty> properties
     protected val List<GeneratedProperty> finalProperties
     protected val List<GeneratedProperty> parentProperties
@@ -179,8 +186,8 @@ class ClassTemplate extends BaseTemplate {
 
             «propertyMethods»
 
-            «IF genTO.isBitsType»
-                «generateGetValueForBitsTypeDef»
+            «IF isBitsTypeObject»
+                «validNamesAndValues»
             «ENDIF»
 
             «generateHashCode»
@@ -208,6 +215,19 @@ class ClassTemplate extends BaseTemplate {
         return false
     }
 
+    def private isBitsTypeObject() {
+        var wlk = genTO
+        while (wlk !== null) {
+            for (impl : wlk.implements) {
+                if (BITS_TYPE_OBJECT.identifier.equals(impl.identifier)) {
+                    return true
+                }
+            }
+            wlk = wlk.superType
+        }
+        return false
+    }
+
     def private defaultProperties() '''
         «FOR field : properties SEPARATOR "\n"»
             «field.getterMethod»
@@ -224,20 +244,29 @@ class ClassTemplate extends BaseTemplate {
         }
     '''
 
-    /**
-     * Template method which generates the method <code>getValue()</code> for typedef,
-     * which base type is BitsDefinition.
-     *
-     * @return string with the <code>getValue()</code> method definition in JAVA format
-     */
-    def protected generateGetValueForBitsTypeDef() '''
+    def private validNamesAndValues() {
+        for (c : consts) {
+            if (TypeConstants.VALID_NAMES_NAME.equals(c.name)) {
+                return validNamesAndValues(c.value as BitsTypeDefinition)
+            }
+        }
+        return ""
+    }
 
-        public boolean[] getValue() {
-            return new boolean[]{
-            «FOR property: genTO.properties SEPARATOR ','»
-                 «property.fieldName»
-            «ENDFOR»
-            };
+    def private validNamesAndValues(BitsTypeDefinition typedef) '''
+
+        @«OVERRIDE.importedName»
+        public «IMMUTABLE_SET.importedName»<«STRING.importedName»> validNames() {
+            return «TypeConstants.VALID_NAMES_NAME»;
+        }
+
+        @«OVERRIDE.importedName»
+        public boolean[] values() {
+            return new boolean[] {
+                    «FOR bit : typedef.bits SEPARATOR ','»
+                        «BindingMapping.getPropertyName(bit.name).getterMethodName»()
+                    «ENDFOR»
+                };
         }
     '''
 
@@ -519,6 +548,11 @@ class ClassTemplate extends BaseTemplate {
                         private static final String[] «Constants.MEMBER_REGEX_LIST» = { «
                         FOR v : cValue.values SEPARATOR ", "»"«v.escapeJava»"«ENDFOR» };
                     «ENDIF»
+                «ELSEIF TypeConstants.VALID_NAMES_NAME.equals(c.name)»
+                    «val cValue = c.value as BitsTypeDefinition»
+                    «val immutableSet = IMMUTABLE_SET.importedName»
+                    protected static final «immutableSet»<«STRING.importedName»> «TypeConstants.VALID_NAMES_NAME» = «immutableSet».of(«
+                    FOR bit : cValue.bits SEPARATOR ", "»"«bit.name»"«ENDFOR»);
                 «ELSE»
                     «emitConstant(c)»
                 «ENDIF»