Remove use of getDataChildByName()
[controller.git] / opendaylight / blueprint / src / main / java / org / opendaylight / controller / blueprint / ext / DataStoreAppConfigDefaultXMLReader.java
index 709e0b4bab1ebeff8d2563a068094d3fc7c4f7e9..6f323716ee481a2bb5e015b9b57e985fdfaf8d3b 100644 (file)
@@ -7,23 +7,24 @@
  */
 package org.opendaylight.controller.blueprint.ext;
 
-import com.google.common.base.Optional;
 import com.google.common.base.Strings;
 import com.google.common.io.Resources;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URISyntaxException;
 import java.net.URL;
+import java.util.Optional;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.stream.XMLStreamException;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
@@ -45,14 +46,14 @@ public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
 
     private final String logName;
     private final String defaultAppConfigFileName;
-    private final SchemaService schemaService;
+    private final DOMSchemaService schemaService;
     private final BindingNormalizedNodeSerializer bindingSerializer;
     private final BindingContext bindingContext;
     private final ConfigURLProvider inputStreamProvider;
 
     @FunctionalInterface
     public interface FallbackConfigProvider {
-        NormalizedNode<?,?> get(SchemaContext schemaContext, DataSchemaNode dataSchema) throws IOException,
+        NormalizedNode<?,?> get(EffectiveModelContext schemaContext, DataSchemaNode dataSchema) throws IOException,
                 XMLStreamException, ParserConfigurationException, SAXException, URISyntaxException;
     }
 
@@ -64,7 +65,7 @@ public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
     public DataStoreAppConfigDefaultXMLReader(
             final String logName,
             final String defaultAppConfigFileName,
-            final SchemaService schemaService,
+            final DOMSchemaService schemaService,
             final BindingNormalizedNodeSerializer bindingSerializer,
             final BindingContext bindingContext,
             final ConfigURLProvider inputStreamProvider) {
@@ -80,7 +81,7 @@ public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
     public DataStoreAppConfigDefaultXMLReader(
             final Class<?> testClass,
             final String defaultAppConfigFileName,
-            final SchemaService schemaService,
+            final DOMSchemaService schemaService,
             final BindingNormalizedNodeSerializer bindingSerializer,
             final Class<T> klass) {
         this(testClass.getName(), defaultAppConfigFileName, schemaService, bindingSerializer,
@@ -108,33 +109,23 @@ public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
         LOG.debug("{}: Creating app config instance from path {}, Qname: {}", logName, yangPath,
                 bindingContext.bindingQName);
 
-        if (schemaService == null) {
-            throw new ConfigXMLReaderException(
-                    String.format("%s: Could not obtain the SchemaService OSGi service", logName));
-        }
+        checkNotNull(schemaService, "%s: Could not obtain the SchemaService OSGi service", logName);
 
-        SchemaContext schemaContext = schemaService.getGlobalContext();
+        EffectiveModelContext schemaContext = schemaService.getGlobalContext();
 
-        Module module = schemaContext.findModuleByNamespaceAndRevision(bindingContext.bindingQName.getNamespace(),
-                bindingContext.bindingQName.getRevision());
-        if (module == null) {
-            throw new ConfigXMLReaderException(
-                    String.format("%s: Could not obtain the module schema for namespace %s, revision %s",
-                    logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision()));
-        }
+        Module module = schemaContext.findModule(bindingContext.bindingQName.getModule()).orElse(null);
+        checkNotNull(module, "%s: Could not obtain the module schema for namespace %s, revision %s",
+                logName, bindingContext.bindingQName.getNamespace(), bindingContext.bindingQName.getRevision());
 
-        DataSchemaNode dataSchema = module.getDataChildByName(bindingContext.bindingQName);
-        if (dataSchema == null) {
-            throw new ConfigXMLReaderException(
-                    String.format("%s: Could not obtain the schema for %s", logName,
-                    bindingContext.bindingQName));
-        }
+        QName qname = bindingContext.bindingQName;
+        DataSchemaNode dataSchema = module.findDataChildByName(qname).orElseThrow(
+            () -> new ConfigXMLReaderException(logName + ": Could not obtain the schema for " + qname));
 
-        if (!bindingContext.schemaType.isAssignableFrom(dataSchema.getClass())) {
-            throw new ConfigXMLReaderException(
-                    String.format("%s: Expected schema type %s for %s but actual type is %s", logName,
-                    bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass()));
-        }
+        checkNotNull(dataSchema, "%s: Could not obtain the schema for %s", logName, bindingContext.bindingQName);
+
+        checkCondition(bindingContext.schemaType.isAssignableFrom(dataSchema.getClass()),
+                "%s: Expected schema type %s for %s but actual type is %s", logName,
+                bindingContext.schemaType, bindingContext.bindingQName, dataSchema.getClass());
 
         NormalizedNode<?, ?> dataNode = parsePossibleDefaultAppConfigXMLFile(schemaContext, dataSchema);
         if (dataNode == null) {
@@ -142,17 +133,27 @@ public class DataStoreAppConfigDefaultXMLReader<T extends DataObject> {
         }
 
         DataObject appConfig = bindingSerializer.fromNormalizedNode(yangPath, dataNode).getValue();
-        if (appConfig == null) {
-            // This shouldn't happen but need to handle it in case...
-            throw new ConfigXMLReaderException(
-                    String.format("%s: Could not create instance for app config binding %s",
-                    logName, bindingContext.appConfigBindingClass));
-        } else {
-            return (T) appConfig;
+
+        // This shouldn't happen but need to handle it in case...
+        checkNotNull(appConfig, "%s: Could not create instance for app config binding %s", logName,
+                bindingContext.appConfigBindingClass);
+
+        return (T) appConfig;
+    }
+
+    private static void checkNotNull(final Object reference, final String errorMessageFormat,
+            final Object... formatArgs) throws ConfigXMLReaderException {
+        checkCondition(reference != null, errorMessageFormat, formatArgs);
+    }
+
+    private static void checkCondition(final boolean expression, final String errorMessageFormat,
+            final Object... formatArgs) throws ConfigXMLReaderException {
+        if (!expression) {
+            throw new ConfigXMLReaderException(String.format(errorMessageFormat, formatArgs));
         }
     }
 
-    private NormalizedNode<?, ?> parsePossibleDefaultAppConfigXMLFile(final SchemaContext schemaContext,
+    private NormalizedNode<?, ?> parsePossibleDefaultAppConfigXMLFile(final EffectiveModelContext schemaContext,
             final DataSchemaNode dataSchema) throws ConfigXMLReaderException {
 
         String appConfigFileName = defaultAppConfigFileName;