package org.opennaas.core.resources.shell;
/*
* #%L
* OpenNaaS :: Core :: Resources
* %%
* Copyright (C) 2007 - 2014 FundaciĆ³ Privada i2CAT, Internet i InnovaciĆ³ a Catalunya
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
import java.io.File;
import org.apache.felix.gogo.commands.Argument;
import org.apache.felix.gogo.commands.Command;
import org.apache.felix.gogo.commands.Option;
import org.opennaas.core.resources.IResourceIdentifier;
import org.opennaas.core.resources.ResourceException;
import org.opennaas.core.resources.ResourceManager;
/**
* Export the descriptor of a resource to a file
*
* @author Scott Campbell (CRC)
*
*/
@Command(scope = "resource", name = "export", description = "Export the descriptor of a resource to a file")
public class ExportResourceDescriptorCommand extends GenericKarafCommand {
@Argument(index = 0, name = "resourceType:resourceName", description = "The resource whose descriptor is going to be exported", required = true, multiValued = false)
private String resourceId = null;
@Argument(index = 1, name = "fileName", description = "The path to the file where the descriptor will be exported", required = true, multiValued = false)
private String fileName = null;
@Option(name = "--networkTopology", aliases = { "-nt" }, description = "Allows explicit file where the network topology will be saved")
private String networkFileName = null;
@Override
protected Object doExecute() throws Exception {
printInitCommand("export resource descriptor");
try {
ResourceManager manager = (ResourceManager) getResourceManager();
String[] argsRouterName = new String[2];
try {
argsRouterName = splitResourceName(resourceId);
} catch (Exception e) {
printError(e.getMessage());
printEndCommand();
return -1;
}
IResourceIdentifier identifier = null;
// check the file Name ends with .descriptor
File file = new File(fileName);
if (file.getName().endsWith(".descriptor") || file.getName().endsWith(".xml")) {
identifier = manager.getIdentifierFromResourceName(argsRouterName[0], argsRouterName[1]);
if (identifier == null) {
printError("Resource " + resourceId + " not found in Repository");
// printError("Export cancelled.");
return null;
}
try {
if (networkFileName != null) {
File networkFile = new File(networkFileName);
if (networkFile.getName().endsWith(".descriptor") || networkFile.getName().endsWith(".xml")) {
manager.exportNetworkTopology(identifier, networkFileName);
printInfo("Export network descriptor succesful for resource: " + resourceId);
}
}
manager.exportResourceDescriptor(identifier, fileName);
printInfo("Export descriptor succesful for resource: " + resourceId);
} catch (ResourceException e) {
printError("An error occurred exporting descriptor of resource " + resourceId);
printError(e);
}
} else {
printError("The type of the file must be .descriptor ");
}
} catch (Exception e) {
printError(e);
printError("Error exporting Resource.");
}
printEndCommand();
return null;
}
}