Merge "BUG-1281: use SchemaContext lookups"
authorTony Tkacik <ttkacik@cisco.com>
Sun, 3 Aug 2014 18:14:14 +0000 (18:14 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Sun, 3 Aug 2014 18:14:14 +0000 (18:14 +0000)
1  2 
opendaylight/md-sal/sal-rest-connector/src/main/java/org/opendaylight/controller/sal/restconf/impl/ControllerContext.java

index 39f4eef657bd0d06d42c7dd7c6cbbb222a97c856,ce73132269001e01b8336e415b25cd6e22250268..befc9f2c65e5685204049d78ca5f9164185c653e
@@@ -9,7 -9,6 +9,7 @@@ package org.opendaylight.controller.sal
  
  import com.google.common.base.Function;
  import com.google.common.base.Objects;
 +import com.google.common.base.Optional;
  import com.google.common.base.Preconditions;
  import com.google.common.base.Predicate;
  import com.google.common.base.Splitter;
@@@ -18,6 -17,7 +18,6 @@@ import com.google.common.collect.BiMap
  import com.google.common.collect.HashBiMap;
  import com.google.common.collect.ImmutableMap;
  import com.google.common.collect.Iterables;
 -
  import java.io.UnsupportedEncodingException;
  import java.net.URI;
  import java.net.URLDecoder;
@@@ -31,9 -31,11 +31,9 @@@ import java.util.List
  import java.util.Map;
  import java.util.Set;
  import java.util.concurrent.atomic.AtomicReference;
 -
  import javax.ws.rs.core.Response.Status;
 -
 -import org.opendaylight.controller.sal.core.api.mount.MountInstance;
 -import org.opendaylight.controller.sal.core.api.mount.MountService;
 +import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
 +import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
  import org.opendaylight.controller.sal.rest.api.Draft02;
  import org.opendaylight.controller.sal.rest.impl.RestUtil;
  import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
@@@ -89,13 -91,13 +89,13 @@@ public class ControllerContext implemen
              new AtomicReference<>(Collections.<QName, RpcDefinition>emptyMap());
  
      private volatile SchemaContext globalSchema;
 -    private volatile MountService mountService;
 +    private volatile DOMMountPointService mountService;
  
      public void setGlobalSchema(final SchemaContext globalSchema) {
          this.globalSchema = globalSchema;
      }
  
 -    public void setMountService(final MountService mountService) {
 +    public void setMountService(final DOMMountPointService mountService) {
          this.mountService = mountService;
      }
  
          }
  
          InstanceIdentifierBuilder builder = YangInstanceIdentifier.builder();
-         Module latestModule = this.getLatestModule(globalSchema, startModule);
+         Module latestModule = globalSchema.findModuleByName(startModule, null);
          InstanceIdWithSchemaNode iiWithSchemaNode = this.collectPathArguments(builder, pathArgs, latestModule, null,
                  toMountPointIdentifier);
  
  
          return list;
      }
