package org.opennaas.itests.router.shell;
import static org.openengsb.labs.paxexam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.opennaas.itests.helpers.OpennaasExamOptions.includeFeatures;
import static org.opennaas.itests.helpers.OpennaasExamOptions.noConsole;
import static org.opennaas.itests.helpers.OpennaasExamOptions.opennaasDistributionConfiguration;
import static org.ops4j.pax.exam.CoreOptions.options;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.felix.service.command.CommandProcessor;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.opennaas.core.resources.IResource;
import org.opennaas.core.resources.IResourceManager;
import org.opennaas.core.resources.ResourceException;
import org.opennaas.core.resources.descriptor.ResourceDescriptor;
import org.opennaas.core.resources.helpers.ResourceDescriptorFactory;
import org.opennaas.core.resources.helpers.ResourceHelper;
import org.opennaas.core.resources.protocol.IProtocolManager;
import org.opennaas.core.resources.protocol.IProtocolSessionManager;
import org.opennaas.core.resources.protocol.ProtocolException;
import org.opennaas.core.resources.protocol.ProtocolSessionContext;
import org.opennaas.extensions.router.model.ComputerSystem;
import org.opennaas.extensions.router.model.EthernetPort;
import org.opennaas.extensions.router.model.IPProtocolEndpoint;
import org.opennaas.extensions.router.model.LogicalDevice;
import org.opennaas.extensions.router.model.LogicalTunnelPort;
import org.opennaas.extensions.router.model.ProtocolEndpoint;
import org.opennaas.extensions.router.model.utils.IPUtilsHelper;
import org.opennaas.itests.helpers.AbstractKarafCommandTest;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.Configuration;
import org.ops4j.pax.exam.junit.ExamReactorStrategy;
import org.ops4j.pax.exam.junit.JUnit4TestRunner;
import org.ops4j.pax.exam.spi.reactors.EagerSingleStagedReactorFactory;
import org.ops4j.pax.exam.util.Filter;
import org.osgi.framework.BundleContext;
import org.osgi.service.blueprint.container.BlueprintContainer;
@RunWith(JUnit4TestRunner.class)
@ExamReactorStrategy(EagerSingleStagedReactorFactory.class)
public class InterfacesIPKarafTest extends AbstractKarafCommandTest
{
private final static Log log = LogFactory.getLog(InterfacesIPKarafTest.class);
private String resourceFriendlyID;
private IResource resource;
private boolean isMock = false;
@Inject
private IProtocolManager protocolManager;
@Inject
private IResourceManager resourceManager;
@Inject
private CommandProcessor commandprocessor;
@Inject
private BundleContext bundleContext;
@SuppressWarnings("unused")
@Inject
@Filter(value = "(osgi.blueprint.container.symbolicname=org.opennaas.extensions.router.repository)", timeout = 20000)
private BlueprintContainer routerService;
@SuppressWarnings("unused")
@Inject
@Filter(value = "(osgi.blueprint.container.symbolicname=org.opennaas.extensions.queuemanager)", timeout = 20000)
private BlueprintContainer queueService;
@SuppressWarnings("unused")
@Inject
@Filter(value = "(osgi.blueprint.container.symbolicname=org.opennaas.extensions.router.capability.ip)", timeout = 20000)
private BlueprintContainer ipService;
@Configuration
public static Option[] configuration() {
return options(opennaasDistributionConfiguration(),
includeFeatures("opennaas-router", "opennaas-junos", "itests-helpers"),
noConsole(),
keepRuntimeFolder());
}
public Boolean createProtocolForResource(String resourceId) throws ProtocolException {
ProtocolSessionContext context = ResourceHelper.newSessionContextNetconf();
IProtocolSessionManager protocolSessionManager = protocolManager.getProtocolSessionManagerWithContext(resourceId, context);
if (context.getSessionParameters().get(context.PROTOCOL_URI).toString().contains("mock")) {
return true;
}
return false;
}
@After
public void deleteResource() throws InterruptedException, ResourceException {
resourceManager.stopResource(resource.getResourceIdentifier());
resourceManager.removeResource(resource.getResourceIdentifier());
for (IResource resource : resourceManager.listResources()) {
resourceManager.removeResource(resource.getResourceIdentifier());
}
}
@Before
public void initTest() throws ProtocolException, ResourceException {
List<String> capabilities = new ArrayList<String>();
capabilities.add("ip");
capabilities.add("queue");
ResourceDescriptor resourceDescriptor = ResourceDescriptorFactory.newResourceDescriptor("junosm20", "router", capabilities);
resourceFriendlyID = resourceDescriptor.getInformation().getType() + ":" + resourceDescriptor.getInformation().getName();
resource = resourceManager.createResource(resourceDescriptor);
isMock = createProtocolForResource(resource.getResourceIdentifier().getId());
resourceManager.startResource(resource.getResourceIdentifier());
}
@Test
public void setIPTest() throws Exception {
// SEt LT
String newIp = "192.168.1.6";
String newMask = "255.255.255.0";
// For Real Router Test
// String inter = "lt-0/1/2";
// For mock TEST
String inter = "lt-0/1/2";
String subport = "12";
testingMethod(inter, subport, newIp, newMask);
// SEt ETH
newIp = "192.168.1.4";
newMask = "255.255.255.0";
// For Real Router Test
// inter = "fe-0/3/1";
// For mock TEST
inter = "fe-0/1/3";
subport = "0";
testingMethod(inter, subport, newIp, newMask);
// LO
List<String> response = executeCommand("ip:setIP " + resourceFriendlyID + " lo0.1 192.168.1.1/24");
//
// assert command output contains "[ERROR] Configuration for Loopback interface not allowed"
// Assert.assertTrue(response.contains("[ERROR] Configuration for Loopback interface not allowed"));
Assert.assertTrue(response.get(1).contains("[ERROR] Configuration for Loopback interface not allowed"));
}
/**
* Configure a IP in a lt interface
*
*/
public void setIPlt() throws Exception {
String newIp = "192.168.1.6";
String newMask = "255.255.255.0";
String inter = "lt-0/1/2";
String subport = "12";
testingMethod(inter, subport, newIp, newMask);
// SET LO
List<String> response = executeCommand("ip:setIP " + resourceFriendlyID + " lo0.1 192.168.1.1/24");
//
// assert command output contains "[ERROR] Configuration for Loopback interface not allowed"
Assert.assertTrue(response.get(1).contains("[ERROR] Configuration for Loopback interface not allowed"));
}
/**
* Configure a IP in a ETh interface
*
*/
public void setIPETH() throws Exception {
String newIp = "192.168.1.4";
String newMask = "255.255.255.0";
String inter = "fe-0/3/1";
String subport = "0";
testingMethod(inter, subport, newIp, newMask);
}
public void testingMethod(String inter, String subport, String newIp, String newMask) throws Exception {
// REFRESH to fill up the model
String newMaskFormated = IPUtilsHelper.parseLongToShortIpv4NetMask(newMask);
List<String> response = executeCommand("ip:list " + resourceFriendlyID);
Assert.assertTrue(response.get(1).isEmpty());
// Obtain the previous IP/MASK make the rollback of the test
List<String> OldIPMask = getOldInterface(resource, inter, subport);
// SET NEW IP
response = executeCommand(
"ip:setIP " + resourceFriendlyID + " " + inter + "." + subport + " " + newIp + "/" + newMaskFormated);
Assert.assertTrue(response.get(1).isEmpty());
response = executeCommand("queue:execute " + resourceFriendlyID);
Assert.assertTrue(response.get(1).isEmpty());
// Check that the resource have the old IP configured in the model despite of have send the command
checkModel(inter, subport, OldIPMask.get(0), OldIPMask.get(1), resource);
// REFRESH to fill up the model
response = executeCommand("ip:list " + resourceFriendlyID);
Assert.assertTrue(response.get(1).isEmpty());
// CHECK CHANGES IN THE INTERFACE
checkModel(inter, subport, newIp, newMask, resource);
// ROLLBACK OF THE INTERFACE
response = executeCommand(
"ip:setIP " + resourceFriendlyID + " " + inter + "." + subport + " " + OldIPMask.get(0) + "/" + IPUtilsHelper
.parseLongToShortIpv4NetMask(OldIPMask.get(1)));
Assert.assertTrue(response.get(1).isEmpty());
response = executeCommand("queue:execute " + resourceFriendlyID);
Assert.assertTrue(response.get(1).isEmpty());
// REFRESH
response = executeCommand("ip:list " + resourceFriendlyID);
Assert.assertTrue(response.get(1).isEmpty());
// CHECK THe ROLLBACK IS DONE
checkModel(inter, subport, OldIPMask.get(0), OldIPMask.get(1), resource);
}
public void checkModel(String inter, String port, String Ip, String mask, IResource resource) {
Boolean found = false;
if (!isMock) {
ComputerSystem system = (ComputerSystem) resource.getModel();
List<LogicalDevice> ld = system.getLogicalDevices();
Assert.assertNotNull(ld);
for (LogicalDevice l : ld) {
// Only check the modified interface
if (l.getName().equalsIgnoreCase(inter)) {
if (l instanceof EthernetPort) {
EthernetPort eth = (EthernetPort) l;
if (eth.getPortNumber() == Integer.parseInt(port)) {
found = true;
List<ProtocolEndpoint> pp = eth.getProtocolEndpoint();
Assert.assertNotNull(pp);
for (ProtocolEndpoint p : pp) {
if (p instanceof IPProtocolEndpoint) {
Assert.assertEquals(Ip, ((IPProtocolEndpoint) p).getIPv4Address());
Assert.assertEquals(mask, ((IPProtocolEndpoint) p).getSubnetMask());
}
}
}
} else if (l instanceof LogicalTunnelPort) {
LogicalTunnelPort lt = (LogicalTunnelPort) l;
if (lt.getPortNumber() == Integer.parseInt(port)) {
found = true;
List<ProtocolEndpoint> pp = lt.getProtocolEndpoint();
Assert.assertNotNull(pp);
for (ProtocolEndpoint p : pp) {
if (p instanceof IPProtocolEndpoint) {
Assert.assertEquals(Ip, ((IPProtocolEndpoint) p).getIPv4Address());
Assert.assertEquals(mask, ((IPProtocolEndpoint) p).getSubnetMask());
}
}
}
}
}
}
// Look if exits the interface to be checked
Assert.assertTrue(found);
}
}
public List<String> getOldInterface(IResource resource, String inter, String port) {
List<String> result = new ArrayList<String>();
ComputerSystem system = (ComputerSystem) resource.getModel();
List<LogicalDevice> ld = system.getLogicalDevices();
Assert.assertNotNull(ld);
for (LogicalDevice l : ld) {
// Only check the modified interface
if (l.getName().equalsIgnoreCase(inter)) {
if (l instanceof EthernetPort) {
EthernetPort eth = (EthernetPort) l;
if (eth.getPortNumber() == Integer.parseInt(port)) {
List<ProtocolEndpoint> pp = eth.getProtocolEndpoint();
Assert.assertNotNull(pp);
for (ProtocolEndpoint p : pp) {
if (p instanceof IPProtocolEndpoint) {
result.add(((IPProtocolEndpoint) p).getIPv4Address());
result.add(((IPProtocolEndpoint) p).getSubnetMask());
}
}
}
} else if (l instanceof LogicalTunnelPort) {
LogicalTunnelPort lt = (LogicalTunnelPort) l;
if (lt.getPortNumber() == Integer.parseInt(port)) {
List<ProtocolEndpoint> pp = lt.getProtocolEndpoint();
Assert.assertNotNull(pp);
for (ProtocolEndpoint p : pp) {
if (p instanceof IPProtocolEndpoint) {
result.add(((IPProtocolEndpoint) p).getIPv4Address());
result.add(((IPProtocolEndpoint) p).getSubnetMask());
}
}
}
}
}
}
Assert.assertFalse(result.isEmpty());
Assert.assertNotNull(result);
return result;
}
}