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.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.AtomicReference;
import javax.ws.rs.core.UriInfo;
import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.controller.sal.core.api.mount.MountProvisionListener;
+import org.opendaylight.mdsal.dom.api.DOMMountPointListener;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
import org.opendaylight.netconf.sal.rest.doc.impl.BaseYangSwaggerGenerator;
import org.opendaylight.netconf.sal.rest.doc.swagger.Api;
import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
import org.opendaylight.netconf.sal.rest.doc.swagger.Operation;
import org.opendaylight.netconf.sal.rest.doc.swagger.Resource;
import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
-import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-public class MountPointSwagger extends BaseYangSwaggerGenerator implements MountProvisionListener {
+public class MountPointSwagger implements DOMMountPointListener, AutoCloseable {
private static final String DATASTORES_REVISION = "-";
private static final String DATASTORES_LABEL = "Datastores";
- private DOMMountPointService mountService;
- private final Map<YangInstanceIdentifier, Long> instanceIdToLongId = new TreeMap<>(
- new Comparator<YangInstanceIdentifier>() {
- @Override
- public int compare(final YangInstanceIdentifier o1, final YangInstanceIdentifier o2) {
- return o1.toString().compareToIgnoreCase(o2.toString());
- }
- });
+ private final DOMSchemaService globalSchema;
+ private final DOMMountPointService mountService;
+ private final BaseYangSwaggerGenerator swaggerGenerator;
+ 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 AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
- private SchemaService globalSchema;
+ private ListenerRegistration<DOMMountPointListener> registration;
+
+ public MountPointSwagger(final DOMSchemaService globalSchema, final DOMMountPointService mountService,
+ final BaseYangSwaggerGenerator swaggerGenerator) {
+ this.globalSchema = Objects.requireNonNull(globalSchema);
+ this.mountService = Objects.requireNonNull(mountService);
+ this.swaggerGenerator = Objects.requireNonNull(swaggerGenerator);
+ }
+
+ public void init() {
+ registration = mountService.registerProvisionListener(this);
+ }
+
+ @Override
+ public void close() {
+ if (registration != null) {
+ registration.close();
+ }
+ }
public Map<String, Long> getInstanceIdentifiers() {
- Map<String, Long> urlToId = new HashMap<>();
- synchronized (lock) {
- SchemaContext context = globalSchema.getGlobalContext();
- for (Entry<YangInstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
- String modName = findModuleName(entry.getKey(), context);
- urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
+ final Map<String, Long> urlToId = new HashMap<>();
+ 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(swaggerGenerator.generateUrlPrefixFromInstanceID(entry.getKey(), modName),
entry.getValue());
}
}
return urlToId;
}
- public void setGlobalSchema(final SchemaService globalSchema) {
- this.globalSchema = globalSchema;
- }
-
private String findModuleName(final YangInstanceIdentifier id, final SchemaContext context) {
- PathArgument rootQName = id.getPathArguments().iterator().next();
- for (Module mod : context.getModules()) {
+ final PathArgument rootQName = id.getPathArguments().iterator().next();
+ for (final Module mod : context.getModules()) {
if (mod.getDataChildByName(rootQName.getNodeType()) != null) {
return mod.getName();
}
return null;
}
- private String generateUrlPrefixFromInstanceID(final YangInstanceIdentifier key, final String moduleName) {
- StringBuilder builder = new StringBuilder();
- if (moduleName != null) {
- builder.append(moduleName);
- builder.append(':');
- }
- for (PathArgument arg : key.getPathArguments()) {
- String name = arg.getNodeType().getLocalName();
- if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
- NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
- for (Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
- builder.append(entry.getValue()).append('/');
- }
- } else {
- builder.append(name);
- builder.append('/');
- }
- }
- return builder.toString();
- }
-
private String getYangMountUrl(final YangInstanceIdentifier key) {
- String modName = findModuleName(key, globalSchema.getGlobalContext());
- return generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount/";
+ final String modName = findModuleName(key, this.globalSchema.getGlobalContext());
+ return swaggerGenerator.generateUrlPrefixFromInstanceID(key, modName) + "yang-ext:mount";
}
public ResourceList getResourceList(final UriInfo uriInfo, final Long id) {
- YangInstanceIdentifier iid = getInstanceId(id);
+ final YangInstanceIdentifier iid = getInstanceId(id);
if (iid == null) {
return null; // indicating not found.
}
- SchemaContext context = getSchemaContext(iid);
- String urlPrefix = getYangMountUrl(iid);
+ final SchemaContext context = getSchemaContext(iid);
if (context == null) {
- return createResourceList();
+ return swaggerGenerator.createResourceList();
}
- List<Resource> resources = new LinkedList<>();
- Resource dataStores = new Resource();
+ final List<Resource> resources = new LinkedList<>();
+ final Resource dataStores = new Resource();
dataStores.setDescription("Provides methods for accessing the data stores.");
- dataStores.setPath(generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
+ dataStores.setPath(swaggerGenerator.generatePath(uriInfo, DATASTORES_LABEL, DATASTORES_REVISION));
resources.add(dataStores);
- ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
+ final String urlPrefix = getYangMountUrl(iid);
+ final ResourceList list = swaggerGenerator.getResourceListing(uriInfo, context, urlPrefix);
resources.addAll(list.getApis());
list.setApis(resources);
return list;
}
private YangInstanceIdentifier getInstanceId(final Long id) {
- YangInstanceIdentifier instanceId;
- synchronized (lock) {
- instanceId = longIdToInstanceId.get(id);
+ final YangInstanceIdentifier instanceId;
+ synchronized (this.lock) {
+ instanceId = this.longIdToInstanceId.get(id);
}
return instanceId;
}
return null;
}
- Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
+ Preconditions.checkState(mountService != null);
+ final Optional<DOMMountPoint> mountPoint = this.mountService.getMountPoint(id);
if (!mountPoint.isPresent()) {
return null;
}
- SchemaContext context = mountPoint.get().getSchemaContext();
+ final SchemaContext context = mountPoint.get().getSchemaContext();
if (context == null) {
return null;
}
return context;
}
- public ApiDeclaration getMountPointApi(final UriInfo uriInfo, final Long id, final String module, final String revision) {
- YangInstanceIdentifier iid = getInstanceId(id);
- SchemaContext context = getSchemaContext(iid);
- String urlPrefix = getYangMountUrl(iid);
+ 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);
if (context == null) {
return null;
}
if (DATASTORES_LABEL.equals(module) && DATASTORES_REVISION.equals(revision)) {
return generateDataStoreApiDoc(uriInfo, urlPrefix);
}
- return super.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
+ return swaggerGenerator.getApiDeclaration(module, revision, uriInfo, context, urlPrefix);
}
private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
-
- ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
- 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));
+ 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));
+
+ final ApiDeclaration declaration = swaggerGenerator.createApiDeclaration(
+ swaggerGenerator.createBasePathFromUriInfo(uriInfo));
declaration.setApis(apis);
return declaration;
}
private Api createGetApi(final String datastore, final String note, final String context) {
- Operation getConfig = new Operation();
+ final Operation getConfig = new Operation();
getConfig.setMethod("GET");
getConfig.setNickname("GET " + datastore);
getConfig.setNotes(note);
- Api api = new Api();
- api.setPath(getDataStorePath("/" + datastore + "/", context));
+ final Api api = new Api();
+ api.setPath(swaggerGenerator.getDataStorePath(datastore, context).concat(
+ swaggerGenerator.getContent(datastore)));
api.setOperations(Collections.singletonList(getConfig));
return api;
}
- public void setMountService(final DOMMountPointService mountService) {
- this.mountService = mountService;
- }
-
@Override
public void onMountPointCreated(final YangInstanceIdentifier path) {
- synchronized (lock) {
- 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) {
- 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();
- if (swagger == null) {
- selfRef.compareAndSet(null, new MountPointSwagger());
- swagger = selfRef.get();
- }
- return swagger;
- }
-
}