* Generates index document for Swagger UI. This document lists out all
* modules with link to get APIs for each module. The API for each module is
* served by <code> getDocByModule()</code> method.
- *
- * @param uriInfo
- * @return
*/
@GET
@Produces(MediaType.APPLICATION_JSON)
/**
* Generates Swagger compliant document listing APIs for module.
- *
- * @param module
- * @param revision
- * @param uriInfo
- * @return
*/
@GET
@Path("/{module}({revision})")
/**
* Redirects to embedded swagger ui.
- *
- * @param uriInfo
- * @return
*/
@GET
@Path("/ui")
* Generates index document for Swagger UI. This document lists out all
* modules with link to get APIs for each module. The API for each module is
* served by <code> getDocByModule()</code> method.
- *
- * @param uriInfo
- * @return
*/
@GET
@Path("/mounts")
/**
* Generates Swagger compliant document listing APIs for module.
- *
- * @param module
- * @param revision
- * @param uriInfo
- * @return
*/
@GET
@Path("/mounts/{instance}/{module}({revision})")
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
/**
- * This class gathers all YANG-defined {@link org.opendaylight.yangtools.yang.model.api.Module}s and generates Swagger compliant documentation.
+ * This class gathers all YANG-defined {@link org.opendaylight.yangtools.yang.model.api.Module}s and
+ * generates Swagger compliant documentation.
*/
public class ApiDocGenerator extends BaseYangSwaggerGenerator {
}
/**
- * Returns singleton instance
- *
- * @return
+ * Returns singleton instance.
*/
public static ApiDocGenerator getInstance() {
return INSTANCE;
}
- /**
- *
- * @param schemaService
- */
public void setSchemaService(SchemaService schemaService) {
this.schemaService = schemaService;
}
* >https://helloreverb.com/developers/swagger</a>) compliant documentation for
* RESTCONF APIs. The output of this is used by embedded Swagger UI.
*
- * NOTE: These API's need to be synchronized due to bug 1198. Thread access to
+ * <p>NOTE: These API's need to be synchronized due to bug 1198. Thread access to
* the SchemaContext is not synchronized properly and thus you can end up with
* missing definitions without this synchronization. There are likely otherways
* to work around this limitation, but given that this API is a dev only tool
* and not dependent UI, this was the fastest work around.
- *
*/
public class ApiDocServiceImpl implements ApiDocService {
* Generates index document for Swagger UI. This document lists out all
* modules with link to get APIs for each module. The API for each module is
* served by <code> getDocByModule()</code> method.
- *
- * @param uriInfo
- * @return
*/
@Override
public synchronized Response getRootDoc(UriInfo uriInfo) {
/**
* Generates Swagger compliant document listing APIs for module.
- *
- * @param module
- * @param revision
- * @param uriInfo
- * @return
*/
@Override
public synchronized Response getDocByModule(String module, String revision, UriInfo uriInfo) {
/**
* Redirects to embedded swagger ui.
- *
- * @param uriInfo
- * @return
*/
@Override
public synchronized Response getApiExplorer(UriInfo uriInfo) {
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
-import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
protected static final String RESTCONF_CONTEXT_ROOT = "restconf";
static final String MODULE_NAME_SUFFIX = "_module";
- protected final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
+ protected static final DateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private final ModelGenerator jsonConverter = new ModelGenerator();
// private Map<String, ApiDeclaration> MODULE_DOC_CACHE = new HashMap<>()
}
/**
- *
- * @param uriInfo
- * @param schemaContext
- * @param context
- * @return list of modules converted to swagger compliant resource list.
+ * Return list of modules converted to swagger compliant resource list.
*/
public ResourceList getResourceListing(UriInfo uriInfo, SchemaContext schemaContext, String context) {
return uri.toASCIIString();
}
- public ApiDeclaration getApiDeclaration(String module, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) {
+ public ApiDeclaration getApiDeclaration(String moduleName, String revision, UriInfo uriInfo, SchemaContext schemaContext, String context) {
Date rev = null;
try {
- if(revision != null && !revision.equals("0000-00-00")) {
+ if (revision != null && !revision.equals("0000-00-00")) {
rev = SIMPLE_DATE_FORMAT.parse(revision);
}
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
- if(rev != null) {
+ if (rev != null) {
Calendar cal = new GregorianCalendar();
cal.setTime(rev);
- if(cal.get(Calendar.YEAR) < 1970) {
+ if (cal.get(Calendar.YEAR) < 1970) {
rev = null;
}
}
- Module m = schemaContext.findModuleByName(module, rev);
- Preconditions.checkArgument(m != null, "Could not find module by name,revision: " + module + "," + revision);
+ Module module = schemaContext.findModuleByName(moduleName, rev);
+ Preconditions.checkArgument(module != null,
+ "Could not find module by name,revision: " + moduleName + "," + revision);
- return getApiDeclaration(m, rev, uriInfo, context, schemaContext);
+ return getApiDeclaration(module, rev, uriInfo, context, schemaContext);
}
public ApiDeclaration getApiDeclaration(Module module, Date revision, UriInfo uriInfo, String context, SchemaContext schemaContext) {
public ApiDeclaration getSwaggerDocSpec(Module m, String basePath, String context, SchemaContext schemaContext) {
ApiDeclaration doc = createApiDeclaration(basePath);
- List<Api> apis = new ArrayList<Api>();
+ List<Api> apis = new ArrayList<>();
Collection<DataSchemaNode> dataSchemaNodes = m.getChildNodes();
LOG.debug("child nodes size [{}]", dataSchemaNodes.size());
LOG.debug("Is Configuration node [{}] [{}]", node.isConfiguration(), node.getQName().getLocalName());
- List<Parameter> pathParams = new ArrayList<Parameter>();
+ List<Parameter> pathParams = new ArrayList<>();
String resourcePath = getDataStorePath("/config/", context);
addRootPostLink(m, (DataNodeContainer) node, pathParams, resourcePath, apis);
addApis(node, apis, resourcePath, pathParams, schemaContext, true);
- pathParams = new ArrayList<Parameter>();
+ pathParams = new ArrayList<>();
resourcePath = getDataStorePath("/operational/", context);
addApis(node, apis, resourcePath, pathParams, schemaContext, false);
}
return null;
}
- private void addRootPostLink(final Module m, final DataNodeContainer node, final List<Parameter> pathParams,
+ private void addRootPostLink(final Module module, final DataNodeContainer node, final List<Parameter> pathParams,
final String resourcePath, final List<Api> apis) {
- if (containsListOrContainer(m.getChildNodes())) {
+ if (containsListOrContainer(module.getChildNodes())) {
final Api apiForRootPostUri = new Api();
apiForRootPostUri.setPath(resourcePath);
- apiForRootPostUri.setOperations(operationPost(m.getName()+MODULE_NAME_SUFFIX, m.getDescription(), m, pathParams, true));
+ apiForRootPostUri.setOperations(operationPost(module.getName() + MODULE_NAME_SUFFIX,
+ module.getDescription(), module, pathParams, true));
apis.add(apiForRootPostUri);
}
}
return dataStore + context;
}
- private String generateCacheKey(Module m) {
- return generateCacheKey(m.getName(), SIMPLE_DATE_FORMAT.format(m.getRevision()));
- }
-
private String generateCacheKey(String module, String revision) {
return module + "(" + revision + ")";
}
boolean addConfigApi) {
Api api = new Api();
- List<Parameter> pathParams = new ArrayList<Parameter>(parentPathParams);
+ List<Parameter> pathParams = new ArrayList<>(parentPathParams);
String resourcePath = parentPath + createPath(node, pathParams, schemaContext) + "/";
LOG.debug("Adding path: [{}]", resourcePath);
api.setPath(resourcePath);
- Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode> emptySet();
+ Iterable<DataSchemaNode> childSchemaNodes = Collections.<DataSchemaNode>emptySet();
if ((node instanceof ListSchemaNode) || (node instanceof ContainerSchemaNode)) {
DataNodeContainer dataNodeContainer = (DataNodeContainer) node;
childSchemaNodes = dataNodeContainer.getChildNodes();
return false;
}
- /**
- * @param node
- * @param pathParams
- * @return
- */
private List<Operation> operation(DataSchemaNode node, List<Parameter> pathParams, boolean isConfig, Iterable<DataSchemaNode> childSchemaNodes) {
List<Operation> operations = new ArrayList<>();
operations.add(deleteBuilder.pathParams(pathParams).build());
if (containsListOrContainer(childSchemaNodes)) {
- operations.addAll(operationPost(node.getQName().getLocalName(), node.getDescription(), (DataNodeContainer) node,
- pathParams, isConfig));
+ operations.addAll(operationPost(node.getQName().getLocalName(), node.getDescription(),
+ (DataNodeContainer) node, pathParams, isConfig));
}
}
return operations;
}
- /**
- * @param node
- * @param pathParams
- * @return
- */
private List<Operation> operationPost(final String name, final String description, final DataNodeContainer dataNodeContainer, List<Parameter> pathParams, boolean isConfig) {
List<Operation> operations = new ArrayList<>();
if (isConfig) {
}
private String createPath(final DataSchemaNode schemaNode, List<Parameter> pathParams, SchemaContext schemaContext) {
- ArrayList<LeafSchemaNode> pathListParams = new ArrayList<LeafSchemaNode>();
+ ArrayList<LeafSchemaNode> pathListParams = new ArrayList<>();
StringBuilder path = new StringBuilder();
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);
- pathListParams.add(((LeafSchemaNode) _dataChildByName));
+ DataSchemaNode dataChildByName = ((DataNodeContainer) schemaNode).getDataChildByName(listKey);
+ pathListParams.add(((LeafSchemaNode) dataChildByName));
String pathParamIdentifier = new StringBuilder("/{").append(listKey.getLocalName()).append("}")
.toString();
Parameter pathParam = new Parameter();
pathParam.setName(listKey.getLocalName());
- pathParam.setDescription(_dataChildByName.getDescription());
+ pathParam.setDescription(dataChildByName.getDescription());
pathParam.setType("string");
pathParam.setParamType("path");
Set<Module> modules = schemaContext.getModules();
- SortedSet<Module> sortedModules = new TreeSet<>(new Comparator<Module>() {
- @Override
- public int compare(Module module1, Module 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);
- result = module1Revision.compareTo(module2Revision);
- }
- if (result == 0) {
- result = module1.getNamespace().compareTo(module2.getNamespace());
- }
- return result;
+ 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);
+ result = module1Revision.compareTo(module2Revision);
+ }
+ if (result == 0) {
+ result = module1.getNamespace().compareTo(module2.getNamespace());
}
+ return result;
});
for (Module m : modules) {
if (m != null) {
import org.slf4j.LoggerFactory;
/**
- * Generates JSON Schema for data defined in Yang
+ * Generates JSON Schema for data defined in YANG.
*/
@NotThreadSafe
public class ModelGenerator {
}
private void processModules(final Module module, final JSONObject models) throws JSONException {
- createConcreteModelForPost(models, module.getName()+ BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX, createPropertiesForPost(module));
+ createConcreteModelForPost(models, module.getName() + BaseYangSwaggerGenerator.MODULE_NAME_SUFFIX, createPropertiesForPost(module));
}
private void processContainersAndLists(final Module module, final JSONObject models, final SchemaContext schemaContext)
* The module from which the identity stmt will be processed
* @param models
* The JSONObject in which the parsed identity will be put as a 'model' obj
- * @throws JSONException
*/
private static void processIdentities(final Module module, final JSONObject models) throws JSONException {
}
/**
- * Processes the container and list nodes and populates the moduleJSON
- *
- * @param container
- * @param moduleName
- * @param isConfig
- * @throws JSONException
- * @throws IOException
+ * Processes the container and list nodes and populates the moduleJSON.
*/
private JSONObject processDataNodeContainer(final DataNodeContainer dataNode, final String moduleName, final JSONObject models,
final SchemaContext schemaContext) throws JSONException, IOException {
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){
+ } else if (childNode instanceof LeafSchemaNode) {
JSONObject property = processLeafNode((LeafSchemaNode)childNode);
properties.put(childNode.getQName().getLocalName(), property);
}
}
/**
- * Processes the nodes
- *
- * @param nodes
- * @param parentQName
- * @param moduleName
- * @param isConfig
- * @return
- * @throws JSONException
- * @throws IOException
+ * Processes the nodes.
*/
- private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName, final String moduleName,
- final JSONObject models, final boolean isConfig, final SchemaContext schemaContext) throws JSONException, IOException {
+ private JSONObject processChildren(final Iterable<DataSchemaNode> nodes, final QName parentQName,
+ final String moduleName, final JSONObject models, final boolean isConfig, final SchemaContext schemaContext)
+ throws JSONException, IOException {
JSONObject properties = new JSONObject();
return properties;
}
- /**
- *
- * @param listNode
- * @throws JSONException
- */
private JSONObject processLeafListNode(final LeafListSchemaNode listNode) throws JSONException {
JSONObject props = new JSONObject();
props.put(TYPE_KEY, ARRAY_TYPE);
return props;
}
- /**
- *
- * @param choiceNode
- * @param moduleName
- * @throws JSONException
- * @throws IOException
- */
private JSONObject processChoiceNode(final ChoiceSchemaNode choiceNode, final String moduleName, final JSONObject models,
final SchemaContext schemaContext) throws JSONException, IOException {
return oneOfProps;
}
- /**
- *
- * @param constraints
- * @param props
- * @throws JSONException
- */
private static void processConstraints(final ConstraintDefinition constraints, final JSONObject props) throws JSONException {
boolean isMandatory = constraints.isMandatory();
props.put(REQUIRED_KEY, isMandatory);
}
}
- /**
- *
- * @param leafNode
- * @return
- * @throws JSONException
- */
private JSONObject processLeafNode(final LeafSchemaNode leafNode) throws JSONException {
JSONObject property = new JSONObject();
return property;
}
- /**
- *
- * @param leafNode
- * @return
- * @throws JSONException
- */
private static JSONObject processAnyXMLNode(final AnyXmlSchemaNode leafNode) throws JSONException {
JSONObject property = new JSONObject();
return property;
}
- /**
- * @param property
- * @throws JSONException
- */
private void processTypeDef(final TypeDefinition<?> leafTypeDef, final JSONObject property) throws JSONException {
-
if (leafTypeDef instanceof ExtendedType) {
processExtendedType(leafTypeDef, property);
} else if (leafTypeDef instanceof BinaryTypeDefinition) {
} else if (leafTypeDef instanceof EnumTypeDefinition) {
processEnumType((EnumTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof IdentityrefTypeDefinition) {
- property.putOpt(TYPE_KEY, ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
+ property.putOpt(TYPE_KEY,
+ ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
} else if (leafTypeDef instanceof StringTypeDefinition) {
processStringType((StringTypeDefinition) leafTypeDef, property);
} else if (leafTypeDef instanceof UnionTypeDefinition) {
}
}
- /**
- *
- * @param leafTypeDef
- * @param property
- * @throws JSONException
- */
private void processExtendedType(final TypeDefinition<?> leafTypeDef, final JSONObject property) throws JSONException {
TypeDefinition<?> leafBaseType = leafTypeDef.getBaseType();
if (leafBaseType instanceof ExtendedType) {
property.put(MEDIA_KEY, media);
}
- /**
- *
- * @param enumLeafType
- * @param property
- * @throws JSONException
- */
private static void processEnumType(final EnumTypeDefinition enumLeafType, final JSONObject property) throws JSONException {
List<EnumPair> enumPairs = enumLeafType.getValues();
- List<String> enumNames = new ArrayList<String>();
+ List<String> enumNames = new ArrayList<>();
for (EnumPair enumPair : enumPairs) {
enumNames.add(enumPair.getName());
}
property.putOpt(ENUM, new JSONArray(enumNames));
}
- /**
- *
- * @param bitsType
- * @param property
- * @throws JSONException
- */
private static void processBitsType(final BitsTypeDefinition bitsType, final JSONObject property) throws JSONException {
property.put(TYPE_KEY, ARRAY_TYPE);
property.put(MIN_ITEMS, 0);
StringTypeDefinition type = stringType;
List<LengthConstraint> lengthConstraints = stringType.getLengthConstraints();
while (lengthConstraints.isEmpty() && type.getBaseType() != null) {
- type = type.getBaseType();
- lengthConstraints = type.getLengthConstraints();
+ type = type.getBaseType();
+ lengthConstraints = type.getLengthConstraints();
}
// FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
property.put(TYPE_KEY, STRING);
}
- /**
- *
- * @param unionType
- * @param property
- * @throws JSONException
- */
private static void processUnionType(final UnionTypeDefinition unionType, final JSONObject property) throws JSONException {
-
StringBuilder type = new StringBuilder();
for (TypeDefinition<?> typeDef : unionType.getTypes()) {
if (type.length() > 0) {
/**
* Helper method to generate a pre-filled JSON schema object.
- *
- * @return
- * @throws JSONException
*/
private static JSONObject getSchemaTemplate() throws JSONException {
JSONObject schemaJSON = new JSONObject();
}
@Override
- public ObjectMapper getContext(Class<?> aClass) {
-
- if (ApiDeclaration.class.isAssignableFrom(aClass)) {
+ public ObjectMapper getContext(Class<?> klass) {
+ if (ApiDeclaration.class.isAssignableFrom(klass)) {
return ctx;
}
- return null;// must return null so that jax-rs can continue context
- // search
+ return null; // must return null so that JAX-RS can continue context search
}
}
public static final String CONFIG = "(config)";
public static final List<String> CONSUMES_PUT_POST = new ArrayList<>();
+
static {
CONSUMES_PUT_POST.add("application/json");
CONSUMES_PUT_POST.add("application/xml");
Parameter payload = new Parameter();
payload.setParamType("body");
payload.setType(CONFIG + node.getQName().getLocalName());
- payload.setName("**"+CONFIG + node.getQName().getLocalName());
+ payload.setName("**" + CONFIG + node.getQName().getLocalName());
parameters.add(payload);
}
}
import com.google.common.base.Optional;
import java.util.Collections;
-import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
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());
- }
- });
+ (o1, o2) -> o1.toString().compareToIgnoreCase(o2.toString()));
private final Map<Long, YangInstanceIdentifier> longIdToInstanceId = new HashMap<>();
private final Object lock = new Object();
return null; // indicating not found.
}
SchemaContext context = getSchemaContext(iid);
- String urlPrefix = getYangMountUrl(iid);
if (context == null) {
return createResourceList();
}
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);
resources.addAll(list.getApis());
list.setApis(resources);
}
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));
"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));
+
+ ApiDeclaration declaration = super.createApiDeclaration(createBasePathFromUriInfo(uriInfo));
declaration.setApis(apis);
return declaration;
private RestDocgenUtil() {
}
- private static Map<URI, Map<Date, Module>> namespaceAndRevisionToModule = new HashMap<URI, Map<Date, Module>>();
+ private static Map<URI, Map<Date, Module>> namespaceAndRevisionToModule = new HashMap<>();
/**
* Resolve path argument name for {@code node}.
*
- * The name can contain also prefix which consists of module name followed by colon. The module prefix is presented
- * if namespace of {@code node} and its parent is different. In other cases only name of {@code node} is returned.
+ * <p>The name can contain also prefix which consists of module name followed by colon. The module
+ * prefix is presented if namespace of {@code node} and its parent is different. In other cases
+ * only name of {@code node} is returned.
*
* @return name of {@code node}
*/
}
}
- private synchronized static String resolveFullNameFromNode(final SchemaNode node, final SchemaContext schemaContext) {
+ private static synchronized String resolveFullNameFromNode(final SchemaNode node,
+ final SchemaContext schemaContext) {
final URI namespace = node.getQName().getNamespace();
final Date revision = node.getQName().getRevision();
return node.getQName().getLocalName();
}
- public static String resolveNodesName(final SchemaNode node, final Module module, final SchemaContext schemaContext) {
+ public static String resolveNodesName(final SchemaNode node, final Module module,
+ final SchemaContext schemaContext) {
if (node.getQName().getNamespace().equals(module.getQNameModule().getNamespace())
&& node.getQName().getRevision().equals(module.getQNameModule().getRevision())) {
return node.getQName().getLocalName();