Fix findbugs violations in sal-rest-docgen
[netconf.git] / restconf / sal-rest-docgen / src / main / java / org / opendaylight / netconf / sal / rest / doc / mountpoints / MountPointSwagger.java
index 98268495b86e5ef8069301a20ed7bdddcf755234..e81d1e5c158c37d56ba25bc6978704e100f7ac17 100644 (file)
@@ -8,6 +8,7 @@
 package org.opendaylight.netconf.sal.rest.doc.mountpoints;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
@@ -39,26 +40,26 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
 
     private static final String DATASTORES_REVISION = "-";
     private static final String DATASTORES_LABEL = "Datastores";
+    private static final AtomicReference<MountPointSwagger> SELF_REF = new AtomicReference<>();
 
     private DOMMountPointService mountService;
-    private final Map<YangInstanceIdentifier, Long> instanceIdToLongId = new TreeMap<>(
-            (o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
+    private final Map<YangInstanceIdentifier, Long> instanceIdToLongId =
+            new TreeMap<>((o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
     private final Map<Long, YangInstanceIdentifier> longIdToInstanceId = new HashMap<>();
+
     private final Object lock = new Object();
 
     private final AtomicLong idKey = new AtomicLong(0);
-
-    private static final AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
     private SchemaService globalSchema;
 
     public Map<String, Long> getInstanceIdentifiers() {
+        Preconditions.checkState(globalSchema != null);
         final Map<String, Long> urlToId = new HashMap<>();
-        synchronized (lock) {
-            final SchemaContext context = globalSchema.getGlobalContext();
-            for (final Entry<YangInstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
+        synchronized (this.lock) {
+            final SchemaContext context = this.globalSchema.getGlobalContext();
+            for (final Entry<YangInstanceIdentifier, Long> entry : this.instanceIdToLongId.entrySet()) {
                 final String modName = findModuleName(entry.getKey(), context);
-                urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
-                        entry.getValue());
+                urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName), entry.getValue());
             }
         }
         return urlToId;
@@ -80,28 +81,32 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
 
     private String generateUrlPrefixFromInstanceID(final YangInstanceIdentifier key, final String moduleName) {
         final StringBuilder builder = new StringBuilder();
+        builder.append("/");
         if (moduleName != null) {
-            builder.append(moduleName);
-            builder.append(':');
+            builder.append(moduleName).append(':');
         }
         for (final PathArgument arg : key.getPathArguments()) {
             final String name = arg.getNodeType().getLocalName();
             if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
                 final NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
                 for (final Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
-                    builder.append(entry.getValue()).append('/');
+                    if (isNewDraft()) {
+                        builder.deleteCharAt(builder.length() - 1).append("=").append(entry.getValue()).append('/');
+                    } else {
+                        builder.append(entry.getValue()).append('/');
+                    }
                 }
             } else {
-                builder.append(name);
-                builder.append('/');
+                builder.append(name).append('/');
             }
         }
         return builder.toString();
     }
 
     private String getYangMountUrl(final YangInstanceIdentifier key) {
-        final String modName = findModuleName(key, globalSchema.getGlobalContext());
-        return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount/";
+        Preconditions.checkState(globalSchema != null);
+        final String modName = findModuleName(key, this.globalSchema.getGlobalContext());
+        return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount";
     }
 
     public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
@@ -127,8 +132,8 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
 
     private YangInstanceIdentifier getInstanceId(final Long id) {
         final YangInstanceIdentifier instanceId;
-        synchronized (lock) {
-            instanceId = longIdToInstanceId.get(id);
+        synchronized (this.lock) {
+            instanceId = this.longIdToInstanceId.get(id);
         }
         return instanceId;
     }
@@ -139,7 +144,8 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
             return null;
         }
 
-        final Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
+        Preconditions.checkState(mountService != null);
+        final Optional<DOMMountPoint> mountPoint = this.mountService.getMountPoint(id);
         if (!mountPoint.isPresent()) {
             return null;
         }
@@ -151,7 +157,8 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
         return context;
     }
 
-    public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module, final String revision) {
+    public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module,
+            final String revision) {
         final YangInstanceIdentifier iid = getInstanceId(id);
         final SchemaContext context = getSchemaContext(iid);
         final String urlPrefix = getYangMountUrl(iid);
@@ -167,12 +174,11 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
 
     private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
         final List<Api> apis = new LinkedList<>();
-        apis.add(createGetApi("config",
-                "Queries the config (startup) datastore on the mounted hosted.", context));
-        apis.add(createGetApi("operational",
-                "Queries the operational (running) datastore on the mounted hosted.", context));
-        apis.add(createGetApi("operations",
-                "Queries the available operations (RPC calls) on the mounted hosted.", context));
+        apis.add(createGetApi("config", "Queries the config (startup) datastore on the mounted hosted.", context));
+        apis.add(createGetApi("operational", "Queries the operational (running) datastore on the mounted hosted.",
+                context));
+        apis.add(createGetApi("operations", "Queries the available operations (RPC calls) on the mounted hosted.",
+                context));
 
         final ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
         declaration.setApis(apis);
@@ -187,7 +193,7 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
         getConfig.setNotes(note);
 
         final Api api = new Api();
-        api.setPath(getDataStorePath("/" + datastore + "/", context));
+        api.setPath(getDataStorePath(datastore, context).concat(getContent(datastore)));
         api.setOperations(Collections.singletonList(getConfig));
 
         return api;
@@ -199,28 +205,40 @@ public class MountPointSwagger extends BaseYangSwaggerGenerator implements Mount
 
     @Override
     public void onMountPointCreated(final YangInstanceIdentifier path) {
-        synchronized (lock) {
-            final Long idLong = idKey.incrementAndGet();
-            instanceIdToLongId.put(path, idLong);
-            longIdToInstanceId.put(idLong, path);
+        synchronized (this.lock) {
+            final Long idLong = this.idKey.incrementAndGet();
+            this.instanceIdToLongId.put(path, idLong);
+            this.longIdToInstanceId.put(idLong, path);
         }
     }
 
     @Override
     public void onMountPointRemoved(final YangInstanceIdentifier path) {
-        synchronized (lock) {
-            final Long id = instanceIdToLongId.remove(path);
-            longIdToInstanceId.remove(id);
+        synchronized (this.lock) {
+            final Long id = this.instanceIdToLongId.remove(path);
+            this.longIdToInstanceId.remove(id);
         }
     }
 
     public static MountPointSwagger getInstance() {
-        MountPointSwagger swagger = selfRef.get();
+        MountPointSwagger swagger = SELF_REF.get();
         if (swagger == null) {
-            selfRef.compareAndSet(null, new MountPointSwagger());
-            swagger = selfRef.get();
+            SELF_REF.compareAndSet(null, new MountPointSwagger());
+            swagger = SELF_REF.get();
         }
+
+        swagger.setDraft(false);
         return swagger;
     }
 
+    public static MountPointSwagger getInstanceDraft18() {
+        MountPointSwagger swagger = SELF_REF.get();
+        if (swagger == null) {
+            SELF_REF.compareAndSet(null, new MountPointSwagger());
+            swagger = SELF_REF.get();
+        }
+
+        swagger.setDraft(true);
+        return swagger;
+    }
 }