-     private Module getLatestModule(final SchemaContext schema, final String moduleName) {
-         Preconditions.checkArgument(schema != null);
-         Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
-         Predicate<Module> filter = new Predicate<Module>() {
-             @Override
-             public boolean apply(final Module m) {
-                 return Objects.equal(m.getName(), moduleName);
-             }
-         };
-         Iterable<Module> modules = Iterables.filter(schema.getModules(), filter);
-         return this.filterLatestModule(modules);
-     }
-     private Module filterLatestModule(final Iterable<Module> modules) {
-         Module latestModule = modules.iterator().hasNext() ? modules.iterator().next() : null;
-         for (final Module module : modules) {
-             if (module.getRevision().after(latestModule.getRevision())) {
-                 latestModule = module;
-             }
-         }
-         return latestModule;
-     }
      public Module findModuleByName(final String moduleName) {
          this.checkPreconditions();
          Preconditions.checkArgument(moduleName != null && !moduleName.isEmpty());
-         return this.getLatestModule(globalSchema, moduleName);
+         return globalSchema.findModuleByName(moduleName, null);
      }
  
 -    public Module findModuleByName(final MountInstance mountPoint, final String moduleName) {
 +    public Module findModuleByName(final DOMMountPoint mountPoint, final String moduleName) {
          Preconditions.checkArgument(moduleName != null && mountPoint != null);
  
          final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-         return mountPointSchema == null ? null : this.getLatestModule(mountPointSchema, moduleName);
+         if (mountPointSchema == null) {
+             return null;
+         }
+         return mountPointSchema.findModuleByName(moduleName, null);
      }
  
      public Module findModuleByNamespace(final URI namespace) {
          this.checkPreconditions();
          Preconditions.checkArgument(namespace != null);
-         final Set<Module> moduleSchemas = globalSchema.findModuleByNamespace(namespace);
-         return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
+         return globalSchema.findModuleByNamespaceAndRevision(namespace, null);
      }
  
 -    public Module findModuleByNamespace(final MountInstance mountPoint, final URI namespace) {
 +    public Module findModuleByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
          Preconditions.checkArgument(namespace != null && mountPoint != null);
  
          final SchemaContext mountPointSchema = mountPoint.getSchemaContext();
-         Set<Module> moduleSchemas = mountPointSchema == null ? null : mountPointSchema.findModuleByNamespace(namespace);
-         return moduleSchemas == null ? null : this.filterLatestModule(moduleSchemas);
+         if (mountPointSchema == null) {
+             return null;
+         }
+         return mountPointSchema.findModuleByNamespaceAndRevision(namespace, null);
      }
  
      public Module findModuleByNameAndRevision(final QName module) {
          return globalSchema.findModuleByName(module.getLocalName(), module.getRevision());
      }
  
 -    public Module findModuleByNameAndRevision(final MountInstance mountPoint, final QName module) {
 +    public Module findModuleByNameAndRevision(final DOMMountPoint mountPoint, final QName module) {
          this.checkPreconditions();
          Preconditions.checkArgument(module != null && module.getLocalName() != null && module.getRevision() != null
                  && mountPoint != null);
          return moduleName;
      }
  
 -    public String findModuleNameByNamespace(final MountInstance mountPoint, final URI namespace) {
 +    public String findModuleNameByNamespace(final DOMMountPoint mountPoint, final URI namespace) {
          final Module module = this.findModuleByNamespace(mountPoint, namespace);
          return module == null ? null : module.getName();
      }
          return namespace;
      }
  
 -    public URI findNamespaceByModuleName(final MountInstance mountPoint, final String moduleName) {
 +    public URI findNamespaceByModuleName(final DOMMountPoint mountPoint, final String moduleName) {
          final Module module = this.findModuleByName(mountPoint, moduleName);
          return module == null ? null : module.getNamespace();
      }
  
 -    public Set<Module> getAllModules(final MountInstance mountPoint) {
 +    public Set<Module> getAllModules(final DOMMountPoint mountPoint) {
          this.checkPreconditions();
  
          SchemaContext schemaContext = mountPoint == null ? null : mountPoint.getSchemaContext();
          return builder.toString();
      }
  
 -    public CharSequence toRestconfIdentifier(final MountInstance mountPoint, final QName qname) {
 +    public CharSequence toRestconfIdentifier(final DOMMountPoint mountPoint, final QName qname) {
          if (mountPoint == null) {
              return null;
          }
      }
  
      private InstanceIdWithSchemaNode collectPathArguments(final InstanceIdentifierBuilder builder,
 -            final List<String> strings, final DataNodeContainer parentNode, final MountInstance mountPoint,
 +            final List<String> strings, final DataNodeContainer parentNode, final DOMMountPoint mountPoint,
              final boolean returnJustMountPoint) {
          Preconditions.<List<String>> checkNotNull(strings);
  
                  }
  
                  final YangInstanceIdentifier partialPath = builder.toInstance();
 -                final MountInstance mount = mountService.getMountPoint(partialPath);
 -                if (mount == null) {
 +                final Optional<DOMMountPoint> mountOpt = mountService.getMountPoint(partialPath);
 +                if (!mountOpt.isPresent()) {
                      LOG.debug("Instance identifier to missing mount point: {}", partialPath);
                      throw new RestconfDocumentedException("Mount point does not exist.", ErrorType.PROTOCOL,
                              ErrorTag.UNKNOWN_ELEMENT);
                  }
 +                DOMMountPoint mount = mountOpt.get();
  
                  final SchemaContext mountPointSchema = mount.getSchemaContext();
                  if (mountPointSchema == null) {
                              ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
                  }
  
-                 final Module moduleBehindMountPoint = this
-                         .getLatestModule(mountPointSchema, moduleNameBehindMountPoint);
+                 final Module moduleBehindMountPoint = mountPointSchema.findModuleByName(moduleNameBehindMountPoint, null);
                  if (moduleBehindMountPoint == null) {
                      throw new RestconfDocumentedException("\"" + moduleName
                              + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
  
              Module module = null;
              if (mountPoint == null) {
-                 module = this.getLatestModule(globalSchema, moduleName);
+                 module = globalSchema.findModuleByName(moduleName, null);
                  if (module == null) {
                      throw new RestconfDocumentedException("\"" + moduleName + "\" module does not exist.",
                              ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
                  }
              } else {
                  SchemaContext schemaContext = mountPoint.getSchemaContext();
-                 module = schemaContext == null ? null : this.getLatestModule(schemaContext, moduleName);
+                 if (schemaContext != null) {
+                     module = schemaContext.findModuleByName(moduleName, null);
+                 } else {
+                     module = null;
+                 }
                  if (module == null) {
                      throw new RestconfDocumentedException("\"" + moduleName
                              + "\" module does not exist in mount point.", ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
      }
  
      private void addKeyValue(final HashMap<QName, Object> map, final DataSchemaNode node, final String uriValue,
 -            final MountInstance mountPoint) {
 +            final DOMMountPoint mountPoint) {
          Preconditions.<String> checkNotNull(uriValue);
          Preconditions.checkArgument((node instanceof LeafSchemaNode));