use of com.cloud.api.APICommand in project cosmic by MissionCriticalCloud.
the class ApiDiscoveryServiceImpl method cacheResponseMap.
protected Map<String, List<String>> cacheResponseMap(final Set<Class<?>> cmdClasses) {
final Map<String, List<String>> responseApiNameListMap = new HashMap<>();
for (final Class<?> cmdClass : cmdClasses) {
APICommand apiCmdAnnotation = cmdClass.getAnnotation(APICommand.class);
if (apiCmdAnnotation == null) {
apiCmdAnnotation = cmdClass.getSuperclass().getAnnotation(APICommand.class);
}
if (apiCmdAnnotation == null || !apiCmdAnnotation.includeInApiDoc() || apiCmdAnnotation.name().isEmpty()) {
continue;
}
final String apiName = apiCmdAnnotation.name();
if (s_logger.isTraceEnabled()) {
s_logger.trace("Found api: " + apiName);
}
final ApiDiscoveryResponse response = getCmdRequestMap(cmdClass, apiCmdAnnotation);
final String responseName = apiCmdAnnotation.responseObject().getName();
if (!responseName.contains("SuccessResponse")) {
if (!responseApiNameListMap.containsKey(responseName)) {
responseApiNameListMap.put(responseName, new ArrayList<>());
}
responseApiNameListMap.get(responseName).add(apiName);
}
response.setRelated(responseName);
final Field[] responseFields = apiCmdAnnotation.responseObject().getDeclaredFields();
for (final Field responseField : responseFields) {
final ApiResponseResponse responseResponse = getFieldResponseMap(responseField);
response.addApiResponse(responseResponse);
}
response.setObjectName("api");
s_apiNameDiscoveryResponseMap.put(apiName, response);
}
for (final String apiName : s_apiNameDiscoveryResponseMap.keySet()) {
final ApiDiscoveryResponse response = s_apiNameDiscoveryResponseMap.get(apiName);
final Set<ApiParameterResponse> processedParams = new HashSet<>();
for (final ApiParameterResponse param : response.getParams()) {
if (responseApiNameListMap.containsKey(param.getRelated())) {
final List<String> relatedApis = responseApiNameListMap.get(param.getRelated());
param.setRelated(StringUtils.join(relatedApis, ","));
} else {
param.setRelated(null);
}
processedParams.add(param);
}
response.setParams(processedParams);
if (responseApiNameListMap.containsKey(response.getRelated())) {
final List<String> relatedApis = responseApiNameListMap.get(response.getRelated());
relatedApis.remove(apiName);
response.setRelated(StringUtils.join(relatedApis, ","));
} else {
response.setRelated(null);
}
s_apiNameDiscoveryResponseMap.put(apiName, response);
}
return responseApiNameListMap;
}
use of com.cloud.api.APICommand in project cosmic by MissionCriticalCloud.
the class ApiXmlDocWriter method main.
public static void main(final String[] args) {
final Set<Class<?>> cmdClasses = ReflectUtil.getClassesWithAnnotation(APICommand.class, new String[] { "com.cloud.api", "com.cloud.api", "com.cloud.api.commands", "com.cloud.api.command.admin.zone", "com.cloud.network.contrail.api.command" });
for (final Class<?> cmdClass : cmdClasses) {
if (cmdClass.getAnnotation(APICommand.class) == null) {
System.out.println("Warning, API Cmd class " + cmdClass.getName() + " has no APICommand annotation ");
continue;
}
final String apiName = cmdClass.getAnnotation(APICommand.class).name();
if (s_apiNameCmdClassMap.containsKey(apiName)) {
// handle API cmd separation into admin cmd and user cmd with the common api name
final Class<?> curCmd = s_apiNameCmdClassMap.get(apiName);
if (curCmd.isAssignableFrom(cmdClass)) {
// api_cmd map always keep the admin cmd class to get full response and parameters
s_apiNameCmdClassMap.put(apiName, cmdClass);
} else if (cmdClass.isAssignableFrom(curCmd)) {
// just skip this one without warning
continue;
} else {
System.out.println("Warning, API Cmd class " + cmdClass.getName() + " has non-unique apiname " + apiName);
continue;
}
} else {
s_apiNameCmdClassMap.put(apiName, cmdClass);
}
}
final LinkedProperties preProcessedCommands = new LinkedProperties();
String[] fileNames = null;
final List<String> argsList = Arrays.asList(args);
final Iterator<String> iter = argsList.iterator();
while (iter.hasNext()) {
final String arg = iter.next();
// populate the file names
if (arg.equals("-f")) {
fileNames = iter.next().split(",");
}
if (arg.equals("-d")) {
s_dirName = iter.next();
}
}
if ((fileNames == null) || (fileNames.length == 0)) {
System.out.println("Please specify input file(s) separated by coma using -f option");
System.exit(2);
}
for (final String fileName : fileNames) {
try (FileInputStream in = new FileInputStream(fileName)) {
preProcessedCommands.load(in);
} catch (final FileNotFoundException ex) {
System.out.println("Can't find file " + fileName);
System.exit(2);
} catch (final IOException ex1) {
System.out.println("Error reading from file " + ex1);
System.exit(2);
}
}
final Iterator<?> propertiesIterator = preProcessedCommands.keys.iterator();
// Get command classes and response object classes
while (propertiesIterator.hasNext()) {
final String key = (String) propertiesIterator.next();
final String preProcessedCommand = preProcessedCommands.getProperty(key);
final int splitIndex = preProcessedCommand.lastIndexOf(";");
final String commandRoleMask = preProcessedCommand.substring(splitIndex + 1);
final Class<?> cmdClass = s_apiNameCmdClassMap.get(key);
if (cmdClass == null) {
System.out.println("Check, is this api part of another build profile? Null value for key: " + key + " preProcessedCommand=" + preProcessedCommand);
continue;
}
final String commandName = cmdClass.getName();
s_allApiCommands.put(key, commandName);
short cmdPermissions = 1;
if (commandRoleMask != null) {
cmdPermissions = Short.parseShort(commandRoleMask);
}
if ((cmdPermissions & DOMAIN_ADMIN_COMMAND) != 0) {
s_domainAdminApiCommands.put(key, commandName);
}
if ((cmdPermissions & USER_COMMAND) != 0) {
s_regularUserApiCommands.put(key, commandName);
}
}
s_allApiCommandsSorted.putAll(s_allApiCommands);
s_domainAdminApiCommandsSorted.putAll(s_domainAdminApiCommands);
s_regularUserApiCommandsSorted.putAll(s_regularUserApiCommands);
try {
// Create object writer
final XStream xs = new XStream();
xs.alias("command", Command.class);
xs.alias("arg", Argument.class);
final String xmlDocDir = s_dirName + "/xmldoc";
final String rootAdminDirName = xmlDocDir + "/root_admin";
final String domainAdminDirName = xmlDocDir + "/domain_admin";
final String regularUserDirName = xmlDocDir + "/regular_user";
(new File(rootAdminDirName)).mkdirs();
(new File(domainAdminDirName)).mkdirs();
(new File(regularUserDirName)).mkdirs();
final ObjectOutputStream out = xs.createObjectOutputStream(new FileWriter(s_dirName + "/commands.xml"), "commands");
final ObjectOutputStream rootAdmin = xs.createObjectOutputStream(new FileWriter(rootAdminDirName + "/" + "rootAdminSummary.xml"), "commands");
final ObjectOutputStream rootAdminSorted = xs.createObjectOutputStream(new FileWriter(rootAdminDirName + "/" + "rootAdminSummarySorted.xml"), "commands");
final ObjectOutputStream domainAdmin = xs.createObjectOutputStream(new FileWriter(domainAdminDirName + "/" + "domainAdminSummary.xml"), "commands");
final ObjectOutputStream outDomainAdminSorted = xs.createObjectOutputStream(new FileWriter(domainAdminDirName + "/" + "domainAdminSummarySorted.xml"), "commands");
final ObjectOutputStream regularUser = xs.createObjectOutputStream(new FileWriter(regularUserDirName + "/regularUserSummary.xml"), "commands");
final ObjectOutputStream regularUserSorted = xs.createObjectOutputStream(new FileWriter(regularUserDirName + "/regularUserSummarySorted.xml"), "commands");
// Write commands in the order they are represented in commands.properties.in file
Iterator<?> it = s_allApiCommands.keySet().iterator();
while (it.hasNext()) {
final String key = (String) it.next();
// Write admin commands
writeCommand(out, key);
writeCommand(rootAdmin, key);
// Write single commands to separate xml files
final ObjectOutputStream singleRootAdminCommandOs = xs.createObjectOutputStream(new FileWriter(rootAdminDirName + "/" + key + ".xml"), "command");
writeCommand(singleRootAdminCommandOs, key);
singleRootAdminCommandOs.close();
if (s_domainAdminApiCommands.containsKey(key)) {
writeCommand(domainAdmin, key);
final ObjectOutputStream singleDomainAdminCommandOs = xs.createObjectOutputStream(new FileWriter(domainAdminDirName + "/" + key + ".xml"), "command");
writeCommand(singleDomainAdminCommandOs, key);
singleDomainAdminCommandOs.close();
}
if (s_regularUserApiCommands.containsKey(key)) {
writeCommand(regularUser, key);
final ObjectOutputStream singleRegularUserCommandOs = xs.createObjectOutputStream(new FileWriter(regularUserDirName + "/" + key + ".xml"), "command");
writeCommand(singleRegularUserCommandOs, key);
singleRegularUserCommandOs.close();
}
}
// Write sorted commands
it = s_allApiCommandsSorted.keySet().iterator();
while (it.hasNext()) {
final String key = (String) it.next();
writeCommand(rootAdminSorted, key);
if (s_domainAdminApiCommands.containsKey(key)) {
writeCommand(outDomainAdminSorted, key);
}
if (s_regularUserApiCommands.containsKey(key)) {
writeCommand(regularUserSorted, key);
}
}
out.close();
rootAdmin.close();
rootAdminSorted.close();
domainAdmin.close();
outDomainAdminSorted.close();
regularUser.close();
regularUserSorted.close();
// write alerttypes to xml
writeAlertTypes(xmlDocDir);
// gzip directory with xml doc
// zipDir(dirName + "xmldoc.zip", xmlDocDir);
// Delete directory
// deleteDir(new File(xmlDocDir));
} catch (final Exception ex) {
ex.printStackTrace();
System.exit(2);
}
}
use of com.cloud.api.APICommand in project cosmic by MissionCriticalCloud.
the class ApiXmlDocWriter method writeCommand.
private static void writeCommand(final ObjectOutputStream out, final String command) throws ClassNotFoundException, IOException {
final Class<?> clas = Class.forName(s_allApiCommands.get(command));
final ArrayList<Argument> request;
final ArrayList<Argument> response;
// Create a new command, set name/description/usage
final Command apiCommand = new Command();
apiCommand.setName(command);
APICommand impl = clas.getAnnotation(APICommand.class);
if (impl == null) {
impl = clas.getSuperclass().getAnnotation(APICommand.class);
}
if (impl == null) {
throw new IllegalStateException(String.format("An %1$s annotation is required for class %2$s.", APICommand.class.getCanonicalName(), clas.getCanonicalName()));
}
if (impl.includeInApiDoc()) {
final String commandDescription = impl.description();
if (commandDescription != null && !commandDescription.isEmpty()) {
apiCommand.setDescription(commandDescription);
} else {
System.out.println("Command " + apiCommand.getName() + " misses description");
}
final String commandUsage = impl.usage();
if (commandUsage != null && !commandUsage.isEmpty()) {
apiCommand.setUsage(commandUsage);
}
// Set version when the API is added
if (!impl.since().isEmpty()) {
apiCommand.setSinceVersion(impl.since());
}
final boolean isAsync = ReflectUtil.isCmdClassAsync(clas, new Class<?>[] { BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
apiCommand.setAsync(isAsync);
final Set<Field> fields = ReflectUtil.getAllFieldsForClass(clas, new Class<?>[] { BaseCmd.class, BaseAsyncCmd.class, BaseAsyncCreateCmd.class });
request = setRequestFields(fields);
// Get response parameters
final Class<?> responseClas = impl.responseObject();
final Field[] responseFields = responseClas.getDeclaredFields();
response = setResponseFields(responseFields, responseClas);
apiCommand.setRequest(request);
apiCommand.setResponse(response);
out.writeObject(apiCommand);
} else {
s_logger.debug("Command " + command + " is not exposed in api doc");
}
}
Aggregations