}
@Override
- public void onSessionInitiated(Broker.ProviderSession providerSession) {
- SchemaService schemaService = providerSession.getService(SchemaService.class);
+ public void onSessionInitiated(final Broker.ProviderSession providerSession) {
+ final SchemaService schemaService = providerSession.getService(SchemaService.class);
ApiDocGenerator.getInstance().setSchemaService(schemaService);
- DOMMountPointService mountService = providerSession
+ final DOMMountPointService mountService = providerSession
.getService(DOMMountPointService.class);
- ListenerRegistration<MountProvisionListener> registration = mountService
+ final ListenerRegistration<MountProvisionListener> registration = mountService
.registerProvisionListener(MountPointSwagger.getInstance());
MountPointSwagger.getInstance().setGlobalSchema(schemaService);
synchronized (toClose) {
}
@Override
- public void start(BundleContext context) throws Exception {
+ public void start(final BundleContext context) throws Exception {
bundleContext = context;
brokerServiceTracker = new ServiceTracker<>(context, Broker.class, this);
brokerServiceTracker.open();
}
@Override
- public void stop(BundleContext context) throws Exception {
+ public void stop(final BundleContext context) throws Exception {
if (brokerServiceTracker != null) {
brokerServiceTracker.close();
}
}
synchronized (toClose) {
- for (AutoCloseable close : toClose) {
+ for (final AutoCloseable close : toClose) {
close.close();
}
}
}
@Override
- public Broker addingService(ServiceReference<Broker> reference) {
- Broker broker = bundleContext.getService(reference);
+ public Broker addingService(final ServiceReference<Broker> reference) {
+ final Broker broker = bundleContext.getService(reference);
session = broker.registerProvider(this, bundleContext);
return broker;
}
@Override
- public void modifiedService(ServiceReference<Broker> reference, Broker service) {
+ public void modifiedService(final ServiceReference<Broker> reference, final Broker service) {
if (session != null) {
session.close();
}
- Broker broker = bundleContext.getService(reference);
+ final Broker broker = bundleContext.getService(reference);
session = broker.registerProvider(this, bundleContext);
}
@Override
- public void removedService(ServiceReference<Broker> reference, Broker service) {
+ public void removedService(final ServiceReference<Broker> reference, final Broker service) {
bundleContext.ungetService(reference);
}
}
private static final ApiDocGenerator INSTANCE = new ApiDocGenerator();
private SchemaService schemaService;
- public ResourceList getResourceListing(UriInfo uriInfo) {
+ public ResourceList getResourceListing(final UriInfo uriInfo) {
Preconditions.checkState(schemaService != null);
- SchemaContext schemaContext = schemaService.getGlobalContext();
+ final SchemaContext schemaContext = schemaService.getGlobalContext();
Preconditions.checkState(schemaContext != null);
return super.getResourceListing(uriInfo, schemaContext, "");
}
- public ApiDeclaration getApiDeclaration(String module, String revision, UriInfo uriInfo) {
- SchemaContext schemaContext = schemaService.getGlobalContext();
+ public ApiDeclaration getApiDeclaration(final String module, final String revision, final UriInfo uriInfo) {
+ final SchemaContext schemaContext = schemaService.getGlobalContext();
Preconditions.checkState(schemaContext != null);
return super.getApiDeclaration(module, revision, uriInfo, schemaContext, "");
}
return INSTANCE;
}
- public void setSchemaService(SchemaService schemaService) {
+ public void setSchemaService(final SchemaService schemaService) {
this.schemaService = schemaService;
}
}
* served by <code> getDocByModule()</code> method.
*/
@Override
- public synchronized Response getRootDoc(UriInfo uriInfo) {
- ApiDocGenerator generator = ApiDocGenerator.getInstance();
- ResourceList rootDoc = generator.getResourceListing(uriInfo);
+ public synchronized Response getRootDoc(final UriInfo uriInfo) {
+ final ApiDocGenerator generator = ApiDocGenerator.getInstance();
+ final ResourceList rootDoc = generator.getResourceListing(uriInfo);
return Response.ok(rootDoc).build();
}
* Generates Swagger compliant document listing APIs for module.
*/
@Override
- public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
- ApiDocGenerator generator = ApiDocGenerator.getInstance();
+ public synchronized Response getDocByModule(final String module, final String revision, final UriInfo uriInfo) {
+ final ApiDocGenerator generator = ApiDocGenerator.getInstance();
- ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
+ final ApiDeclaration doc = generator.getApiDeclaration(module, revision, uriInfo);
return Response.ok(doc).build();
}
* Redirects to embedded swagger ui.
*/
@Override
- public synchronized Response getApiExplorer(UriInfo uriInfo) {
+ public synchronized Response getApiExplorer(final UriInfo uriInfo) {
return Response
.seeOther(uriInfo.getBaseUriBuilder().path("../explorer/index.html").build())
.build();
}
@Override
- public synchronized Response getListOfMounts(UriInfo uriInfo) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ public synchronized Response getListOfMounts(final UriInfo uriInfo) {
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos)) {
- JSONWriter writer = new JSONWriter(streamWriter);
+ final JSONWriter writer = new JSONWriter(streamWriter);
writer.array();
- for (Entry<String, Long> entry : MountPointSwagger.getInstance()
+ for (final Entry<String, Long> entry : MountPointSwagger.getInstance()
.getInstanceIdentifiers().entrySet()) {
writer.object();
writer.key("instance").value(entry.getKey());
writer.endObject();
}
writer.endArray();
- } catch (Exception e) {
+ } catch (final Exception e) {
return Response.status(500).entity(e.getMessage()).build();
}
return Response.status(200).entity(baos.toString()).build();
}
@Override
- public synchronized Response getMountRootDoc(String instanceNum, UriInfo uriInfo) {
- ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
+ public synchronized Response getMountRootDoc(final String instanceNum, final UriInfo uriInfo) {
+ final ResourceList resourceList = MountPointSwagger.getInstance().getResourceList(uriInfo,
Long.parseLong(instanceNum));
return Response.ok(resourceList).build();
}
@Override
- public synchronized Response getMountDocByModule(String instanceNum, String module,
- String revision, UriInfo uriInfo) {
- ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
+ public synchronized Response getMountDocByModule(final String instanceNum, final String module,
+ final String revision, final UriInfo uriInfo) {
+ final ApiDeclaration api = MountPointSwagger.getInstance().getMountPointApi(uriInfo,
Long.parseLong(instanceNum), module, revision);
return Response.ok(api).build();
}
/**
* Return list of modules converted to swagger compliant resource list.
*/
- public ResourceList getResourceListing(UriInfo uriInfo, SchemaContext schemaContext, String context) {
+ public ResourceList getResourceListing(final UriInfo uriInfo, final SchemaContext schemaContext, final String context) {
- ResourceList resourceList = createResourceList();
+ final ResourceList resourceList = createResourceList();
- Set<Module> modules = getSortedModules(schemaContext);
+ final Set<Module> modules = getSortedModules(schemaContext);
- List<Resource> resources = new ArrayList<>(modules.size());
+ final List<Resource> resources = new ArrayList<>(modules.size());
LOG.info("Modules found [{}]", modules.size());
- for (Module module : modules) {
- String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision());
- Resource resource = new Resource();
+ for (final Module module : modules) {
+ final String revisionString = SIMPLE_DATE_FORMAT.format(module.getRevision());
+ final Resource resource = new Resource();
LOG.debug("Working on [{},{}]...", module.getName(), revisionString);
- ApiDeclaration doc = getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context);
+ final ApiDeclaration doc = getApiDeclaration(module.getName(), revisionString, uriInfo, schemaContext, context);
if (doc != null) {
resource.setPath(generatePath(uriInfo, module.getName(), revisionString));
}
protected ResourceList createResourceList() {
- ResourceList resourceList = new ResourceList();
+ final ResourceList resourceList = new ResourceList();
resourceList.setApiVersion(API_VERSION);
resourceList.setSwaggerVersion(SWAGGER_VERSION);
return resourceList;
}
- protected String generatePath(UriInfo uriInfo, String name, String revision) {
- URI uri = uriInfo.getRequestUriBuilder().path(generateCacheKey(name, revision)).build();
+ protected String generatePath(final UriInfo uriInfo, final String name, final String revision) {
+ final URI uri = uriInfo.getRequestUriBuilder().path(generateCacheKey(name, revision)).build();
return uri.toASCIIString();
}
- public ApiDeclaration getApiDeclaration(String moduleName, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) {
+ public ApiDeclaration getApiDeclaration(final String moduleName, final String revision, final UriInfo uriInfo, final SchemaContext schemaContext, final String context) {
Date rev = null;
try {
if (revision != null && !revision.equals("0000-00-00")) {
rev = SIMPLE_DATE_FORMAT.parse(revision);
}
- } catch (ParseException e) {
+ } catch (final ParseException e) {
throw new IllegalArgumentException(e);
}
if (rev != null) {
- Calendar cal = new GregorianCalendar();
+ final Calendar cal = new GregorianCalendar();
cal.setTime(rev);
}
}
- Module module = schemaContext.findModuleByName(moduleName, rev);
+ final Module module = schemaContext.findModuleByName(moduleName, rev);
Preconditions.checkArgument(module != null,
"Could not find module by name,revision: " + moduleName + "," + revision);
return getApiDeclaration(module, rev, uriInfo, context, schemaContext);
}
- public ApiDeclaration getApiDeclaration(Module module, Date revision, UriInfo uriInfo, String context, SchemaContext schemaContext) {
- String basePath = createBasePathFromUriInfo(uriInfo);
+ public ApiDeclaration getApiDeclaration(final Module module, final Date revision, final UriInfo uriInfo, final String context, final SchemaContext schemaContext) {
+ final String basePath = createBasePathFromUriInfo(uriInfo);
- ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext);
+ final ApiDeclaration doc = getSwaggerDocSpec(module, basePath, context, schemaContext);
if (doc != null) {
return doc;
}
return null;
}
- protected String createBasePathFromUriInfo(UriInfo uriInfo) {
+ protected String createBasePathFromUriInfo(final UriInfo uriInfo) {
String portPart = "";
- int port = uriInfo.getBaseUri().getPort();
+ final int port = uriInfo.getBaseUri().getPort();
if (port != -1) {
portPart = ":" + port;
}
- String basePath = new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://")
+ final String basePath = new StringBuilder(uriInfo.getBaseUri().getScheme()).append("://")
.append(uriInfo.getBaseUri().getHost()).append(portPart).append("/").append(RESTCONF_CONTEXT_ROOT)
.toString();
return basePath;
}
- public ApiDeclaration getSwaggerDocSpec(Module m, String basePath, String context, SchemaContext schemaContext) {
- ApiDeclaration doc = createApiDeclaration(basePath);
+ public ApiDeclaration getSwaggerDocSpec(final Module m, final String basePath, final String context, final SchemaContext schemaContext) {
+ final ApiDeclaration doc = createApiDeclaration(basePath);
- List<Api> apis = new ArrayList<>();
+ final List<Api> apis = new ArrayList<>();
boolean hasAddRootPostLink = false;
- Collection<DataSchemaNode> dataSchemaNodes = m.getChildNodes();
+ final Collection<DataSchemaNode> dataSchemaNodes = m.getChildNodes();
LOG.debug("child nodes size [{}]", dataSchemaNodes.size());
- for (DataSchemaNode node : dataSchemaNodes) {
+ for (final DataSchemaNode node : dataSchemaNodes) {
if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
LOG.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName());
}
}
- Set<RpcDefinition> rpcs = m.getRpcs();
- for (RpcDefinition rpcDefinition : rpcs) {
- String resourcePath = getDataStorePath("/operations/", context);
+ final Set<RpcDefinition> rpcs = m.getRpcs();
+ for (final RpcDefinition rpcDefinition : rpcs) {
+ final String resourcePath = getDataStorePath("/operations/", context);
addRpcs(rpcDefinition, apis, resourcePath, schemaContext);
}
}
}
- protected ApiDeclaration createApiDeclaration(String basePath) {
- ApiDeclaration doc = new ApiDeclaration();
+ protected ApiDeclaration createApiDeclaration(final String basePath) {
+ final ApiDeclaration doc = new ApiDeclaration();
doc.setApiVersion(API_VERSION);
doc.setSwaggerVersion(SWAGGER_VERSION);
doc.setBasePath(basePath);
return doc;
}
- protected String getDataStorePath(String dataStore, String context) {
+ protected String getDataStorePath(final String dataStore, final String context) {
return dataStore + context;
}
- private String generateCacheKey(String module, String revision) {
+ private String generateCacheKey(final String module, final String revision) {
return module + "(" + revision + ")";
}
- private void addApis(DataSchemaNode node, List<Api> apis, String parentPath, List<Parameter> parentPathParams, SchemaContext schemaContext,
- boolean addConfigApi) {
+ private void addApis(final DataSchemaNode node, final List<Api> apis, final String parentPath, final List<Parameter> parentPathParams, final SchemaContext schemaContext,
+ final boolean addConfigApi) {
- Api api = new Api();
- List<Parameter> pathParams = new ArrayList<>(parentPathParams);
+ final Api api = new Api();
+ final List<Parameter> pathParams = new ArrayList<>(parentPathParams);
- String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/";
+ final String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/";
LOG.debug("Adding path: [{}]", resourcePath);
api.setPath(resourcePath);
Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode>emptySet();
if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
- DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
+ final DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
childSchemaNodes = dataNodeContainer.getChildNodes();
}
api.setOperations(operation(node, pathParams, addConfigApi, childSchemaNodes));
apis.add(api);
- for (DataSchemaNode childNode : childSchemaNodes) {
+ for (final DataSchemaNode childNode : childSchemaNodes) {
if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
// keep config and operation attributes separate.
if (childNode.isConfiguration() == addConfigApi) {
}
private boolean containsListOrContainer(final Iterable<DataSchemaNode> nodes) {
- for (DataSchemaNode child : nodes) {
+ for (final DataSchemaNode child : nodes) {
if (child instanceof ListSchemaNode || child instanceof ContainerSchemaNode) {
return true;
}
return false;
}
- private List<Operation> operation(DataSchemaNode node, List<Parameter> pathParams, boolean isConfig, Iterable<DataSchemaNode> childSchemaNodes) {
- List<Operation> operations = new ArrayList<>();
+ private List<Operation> operation(final DataSchemaNode node, final List<Parameter> pathParams, final boolean isConfig, final Iterable<DataSchemaNode> childSchemaNodes) {
+ final List<Operation> operations = new ArrayList<>();
- Get getBuilder = new Get(node, isConfig);
+ final Get getBuilder = new Get(node, isConfig);
operations.add(getBuilder.pathParams(pathParams).build());
if (isConfig) {
- Put putBuilder = new Put(node.getQName().getLocalName(),
+ final Put putBuilder = new Put(node.getQName().getLocalName(),
node.getDescription());
operations.add(putBuilder.pathParams(pathParams).build());
- Delete deleteBuilder = new Delete(node);
+ final Delete deleteBuilder = new Delete(node);
operations.add(deleteBuilder.pathParams(pathParams).build());
if (containsListOrContainer(childSchemaNodes)) {
return operations;
}
- private List<Operation> operationPost(final String name, final String description, final DataNodeContainer dataNodeContainer, List<Parameter> pathParams, boolean isConfig) {
- List<Operation> operations = new ArrayList<>();
+ private List<Operation> operationPost(final String name, final String description, final DataNodeContainer dataNodeContainer, final List<Parameter> pathParams, final boolean isConfig) {
+ final List<Operation> operations = new ArrayList<>();
if (isConfig) {
- Post postBuilder = new Post(name, description, dataNodeContainer);
+ final Post postBuilder = new Post(name, description, dataNodeContainer);
operations.add(postBuilder.pathParams(pathParams).build());
}
return operations;
}
- private String createPath(final DataSchemaNode schemaNode, List<Parameter> pathParams, SchemaContext schemaContext) {
- ArrayList<LeafSchemaNode> pathListParams = new ArrayList<>();
- StringBuilder path = new StringBuilder();
- String localName = resolvePathArgumentsName(schemaNode, schemaContext);
+ private String createPath(final DataSchemaNode schemaNode, final List<Parameter> pathParams, final SchemaContext schemaContext) {
+ final ArrayList<LeafSchemaNode> pathListParams = new ArrayList<>();
+ final StringBuilder path = new StringBuilder();
+ final String localName = resolvePathArgumentsName(schemaNode, schemaContext);
path.append(localName);
if ((schemaNode instanceof ListSchemaNode)) {
final List<QName> listKeys = ((ListSchemaNode) schemaNode).getKeyDefinition();
for (final QName listKey : listKeys) {
- DataSchemaNode dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey);
+ final DataSchemaNode dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey);
pathListParams.add(((LeafSchemaNode) dataChildByName));
- String pathParamIdentifier = new StringBuilder("/{").append(listKey.getLocalName()).append("}")
+ final String pathParamIdentifier = new StringBuilder("/{").append(listKey.getLocalName()).append("}")
.toString();
path.append(pathParamIdentifier);
- Parameter pathParam = new Parameter();
+ final Parameter pathParam = new Parameter();
pathParam.setName(listKey.getLocalName());
pathParam.setDescription(dataChildByName.getDescription());
pathParam.setType("string");
return path.toString();
}
- protected void addRpcs(RpcDefinition rpcDefn, List<Api> apis, String parentPath, SchemaContext schemaContext) {
- Api rpc = new Api();
- String resourcePath = parentPath + resolvePathArgumentsName(rpcDefn, schemaContext);
+ protected void addRpcs(final RpcDefinition rpcDefn, final List<Api> apis, final String parentPath, final SchemaContext schemaContext) {
+ final Api rpc = new Api();
+ final String resourcePath = parentPath + resolvePathArgumentsName(rpcDefn, schemaContext);
rpc.setPath(resourcePath);
- Operation operationSpec = new Operation();
+ final Operation operationSpec = new Operation();
operationSpec.setMethod("POST");
operationSpec.setNotes(rpcDefn.getDescription());
operationSpec.setNickname(rpcDefn.getQName().getLocalName());
operationSpec.setType("(" + rpcDefn.getQName().getLocalName() + ")output");
}
if (rpcDefn.getInput() != null) {
- Parameter payload = new Parameter();
+ final Parameter payload = new Parameter();
payload.setParamType("body");
payload.setType("(" + rpcDefn.getQName().getLocalName() + ")input");
operationSpec.setParameters(Collections.singletonList(payload));
apis.add(rpc);
}
- protected SortedSet<Module> getSortedModules(SchemaContext schemaContext) {
+ protected SortedSet<Module> getSortedModules(final SchemaContext schemaContext) {
if (schemaContext == null) {
return new TreeSet<>();
}
- Set<Module> modules = schemaContext.getModules();
+ final Set<Module> modules = schemaContext.getModules();
- SortedSet<Module> sortedModules = new TreeSet<>((module1, module2) -> {
+ final SortedSet<Module> sortedModules = new TreeSet<>((module1, module2) -> {
int result = module1.getName().compareTo(module2.getName());
if (result == 0) {
- Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0);
- Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0);
+ final Date module1Revision = module1.getRevision() != null ? module1.getRevision() : new Date(0);
+ final Date module2Revision = module2.getRevision() != null ? module2.getRevision() : new Date(0);
result = module1Revision.compareTo(module2Revision);
}
if (result == 0) {
}
return result;
});
- for (Module m : modules) {
+ for (final Module m : modules) {
if (m != null) {
sortedModules.add(m);
}
}
public JSONObject convertToJsonSchema(final Module module, final SchemaContext schemaContext) throws IOException, JSONException {
- JSONObject models = new JSONObject();
+ final JSONObject models = new JSONObject();
topLevelModule = module;
processModules(module, models);
processContainersAndLists(module, models, schemaContext);
private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
throws IOException, JSONException {
- String moduleName = module.getName();
+ final String moduleName = module.getName();
- for (DataSchemaNode childNode : module.getChildNodes()) {
+ for (final DataSchemaNode childNode : module.getChildNodes()) {
// For every container and list in the module
if (childNode instanceof ContainerSchemaNode || childNode instanceof ListSchemaNode) {
processDataNodeContainer((DataNodeContainer) childNode, moduleName, models, true, schemaContext);
private void processRPCs(final Module module, final JSONObject models, final SchemaContext schemaContext) throws JSONException,
IOException {
- Set<RpcDefinition> rpcs = module.getRpcs();
- String moduleName = module.getName();
- for (RpcDefinition rpc : rpcs) {
+ final Set<RpcDefinition> rpcs = module.getRpcs();
+ final String moduleName = module.getName();
+ for (final RpcDefinition rpc : rpcs) {
- ContainerSchemaNode input = rpc.getInput();
+ final ContainerSchemaNode input = rpc.getInput();
if (input != null) {
- JSONObject inputJSON = processDataNodeContainer(input, moduleName, models, schemaContext);
- String filename = "(" + rpc.getQName().getLocalName() + ")input";
+ final JSONObject inputJSON = processDataNodeContainer(input, moduleName, models, schemaContext);
+ final String filename = "(" + rpc.getQName().getLocalName() + ")input";
inputJSON.put("id", filename);
// writeToFile(filename, inputJSON.toString(2), moduleName);
models.put(filename, inputJSON);
}
- ContainerSchemaNode output = rpc.getOutput();
+ final ContainerSchemaNode output = rpc.getOutput();
if (output != null) {
- JSONObject outputJSON = processDataNodeContainer(output, moduleName, models, schemaContext);
- String filename = "(" + rpc.getQName().getLocalName() + ")output";
+ final JSONObject outputJSON = processDataNodeContainer(output, moduleName, models, schemaContext);
+ final String filename = "(" + rpc.getQName().getLocalName() + ")output";
outputJSON.put("id", filename);
models.put(filename, outputJSON);
}
*/
private static void processIdentities(final Module module, final JSONObject models) throws JSONException {
- String moduleName = module.getName();
- Set<IdentitySchemaNode> idNodes = module.getIdentities();
+ final String moduleName = module.getName();
+ final Set<IdentitySchemaNode> idNodes = module.getIdentities();
LOG.debug("Processing Identities for module {} . Found {} identity statements", moduleName, idNodes.size());
- for (IdentitySchemaNode idNode : idNodes) {
- JSONObject identityObj = new JSONObject();
- String identityName = idNode.getQName().getLocalName();
+ for (final IdentitySchemaNode idNode : idNodes) {
+ final JSONObject identityObj = new JSONObject();
+ final String identityName = idNode.getQName().getLocalName();
LOG.debug("Processing Identity: {}", identityName);
identityObj.put(ID_KEY, identityName);
identityObj.put(DESCRIPTION_KEY, idNode.getDescription());
- JSONObject props = new JSONObject();
- IdentitySchemaNode baseId = idNode.getBaseIdentity();
+ final JSONObject props = new JSONObject();
+ final IdentitySchemaNode baseId = idNode.getBaseIdentity();
if (baseId == null) {
/**
* This is a base identity. So lets see if it has sub types. If it does, then add them to the model
* definition.
*/
- Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
+ final Set<IdentitySchemaNode> derivedIds = idNode.getDerivedIdentities();
if (derivedIds != null) {
- JSONArray subTypes = new JSONArray();
- for (IdentitySchemaNode derivedId : derivedIds) {
+ final JSONArray subTypes = new JSONArray();
+ for (final IdentitySchemaNode derivedId : derivedIds) {
subTypes.put(derivedId.getQName().getLocalName());
}
identityObj.put(SUB_TYPES_KEY, subTypes);
final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
if (dataNode instanceof ListSchemaNode || dataNode instanceof ContainerSchemaNode) {
Preconditions.checkArgument(dataNode instanceof SchemaNode, "Data node should be also schema node");
- Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
- JSONObject properties = processChildren(containerChildren, ((SchemaNode) dataNode).getQName(), moduleName,
+ final Iterable<DataSchemaNode> containerChildren = dataNode.getChildNodes();
+ final JSONObject properties = processChildren(containerChildren, ((SchemaNode) dataNode).getQName(), moduleName,
models, isConfig, schemaContext);
- String nodeName = (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
+ final String nodeName = (isConfig ? OperationBuilder.CONFIG : OperationBuilder.OPERATIONAL)
+ ((SchemaNode) dataNode).getQName().getLocalName();
- JSONObject childSchema = getSchemaTemplate();
+ final JSONObject childSchema = getSchemaTemplate();
childSchema.put(TYPE_KEY, OBJECT_TYPE);
childSchema.put(PROPERTIES_KEY, properties);
childSchema.put("id", nodeName);
createPropertiesForPost(dataNode));
}
- JSONObject items = new JSONObject();
+ final JSONObject items = new JSONObject();
items.put(REF_KEY, nodeName);
- JSONObject dataNodeProperties = new JSONObject();
+ final JSONObject dataNodeProperties = new JSONObject();
dataNodeProperties.put(TYPE_KEY, dataNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
dataNodeProperties.put(ITEMS_KEY, items);
private static void createConcreteModelForPost(final JSONObject models, final String localName,
final JSONObject properties) throws JSONException {
- String nodePostName = OperationBuilder.CONFIG + localName + Post.METHOD_NAME;
- JSONObject postSchema = getSchemaTemplate();
+ final String nodePostName = OperationBuilder.CONFIG + localName + Post.METHOD_NAME;
+ final JSONObject postSchema = getSchemaTemplate();
postSchema.put(TYPE_KEY, OBJECT_TYPE);
postSchema.put("id", nodePostName);
postSchema.put(PROPERTIES_KEY, properties);
}
private JSONObject createPropertiesForPost(final DataNodeContainer dataNodeContainer) throws JSONException {
- JSONObject properties = new JSONObject();
- for (DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
+ final JSONObject properties = new JSONObject();
+ for (final DataSchemaNode childNode : dataNodeContainer.getChildNodes()) {
if (childNode instanceof ListSchemaNode || childNode instanceof ContainerSchemaNode) {
- JSONObject items = new JSONObject();
+ final JSONObject items = new JSONObject();
items.put(REF_KEY, "(config)" + childNode.getQName().getLocalName());
- JSONObject property = new JSONObject();
+ final JSONObject property = new JSONObject();
property.put(TYPE_KEY, childNode instanceof ListSchemaNode ? ARRAY_TYPE : OBJECT_TYPE);
property.put(ITEMS_KEY, items);
properties.put(childNode.getQName().getLocalName(), property);
} else if (childNode instanceof LeafSchemaNode) {
- JSONObject property = processLeafNode((LeafSchemaNode)childNode);
+ final JSONObject property = processLeafNode((LeafSchemaNode) childNode);
properties.put(childNode.getQName().getLocalName(), property);
}
}
final String moduleName, final JSONObject models, final boolean isConfig, final SchemaContext schemaContext)
throws JSONException, IOException {
- JSONObject properties = new JSONObject();
+ final JSONObject properties = new JSONObject();
- for (DataSchemaNode node : nodes) {
+ for (final DataSchemaNode node : nodes) {
if (node.isConfiguration() == isConfig) {
- String name = resolveNodesName(node, topLevelModule, schemaContext);
+ final String name = resolveNodesName(node, topLevelModule, schemaContext);
JSONObject property = null;
if (node instanceof LeafSchemaNode) {
property = processLeafNode((LeafSchemaNode) node);
}
private JSONObject processLeafListNode(final LeafListSchemaNode listNode) throws JSONException {
- JSONObject props = new JSONObject();
+ final JSONObject props = new JSONObject();
props.put(TYPE_KEY, ARRAY_TYPE);
- JSONObject itemsVal = new JSONObject();
+ final JSONObject itemsVal = new JSONObject();
processTypeDef(listNode.getType(), itemsVal);
props.put(ITEMS_KEY, itemsVal);
- ConstraintDefinition constraints = listNode.getConstraints();
+ final ConstraintDefinition constraints = listNode.getConstraints();
processConstraints(constraints, props);
return props;
private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models,
final SchemaContext schemaContext) throws JSONException, IOException {
- Set<ChoiceCaseNode> cases = choiceNode.getCases();
+ final Set<ChoiceCaseNode> cases = choiceNode.getCases();
- JSONArray choiceProps = new JSONArray();
- for (ChoiceCaseNode choiceCase : cases) {
- String choiceName = choiceCase.getQName().getLocalName();
- JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), choiceCase.getQName(), moduleName,
+ final JSONArray choiceProps = new JSONArray();
+ for (final ChoiceCaseNode choiceCase : cases) {
+ final String choiceName = choiceCase.getQName().getLocalName();
+ final JSONObject choiceProp = processChildren(choiceCase.getChildNodes(), choiceCase.getQName(), moduleName,
models, schemaContext);
- JSONObject choiceObj = new JSONObject();
+ final JSONObject choiceObj = new JSONObject();
choiceObj.put(choiceName, choiceProp);
choiceObj.put(TYPE_KEY, OBJECT_TYPE);
choiceProps.put(choiceObj);
}
- JSONObject oneOfProps = new JSONObject();
+ final JSONObject oneOfProps = new JSONObject();
oneOfProps.put(ONE_OF_KEY, choiceProps);
oneOfProps.put(TYPE_KEY, OBJECT_TYPE);
}
private static void processConstraints(final ConstraintDefinition constraints, final JSONObject props) throws JSONException {
- boolean isMandatory = constraints.isMandatory();
+ final boolean isMandatory = constraints.isMandatory();
props.put(REQUIRED_KEY, isMandatory);
- Integer minElements = constraints.getMinElements();
- Integer maxElements = constraints.getMaxElements();
+ final Integer minElements = constraints.getMinElements();
+ final Integer maxElements = constraints.getMaxElements();
if (minElements != null) {
props.put(MIN_ITEMS, minElements);
}
}
private JSONObject processLeafNode(final LeafSchemaNode leafNode) throws JSONException {
- JSONObject property = new JSONObject();
+ final JSONObject property = new JSONObject();
- String leafDescription = leafNode.getDescription();
+ final String leafDescription = leafNode.getDescription();
property.put(DESCRIPTION_KEY, leafDescription);
processConstraints(leafNode.getConstraints(), property);
}
private static JSONObject processAnyXMLNode(final AnyXmlSchemaNode leafNode) throws JSONException {
- JSONObject property = new JSONObject();
+ final JSONObject property = new JSONObject();
- String leafDescription = leafNode.getDescription();
+ final String leafDescription = leafNode.getDescription();
property.put(DESCRIPTION_KEY, leafDescription);
processConstraints(leafNode.getConstraints(), property);
private static void processBinaryType(final BinaryTypeDefinition binaryType, final JSONObject property) throws JSONException {
property.put(TYPE_KEY, STRING);
- JSONObject media = new JSONObject();
+ final JSONObject media = new JSONObject();
media.put(BINARY_ENCODING_KEY, BASE_64);
property.put(MEDIA_KEY, media);
}
private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException {
- List<EnumPair> enumPairs = enumLeafType.getValues();
- List<String> enumNames = new ArrayList<>();
- for (EnumPair enumPair : enumPairs) {
+ final List<EnumPair> enumPairs = enumLeafType.getValues();
+ final List<String> enumNames = new ArrayList<>();
+ for (final EnumPair enumPair : enumPairs) {
enumNames.add(enumPair.getName());
}
property.putOpt(ENUM, new JSONArray(enumNames));
property.put(TYPE_KEY, ARRAY_TYPE);
property.put(MIN_ITEMS, 0);
property.put(UNIQUE_ITEMS_KEY, true);
- JSONArray enumValues = new JSONArray();
+ final JSONArray enumValues = new JSONArray();
- List<Bit> bits = bitsType.getBits();
- for (Bit bit : bits) {
+ final List<Bit> bits = bitsType.getBits();
+ for (final Bit bit : bits) {
enumValues.put(bit.getName());
}
- JSONObject itemsValue = new JSONObject();
+ final JSONObject itemsValue = new JSONObject();
itemsValue.put(ENUM, enumValues);
property.put(ITEMS_KEY, itemsValue);
}
// FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
// and true maximum implied by the constraints and use that.
- for (LengthConstraint lengthConstraint : lengthConstraints) {
- Number min = lengthConstraint.getMin();
- Number max = lengthConstraint.getMax();
+ for (final LengthConstraint lengthConstraint : lengthConstraints) {
+ final Number min = lengthConstraint.getMin();
+ final Number max = lengthConstraint.getMax();
property.putOpt(MIN_LENGTH_KEY, min);
property.putOpt(MAX_LENGTH_KEY, max);
}
}
private static void processUnionType(final UnionTypeDefinition unionType, final JSONObject property) throws JSONException {
- StringBuilder type = new StringBuilder();
- for (TypeDefinition<?> typeDef : unionType.getTypes()) {
+ final StringBuilder type = new StringBuilder();
+ for (final TypeDefinition<?> typeDef : unionType.getTypes()) {
if (type.length() > 0) {
type.append(" or ");
}
* Helper method to generate a pre-filled JSON schema object.
*/
private static JSONObject getSchemaTemplate() throws JSONException {
- JSONObject schemaJSON = new JSONObject();
+ final JSONObject schemaJSON = new JSONObject();
schemaJSON.put(SCHEMA_KEY, SCHEMA_URL);
return schemaJSON;
}
@Override
- public ObjectMapper getContext(Class<?> klass) {
+ public ObjectMapper getContext(final Class<?> klass) {
if (ApiDeclaration.class.isAssignableFrom(klass)) {
return ctx;
}
protected DataSchemaNode schemaNode;
private static final String METHOD_NAME = "GET";
- public Get(DataSchemaNode node, boolean isConfig) {
+ public Get(final DataSchemaNode node, final boolean isConfig) {
this.schemaNode = node;
spec = new Operation();
spec.setMethod(METHOD_NAME);
spec.setNotes(node.getDescription());
}
- public Get pathParams(List<Parameter> params) {
- List<Parameter> pathParameters = new ArrayList<>(params);
+ public Get pathParams(final List<Parameter> params) {
+ final List<Parameter> pathParameters = new ArrayList<>(params);
spec.setParameters(pathParameters);
return this;
}
protected String nodeName;
private static final String METHOD_NAME = "PUT";
- public Put(String nodeName, final String description) {
+ public Put(final String nodeName, final String description) {
this.nodeName = nodeName;
spec = new Operation();
spec.setType(CONFIG + nodeName);
spec.setConsumes(CONSUMES_PUT_POST);
}
- public Put pathParams(List<Parameter> params) {
- List<Parameter> parameters = new ArrayList<>(params);
- Parameter payload = new Parameter();
+ public Put pathParams(final List<Parameter> params) {
+ final List<Parameter> parameters = new ArrayList<>(params);
+ final Parameter payload = new Parameter();
payload.setParamType("body");
payload.setType(CONFIG + nodeName);
parameters.add(payload);
}
@Override
- public Put pathParams(List<Parameter> params) {
- List<Parameter> parameters = new ArrayList<>(params);
- for (DataSchemaNode node : dataNodeContainer.getChildNodes()) {
+ public Put pathParams(final List<Parameter> params) {
+ final List<Parameter> parameters = new ArrayList<>(params);
+ for (final DataSchemaNode node : dataNodeContainer.getChildNodes()) {
if (node instanceof ListSchemaNode || node instanceof ContainerSchemaNode) {
- Parameter payload = new Parameter();
+ final Parameter payload = new Parameter();
payload.setParamType("body");
payload.setType(CONFIG + node.getQName().getLocalName());
payload.setName("**" + CONFIG + node.getQName().getLocalName());
public static final class Delete extends Get {
private static final String METHOD_NAME = "DELETE";
- public Delete(DataSchemaNode node) {
+ public Delete(final DataSchemaNode node) {
super(node, false);
}
private final AtomicLong idKey = new AtomicLong(0);
- private static AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
+ private static final AtomicReference<MountPointSwagger> selfRef = new AtomicReference<>();
private SchemaService globalSchema;
public Map<String, Long> getInstanceIdentifiers() {
- Map<String, Long> urlToId = new HashMap<>();
+ final 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);
+ final SchemaContext context = globalSchema.getGlobalContext();
+ for (final Entry<YangInstanceIdentifier, Long> entry : instanceIdToLongId.entrySet()) {
+ final String modName = findModuleName(entry.getKey(), context);
urlToId.put(generateUrlPrefixFromInstanceID(entry.getKey(), modName),
entry.getValue());
}
}
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();
}
}
private String generateUrlPrefixFromInstanceID(final YangInstanceIdentifier key, final String moduleName) {
- StringBuilder builder = new StringBuilder();
+ final StringBuilder builder = new StringBuilder();
if (moduleName != null) {
builder.append(moduleName);
builder.append(':');
}
- for (PathArgument arg : key.getPathArguments()) {
- String name = arg.getNodeType().getLocalName();
+ for (final PathArgument arg : key.getPathArguments()) {
+ final String name = arg.getNodeType().getLocalName();
if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
- NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
- for (Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
+ final NodeIdentifierWithPredicates nodeId = (NodeIdentifierWithPredicates) arg;
+ for (final Entry<QName, Object> entry : nodeId.getKeyValues().entrySet()) {
builder.append(entry.getValue()).append('/');
}
} else {
}
private String getYangMountUrl(final YangInstanceIdentifier key) {
- String modName = findModuleName(key, globalSchema.getGlobalContext());
+ final String modName = findModuleName(key, globalSchema.getGlobalContext());
return 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);
+ final SchemaContext context = getSchemaContext(iid);
if (context == null) {
return 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));
resources.add(dataStores);
- String urlPrefix = getYangMountUrl(iid);
- ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
+ final String urlPrefix = getYangMountUrl(iid);
+ final ResourceList list = super.getResourceListing(uriInfo, context, urlPrefix);
resources.addAll(list.getApis());
list.setApis(resources);
return list;
}
private YangInstanceIdentifier getInstanceId(final Long id) {
- YangInstanceIdentifier instanceId;
+ final YangInstanceIdentifier instanceId;
synchronized (lock) {
instanceId = longIdToInstanceId.get(id);
}
return null;
}
- Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
+ final Optional<DOMMountPoint> mountPoint = mountService.getMountPoint(id);
if (!mountPoint.isPresent()) {
return null;
}
- SchemaContext context = mountPoint.get().getSchemaContext();
+ final SchemaContext context = mountPoint.get().getSchemaContext();
if (context == null) {
return null;
}
}
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);
+ final YangInstanceIdentifier iid = getInstanceId(id);
+ final SchemaContext context = getSchemaContext(iid);
+ final String urlPrefix = getYangMountUrl(iid);
if (context == null) {
return null;
}
}
private ApiDeclaration generateDataStoreApiDoc(final UriInfo uriInfo, final String context) {
- List<Api> apis = new LinkedList<>();
+ final List<Api> apis = new LinkedList<>();
apis.add(createGetApi("config",
"Queries the config (startup) datastore on the mounted hosted.", context));
apis.add(createGetApi("operational",
apis.add(createGetApi("operations",
"Queries the available operations (RPC calls) on the mounted hosted.", context));
- ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
+ final ApiDeclaration declaration = super.createApiDeclaration(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();
+ final Api api = new Api();
api.setPath(getDataStorePath("/" + datastore + "/", context));
api.setOperations(Collections.singletonList(getConfig));
@Override
public void onMountPointCreated(final YangInstanceIdentifier path) {
synchronized (lock) {
- Long idLong = idKey.incrementAndGet();
+ final Long idLong = idKey.incrementAndGet();
instanceIdToLongId.put(path, idLong);
longIdToInstanceId.put(idLong, path);
}
@Override
public void onMountPointRemoved(final YangInstanceIdentifier path) {
synchronized (lock) {
- Long id = instanceIdToLongId.remove(path);
+ final Long id = instanceIdToLongId.remove(path);
longIdToInstanceId.remove(id);
}
}
private RestDocgenUtil() {
}
- private static Map<URI, Map<Date, Module>> namespaceAndRevisionToModule = new HashMap<>();
+ private static final Map<URI, Map<Date, Module>> namespaceAndRevisionToModule = new HashMap<>();
/**
* Resolve path argument name for {@code node}.
* @return name of {@code node}
*/
public static String resolvePathArgumentsName(final SchemaNode node, final SchemaContext schemaContext) {
- Iterable<QName> schemaPath = node.getPath().getPathTowardsRoot();
- Iterator<QName> it = schemaPath.iterator();
- QName nodeQName = it.next();
+ final Iterable<QName> schemaPath = node.getPath().getPathTowardsRoot();
+ final Iterator<QName> it = schemaPath.iterator();
+ final QName nodeQName = it.next();
QName parentQName = null;
if (it.hasNext()) {
}
}
- private static boolean isEqualNamespaceAndRevision(QName parentQName, QName nodeQName) {
+ private static boolean isEqualNamespaceAndRevision(final QName parentQName, final QName nodeQName) {
if (parentQName == null) {
if (nodeQName == null) {
return true;
return parentQName.getNamespace().equals(nodeQName.getNamespace())
&& parentQName.getRevision().equals(nodeQName.getRevision());
}
+
}