From 41608cf440761e08334d3a12fcf0cb72ef38e8dd Mon Sep 17 00:00:00 2001 From: Zhang Yali Date: Mon, 25 Jan 2016 19:18:55 +0800 Subject: [PATCH] Bug 5077: Codes break the security rules - Change the method of catching Throwable and errors - Remove all the .printStackTrace method. Change-Id: I3cede81b38564c8fda1d881d76df84d813db5cc0 Signed-off-by: Zhang Yali --- .../computation/PNResourcesTracker.java | 3 +- .../intent/condition/ConditionMonitor.java | 7 +- .../opendaylight/nemo/user/UserManager.java | 17 +- .../nemo/user/tenantmanager/TenantManage.java | 28 +- .../NEMOParse/ParseException.java | 13 +- .../NEMOParse/SimpleCharStream.java | 4 +- .../updateintent/GetDefinitions.java | 4 +- .../nemo/renderer/cli/CliBuilder.java | 3 +- .../nemo/renderer/cli/CliTrigger.java | 12 +- .../nemo/renderer/cli/TelnetUtils.java | 8 +- .../physicalnetwork/DataBrokerAdapter.java | 15 +- .../PhysicalResourceLoader.java | 2 +- .../renderer/cli/physicalnetwork/Utils.java | 4 +- .../renderer/openflow/FlowTableManager.java | 12 +- .../physicalnetwork/DataBrokerAdapter.java | 15 +- .../physicalnetwork/PhyConfigLoader.java | 2 +- .../PhysicalNetworkAdapter.java | 3 +- .../openflow/physicalnetwork/Utils.java | 11 +- .../nemo/tool/sandbox/CmdExecutor.java | 7 +- .../nemo/tool/sandbox/EasyCli.java | 3 +- .../nemo/tool/sandbox/models/Cache.java | 4 +- .../nemo/tool/sandbox/models/Firewall.java | 4 +- .../nemo/tool/sandbox/models/Link.java | 7 +- .../nemo/tool/sandbox/models/Network.java | 18 +- .../nemo/tool/sandbox/models/Node.java | 3 +- .../tool/sandbox/models/VirtualMachine.java | 4 +- .../nemo/tool/sandbox/utils/FileUtils.java | 3 +- .../nemo/tool/sandbox/CliTest.java | 44 --- .../nemo/tool/sandbox/CmdExecutorTest.java | 89 ------- .../nemo/tool/sandbox/EasyCliTest.java | 49 ---- .../nemo/tool/sandbox/SandBoxManagerTest.java | 252 ------------------ .../nemo/tool/sandbox/models/CacheTest.java | 66 ----- .../tool/sandbox/models/ConnectorTest.java | 72 ----- .../tool/sandbox/models/FirewallTest.java | 68 ----- .../nemo/tool/sandbox/models/LinkTest.java | 84 ------ .../nemo/tool/sandbox/models/NetworkTest.java | 130 --------- .../nemo/tool/sandbox/models/RouterTest.java | 35 --- .../nemo/tool/sandbox/models/SwitchTest.java | 70 ----- .../sandbox/models/VirtualMachineTest.java | 68 ----- .../northbound/SandboxNorthboundTest.java | 81 ------ 40 files changed, 137 insertions(+), 1187 deletions(-) delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CliTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CmdExecutorTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/EasyCliTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/SandBoxManagerTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/CacheTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/ConnectorTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/FirewallTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/LinkTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/NetworkTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/RouterTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/SwitchTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachineTest.java delete mode 100644 nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/northbound/SandboxNorthboundTest.java diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/intent/computation/PNResourcesTracker.java b/nemo-impl/src/main/java/org/opendaylight/nemo/intent/computation/PNResourcesTracker.java index 566f4ef..a6a9f44 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/intent/computation/PNResourcesTracker.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/intent/computation/PNResourcesTracker.java @@ -134,7 +134,8 @@ public class PNResourcesTracker { intentResolver.resolveIntent(userId); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } } diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/intent/condition/ConditionMonitor.java b/nemo-impl/src/main/java/org/opendaylight/nemo/intent/condition/ConditionMonitor.java index 0345a48..84ff12f 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/intent/condition/ConditionMonitor.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/intent/condition/ConditionMonitor.java @@ -11,7 +11,8 @@ package org.opendaylight.nemo.intent.condition; import org.opendaylight.nemo.intent.IntentResolver; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.condition.instance.ConditionSegment; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.List; import java.util.TimerTask; @@ -20,6 +21,7 @@ import java.util.TimerTask; * TimerTask for one condition. */ public class ConditionMonitor extends TimerTask { + private static Logger log = LoggerFactory.getLogger(ConditionMonitor.class); private List conditionSegments; //Handle operation when condition state changed private IntentResolver intentResolver; @@ -40,7 +42,8 @@ public class ConditionMonitor extends TimerTask { try { intentResolver.resolveIntent(userId); } catch (Exception e) { - e.printStackTrace(); + //TODO Auto-generated catch block + log.error("Exception:",e); } preState = currentState; } diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/user/UserManager.java b/nemo-impl/src/main/java/org/opendaylight/nemo/user/UserManager.java index dd06ce6..ae2f91b 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/user/UserManager.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/user/UserManager.java @@ -25,7 +25,8 @@ import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.Upd import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.*; import org.opendaylight.yangtools.yang.common.RpcResult; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.concurrent.Future; import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.CommonRpcResult.ResultCode.Error; @@ -35,7 +36,7 @@ import static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.n * Created by z00293636 on 2015/9/7. */ public class UserManager implements NemoIntentService { - + private static final Logger LOG = LoggerFactory.getLogger(UserManager.class); private final DataBroker dataBroker; private RegisterUser registerUser; @@ -129,15 +130,15 @@ public class UserManager implements NemoIntentService { outputBuilder.setResultCode(Ok).setMessage("The transaction ends."); } catch (IntentResolutionException e){ - e.printStackTrace(); + LOG.error("Exception:",e); outputBuilder.setResultCode(Error).setMessage(e.getMessage()); } catch (VNMappingException e){ - e.printStackTrace(); + LOG.error("Exception:",e); outputBuilder.setResultCode(Error).setMessage(e.getMessage()); } catch (Exception e){ - e.printStackTrace(); + LOG.error("Exception:",e); } } else{ @@ -168,11 +169,11 @@ public class UserManager implements NemoIntentService { } } catch (ParseException e) { - e.printStackTrace(); + LOG.error("Exception:",e); outputBuilder.setResultCode(Error).setMessage(e.getMessage()); } catch (NumberFormatException e) { - e.printStackTrace(); + LOG.error("Exception:",e); outputBuilder.setResultCode(Error).setMessage(e.getMessage()); } return RpcResultBuilder.success(outputBuilder).buildFuture(); @@ -224,7 +225,7 @@ public class UserManager implements NemoIntentService { } } catch (NumberFormatException e){ - e.printStackTrace(); + LOG.error("Exception:",e); outputBuilder.setResultCode(Error).setMessage(e.getMessage()); } return RpcResultBuilder.success(outputBuilder).buildFuture(); diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/user/tenantmanager/TenantManage.java b/nemo-impl/src/main/java/org/opendaylight/nemo/user/tenantmanager/TenantManage.java index aca173a..3780f39 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/user/tenantmanager/TenantManage.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/user/tenantmanager/TenantManage.java @@ -182,10 +182,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } @@ -519,10 +519,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } @@ -547,10 +547,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } @@ -574,10 +574,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } @@ -603,10 +603,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } @@ -632,10 +632,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } @@ -661,10 +661,10 @@ public class TenantManage { f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } } } diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/ParseException.java b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/ParseException.java index 7aad8aa..2f79e5c 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/ParseException.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/ParseException.java @@ -8,6 +8,7 @@ /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 5.0 */ /* JavaCCOptions:KEEP_LINE_COL=null */ package org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOParse; +import java.util.Arrays; /** * This exception is thrown when parse errors are encountered. @@ -38,10 +39,14 @@ public class ParseException extends Exception { String[] tokenImageVal ) { - super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); - currentToken = currentTokenVal; - expectedTokenSequences = expectedTokenSequencesVal; - tokenImage = tokenImageVal; + + super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal)); + this.currentToken = currentTokenVal; + this.tokenImage = Arrays.copyOf(tokenImageVal,tokenImageVal.length); + this.expectedTokenSequences = Arrays.copyOf(expectedTokenSequencesVal,expectedTokenSequencesVal.length); +// currentToken = currentTokenVal; +// expectedTokenSequences = expectedTokenSequencesVal; +// tokenImage = tokenImageVal; } /** diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/SimpleCharStream.java b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/SimpleCharStream.java index 91bb640..232b31f 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/SimpleCharStream.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/languagestyle/NEMOParse/SimpleCharStream.java @@ -81,9 +81,9 @@ public class SimpleCharStream maxNextCharInd = (bufpos -= tokenBegin); } } - catch (Throwable t) + catch (Exception e) { - throw new Error(t.getMessage()); + throw new Error(e.getMessage()); } diff --git a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitions.java b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitions.java index 5aacc31..e8768f5 100644 --- a/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitions.java +++ b/nemo-impl/src/main/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitions.java @@ -228,10 +228,10 @@ public class GetDefinitions { conditionparadefinitionFuture.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } catch (ExecutionException e){ // TODO Auto-generated catch block - e.printStackTrace(); + LOG.error("Exception:",e); } return ; } diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliBuilder.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliBuilder.java index b2f4957..b8ddab0 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliBuilder.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliBuilder.java @@ -815,7 +815,8 @@ public class CliBuilder implements AutoCloseable { try { Thread.sleep(3000); } catch (InterruptedException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } } } diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliTrigger.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliTrigger.java index 294ecd7..1d745e8 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliTrigger.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/CliTrigger.java @@ -100,7 +100,8 @@ public class CliTrigger implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, userIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()){ LOG.info("getUser OK"); @@ -133,7 +134,8 @@ public class CliTrigger implements AutoCloseable { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, virtualNetworkIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.info("getVirtualNetwork OK"); @@ -161,7 +163,8 @@ public class CliTrigger implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, userIntentVnMappingIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.info("getUserIntentVnMapping OK"); @@ -187,7 +190,8 @@ public class CliTrigger implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, physicalNetworkIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.info("getPhysicalNetwork OK"); diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/TelnetUtils.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/TelnetUtils.java index 94eda81..1929580 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/TelnetUtils.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/TelnetUtils.java @@ -246,7 +246,9 @@ public class TelnetUtils implements AutoCloseable { @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { - e.getCause().printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e.getCause()); + // e.getCause().printStackTrace(); e.getChannel().close(); } } @@ -334,7 +336,9 @@ public class TelnetUtils implements AutoCloseable { @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { - e.getCause().printStackTrace(); + // e.getCause().printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e.getCause()); e.getChannel().close(); } } diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/DataBrokerAdapter.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/DataBrokerAdapter.java index 5c7a51b..3bdce05 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/DataBrokerAdapter.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/DataBrokerAdapter.java @@ -88,7 +88,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } @@ -114,7 +115,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } return addResult[0]; } @@ -140,7 +142,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } return removeResult[0]; } @@ -166,7 +169,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } protected void removePhysicalLink(final PhysicalLinkKey physicalLinkKey){ @@ -190,7 +194,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/PhysicalResourceLoader.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/PhysicalResourceLoader.java index 89ad3f2..de68309 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/PhysicalResourceLoader.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/PhysicalResourceLoader.java @@ -1 +1 @@ -/* * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.nemo.renderer.cli.physicalnetwork; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class PhysicalResourceLoader { private static final Logger log = LoggerFactory.getLogger(PhysicalResourceLoader.class); public final static String NODE_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-node-resource.json"; public final static String LINK_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-link-resource.json"; public final static String EXTERNAL_NETWORK_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-external-resource.json"; public final static String HOST_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-host-resource.json"; private final static String NODES = "node"; private final static String NODE_ID = "node-id"; private final static String NODE_TYPE = "node-type"; private final static String PORTS = "port"; private final static String PORT_ID = "port-id"; private final static String PORT_TYPE = "port-type"; private final static String PORT_IP = "port-ip-address"; private final static String PORT_MAC = "port-mac-address"; private final static String PORT_MASK = "mask"; private final static String PORT_BANDWIDTH = "bandwidth"; private final static String LINKS = "link"; private final static String LINK_ID = "link-id"; private final static String SRC_NODE_ID = "left-node-id"; private final static String SRC_PORT_ID = "left-port-id"; private final static String DST_NODE_ID = "right-node-id"; private final static String DST_PORT_ID = "right-port-id"; private final static String BANDWIDTH = "link-bandwidth"; private final static String METRIC = "metric"; private final static String DELAY = "delay"; private final static String LOSS_RATE = "loss-rate"; private final static String HOSTS = "host"; private final static String HOST_NAME = "name"; private final static String HOST_ID = "id"; private final static String HOST_IPS = "ip-addresses"; private final static String CONNECTOR_ID = "connector-id"; private final static String IP_ADDRESS = "ip-address"; private final static String MAC_ADDRESS = "mac-address"; private final static String EXTERNAL_NETWORK = "external-network"; private final static String ACCESS_NODE_ID = "access-node-id"; private final static String ACCESS_PORT_ID = "access-port-id"; private final static String LAYER = "layer"; private final static String PROTOCOL = "protocol"; private final static String ATTRIBUTES = "attribute"; private final static String ATTRIBUTE_NAME = "name"; private final static String ATTRIBUTE_VALUE = "value"; protected final static long DEFAULT_PORT_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_DELAY = 1; protected final static short DEFAULT_LINK_LOSS_RATE = 1; private HashMap physicalNodeHashMap; private HashMap physicalPortHashMap; private HashMap physicalLinkHashMap; private ObjectMapper objectMapper = null; private DataBrokerAdapter dataBrokerAdapter = null; public PhysicalResourceLoader(DataBroker dataBroker) { physicalNodeHashMap = new HashMap(); physicalPortHashMap = new HashMap(); physicalLinkHashMap = new HashMap(); this.objectMapper = new ObjectMapper(); this.dataBrokerAdapter = new DataBrokerAdapter(dataBroker); loadNodesPorts(); loadLinks(); loadHosts(); //loadExternals(); } protected PhysicalNode getPhysicalNode(PhysicalNodeId physicalNodeId){ return physicalNodeHashMap.get(physicalNodeId); } protected PhysicalPort getPhysicalPort(PhysicalPortId physicalPortId){ return physicalPortHashMap.get(physicalPortId); } protected PhysicalLink getPhysicalLink(PhysicalLinkId physicalLinkId){ return physicalLinkHashMap.get(physicalLinkId); } private void loadNodesPorts() { String nodesStr = Utils.readFile(NODE_PROPERTY); try { JsonNode nodeRoot = objectMapper.readTree(nodesStr); List physicalNodes = buildNodes(nodeRoot); for (PhysicalNode physicalNode : physicalNodes) { physicalNodeHashMap.put(physicalNode.getNodeId(), physicalNode); for (PhysicalPort physicalPort : physicalNode.getPhysicalPort()) { physicalPortHashMap.put(physicalPort.getPortId(), physicalPort); } //lsx dataBrokerAdapter.addPhysicalNode(physicalNode); } } catch (IOException e) { e.printStackTrace(); } } private void loadLinks() { String linkStr = Utils.readFile(LINK_PROPERTY); try { JsonNode linkRoot = objectMapper.readTree(linkStr); List physicalLinks = buildLinks(linkRoot); for (PhysicalLink physicalLink : physicalLinks) { physicalLinkHashMap.put(physicalLink.getLinkId(), physicalLink); //lsx dataBrokerAdapter.addPhysicalLink(physicalLink); } } catch (IOException e) { e.printStackTrace(); } } /* private void loadExternals() { String externalStr = Utils.readFile(EXTERNAL_NETWORK_MAC_PROPERTY); try { JsonNode externalRoot = objectMapper.readTree(externalStr); buildExternals(externalRoot); } catch (IOException e) { e.printStackTrace(); } } */ private void loadHosts() { String hostStr = Utils.readFile(HOST_PROPERTY); try { JsonNode hostRoot = objectMapper.readTree(hostStr); List physicalHosts = buildHosts(hostRoot); for (PhysicalHost physicalHost : physicalHosts) { dataBrokerAdapter.addPhysicalHost(physicalHost); } } catch (IOException e) { e.printStackTrace(); } } /* private void buildExternals(JsonNode externalRoot) { JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC); log.debug("Build external network mac : {} .", exNetworkNodes); for (int i = 0; i < exNetworkNodes.size(); i++) { log.debug("Build external network execution body"); JsonNode exNetworkNode = exNetworkNodes.get(i); buildExNetwork(exNetworkNode); } } private void buildExNetwork(JsonNode exNetwork){ String nodeId = exNetwork.get(NODE_ID).asText(); String portId = exNetwork.get(PORT_ID).asText(); String peerMac = exNetwork.get(MAC_ADDRESS).asText(); PhysicalNodeId accessNodeId = new PhysicalNodeId(nodeId); PhysicalPortId accessPortId = new PhysicalPortId(portId); MacAddress macAddress = new MacAddress(peerMac); externalNetworkMac.put(accessNodeId,accessPortId,macAddress); } */ private List buildNodes(JsonNode nodesRoot) { List physicalNodes = new ArrayList(); JsonNode nodes = nodesRoot.path(NODES); log.debug("Build nodes: {} .", nodes); for (int i = 0; i < nodes.size(); i++) { log.debug("build physical node execution body"); JsonNode node = nodes.get(i); PhysicalNode phyNode = buildNode(node); if (phyNode != null) { physicalNodes.add(phyNode); } } return physicalNodes; } private PhysicalNode buildNode(JsonNode node) { PhysicalNodeBuilder nodeBuilder = new PhysicalNodeBuilder(); String node_id = node.get(NODE_ID).asText(); if (node_id.equals("")) return null; nodeBuilder.setNodeId(new PhysicalNodeId(node_id)); PhysicalNodeKey key = new PhysicalNodeKey(nodeBuilder.getNodeId()); nodeBuilder.setKey(key); String strType = node.get(NODE_TYPE).asText(); PhysicalNodeInstance.NodeType nodeType = Utils.getNodeType(strType); nodeBuilder.setNodeType(nodeType); JsonNode ports = node.path(PORTS); List phyPortList = buildPorts(ports); nodeBuilder.setPhysicalPort(phyPortList); JsonNode attributes = node.path(ATTRIBUTES); nodeBuilder.setAttribute(buildNodeAttributes(attributes)); return nodeBuilder.build(); } private List buildPorts(JsonNode ports) { List phyPortList = new ArrayList(); for (int j = 0; j < ports.size(); j++) { //JsonNode port = portIt.next(); JsonNode port = ports.get(j); PhysicalPort physicalPort = buildPort(port); if (physicalPort != null) { phyPortList.add(physicalPort); } } return phyPortList; } private PhysicalPort buildPort(JsonNode port) { PhysicalPortBuilder physicalPortBuilder = new PhysicalPortBuilder(); String strPortId = port.get(PORT_ID).asText(); physicalPortBuilder.setPortId(new PhysicalPortId(strPortId)); String strType = port.get(PORT_TYPE).asText(); PhysicalPortInstance.PortType portType = Utils.getPortType(strType); physicalPortBuilder.setPortType(portType); //lsx port mac if(!(port.get("port-mac-address").asText().equals(""))){ MacAddress mac = new MacAddress(port.get("port-mac-address").asText()); physicalPortBuilder.setMacAddress(mac); } //lsx port bandwidth if(port.get("bandwidth").asText().equals("")) { physicalPortBuilder.setBandwidth(DEFAULT_PORT_BANDWIDTH); } else{ physicalPortBuilder.setBandwidth((long) (port.get("bandwidth").asInt())); } JsonNode portAttributes = port.path(ATTRIBUTES); List attributes = buildPortAttributes(portAttributes); physicalPortBuilder.setAttribute(attributes); return physicalPortBuilder.build(); } private List buildPortAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); Attribute attribute = buildPortAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private Attribute buildPortAttribute(JsonNode attribute) { AttributeBuilder attributeBuilder = new AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildNodeAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute attribute = buildNodeAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute buildNodeAttribute(JsonNode attribute) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder attributeBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildLinks(JsonNode linksRoot) { List physicalLinks = new ArrayList(); JsonNode links = linksRoot.path(LINKS); log.debug("Build links: {} .", links); for (int i = 0; i < links.size(); i++) { log.debug("build physical node execution body"); JsonNode link = links.get(i); PhysicalLink phyLink = buildLink(link); if (phyLink != null) { physicalLinks.add(phyLink); } } return physicalLinks; } private PhysicalLink buildLink(JsonNode linkNode) { PhysicalLinkBuilder linkBuilder = new PhysicalLinkBuilder(); String strLinkId = linkNode.get(LINK_ID).asText(); linkBuilder.setLinkId(new PhysicalLinkId(strLinkId)); //lsx linkBuilder.setSrcNodeId(new PhysicalNodeId(linkNode.get(SRC_NODE_ID).asText())); linkBuilder.setSrcPortId(new PhysicalPortId(linkNode.get(SRC_PORT_ID).asText())); linkBuilder.setDestNodeId(new PhysicalNodeId(linkNode.get(DST_NODE_ID).asText())); linkBuilder.setDestPortId(new PhysicalPortId(linkNode.get(DST_PORT_ID).asText())); //lsx if(linkNode.get("link-bandwidth").asText().equals("")) { linkBuilder.setBandwidth(DEFAULT_LINK_BANDWIDTH); }else{ linkBuilder.setBandwidth((long) (linkNode.get("link-bandwidth").asLong())); } long metric = linkNode.get(METRIC).asLong(); linkBuilder.setMetric(metric); linkBuilder.setDelay(DEFAULT_LINK_DELAY); linkBuilder.setLossRate(DEFAULT_LINK_LOSS_RATE); return linkBuilder.build(); } private List buildHosts(JsonNode hostsNode) { List physicalHosts = new ArrayList(); JsonNode hosts = hostsNode.path(HOSTS); log.debug("Build hosts: {} .", hosts); for (int i = 0; i < hosts.size(); i++) { JsonNode host = hosts.get(i); PhysicalHost physicalHost = buildHost(host); if (physicalHost != null) { physicalHosts.add(physicalHost); } } return physicalHosts; } private PhysicalHost buildHost(JsonNode hostNode) { PhysicalHostBuilder hostBuilder = new PhysicalHostBuilder(); hostBuilder.setHostId(new PhysicalHostId(hostNode.get(HOST_ID).asText())); hostBuilder.setKey(new PhysicalHostKey(hostBuilder.getHostId())); hostBuilder.setHostName(new PhysicalHostName(hostNode.get(HOST_NAME).asText())); IpAddressesBuilder IpAddrBuilder = new IpAddressesBuilder(); List ipList = new ArrayList(); JsonNode ipaddrs = hostNode.path(HOST_IPS); for (int p = 0; p < ipaddrs.size(); p++) { JsonNode ipaddr = ipaddrs.get(p); IpAddress ip = new IpAddress(new Ipv4Address(ipaddr.get(IP_ADDRESS).asText())); ipList.add(ip); } IpAddrBuilder.setIpAddress(ipList); hostBuilder.setIpAddresses(IpAddrBuilder.build()); MacAddress mac = new MacAddress(hostNode.get(MAC_ADDRESS).asText()); hostBuilder.setMacAddress(mac); hostBuilder.setNodeId(new PhysicalNodeId(hostNode.get(NODE_ID).asText())); hostBuilder.setPortId(new PhysicalPortId(hostNode.get(CONNECTOR_ID).asText())); return hostBuilder.build(); } public void close() { physicalLinkHashMap.clear(); physicalPortHashMap.clear(); physicalNodeHashMap.clear(); //externalNetworkMac.clear(); } } \ No newline at end of file +/* * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.nemo.renderer.cli.physicalnetwork; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class PhysicalResourceLoader { private static final Logger log = LoggerFactory.getLogger(PhysicalResourceLoader.class); public final static String NODE_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-node-resource.json"; public final static String LINK_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-link-resource.json"; public final static String EXTERNAL_NETWORK_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-external-resource.json"; public final static String HOST_PROPERTY = "etc/opendaylight/karaf/nemo-cli-renderer-host-resource.json"; private final static String NODES = "node"; private final static String NODE_ID = "node-id"; private final static String NODE_TYPE = "node-type"; private final static String PORTS = "port"; private final static String PORT_ID = "port-id"; private final static String PORT_TYPE = "port-type"; private final static String PORT_IP = "port-ip-address"; private final static String PORT_MAC = "port-mac-address"; private final static String PORT_MASK = "mask"; private final static String PORT_BANDWIDTH = "bandwidth"; private final static String LINKS = "link"; private final static String LINK_ID = "link-id"; private final static String SRC_NODE_ID = "left-node-id"; private final static String SRC_PORT_ID = "left-port-id"; private final static String DST_NODE_ID = "right-node-id"; private final static String DST_PORT_ID = "right-port-id"; private final static String BANDWIDTH = "link-bandwidth"; private final static String METRIC = "metric"; private final static String DELAY = "delay"; private final static String LOSS_RATE = "loss-rate"; private final static String HOSTS = "host"; private final static String HOST_NAME = "name"; private final static String HOST_ID = "id"; private final static String HOST_IPS = "ip-addresses"; private final static String CONNECTOR_ID = "connector-id"; private final static String IP_ADDRESS = "ip-address"; private final static String MAC_ADDRESS = "mac-address"; private final static String EXTERNAL_NETWORK = "external-network"; private final static String ACCESS_NODE_ID = "access-node-id"; private final static String ACCESS_PORT_ID = "access-port-id"; private final static String LAYER = "layer"; private final static String PROTOCOL = "protocol"; private final static String ATTRIBUTES = "attribute"; private final static String ATTRIBUTE_NAME = "name"; private final static String ATTRIBUTE_VALUE = "value"; protected final static long DEFAULT_PORT_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_DELAY = 1; protected final static short DEFAULT_LINK_LOSS_RATE = 1; private HashMap physicalNodeHashMap; private HashMap physicalPortHashMap; private HashMap physicalLinkHashMap; private ObjectMapper objectMapper = null; private DataBrokerAdapter dataBrokerAdapter = null; public PhysicalResourceLoader(DataBroker dataBroker) { physicalNodeHashMap = new HashMap(); physicalPortHashMap = new HashMap(); physicalLinkHashMap = new HashMap(); this.objectMapper = new ObjectMapper(); this.dataBrokerAdapter = new DataBrokerAdapter(dataBroker); loadNodesPorts(); loadLinks(); loadHosts(); //loadExternals(); } protected PhysicalNode getPhysicalNode(PhysicalNodeId physicalNodeId){ return physicalNodeHashMap.get(physicalNodeId); } protected PhysicalPort getPhysicalPort(PhysicalPortId physicalPortId){ return physicalPortHashMap.get(physicalPortId); } protected PhysicalLink getPhysicalLink(PhysicalLinkId physicalLinkId){ return physicalLinkHashMap.get(physicalLinkId); } private void loadNodesPorts() { String nodesStr = Utils.readFile(NODE_PROPERTY); try { JsonNode nodeRoot = objectMapper.readTree(nodesStr); List physicalNodes = buildNodes(nodeRoot); for (PhysicalNode physicalNode : physicalNodes) { physicalNodeHashMap.put(physicalNode.getNodeId(), physicalNode); for (PhysicalPort physicalPort : physicalNode.getPhysicalPort()) { physicalPortHashMap.put(physicalPort.getPortId(), physicalPort); } //lsx dataBrokerAdapter.addPhysicalNode(physicalNode); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception:",e); } } private void loadLinks() { String linkStr = Utils.readFile(LINK_PROPERTY); try { JsonNode linkRoot = objectMapper.readTree(linkStr); List physicalLinks = buildLinks(linkRoot); for (PhysicalLink physicalLink : physicalLinks) { physicalLinkHashMap.put(physicalLink.getLinkId(), physicalLink); //lsx dataBrokerAdapter.addPhysicalLink(physicalLink); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception:",e); } } /* private void loadExternals() { String externalStr = Utils.readFile(EXTERNAL_NETWORK_MAC_PROPERTY); try { JsonNode externalRoot = objectMapper.readTree(externalStr); buildExternals(externalRoot); } catch (IOException e) { e.printStackTrace(); } } */ private void loadHosts() { String hostStr = Utils.readFile(HOST_PROPERTY); try { JsonNode hostRoot = objectMapper.readTree(hostStr); List physicalHosts = buildHosts(hostRoot); for (PhysicalHost physicalHost : physicalHosts) { dataBrokerAdapter.addPhysicalHost(physicalHost); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception:",e); } } /* private void buildExternals(JsonNode externalRoot) { JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC); log.debug("Build external network mac : {} .", exNetworkNodes); for (int i = 0; i < exNetworkNodes.size(); i++) { log.debug("Build external network execution body"); JsonNode exNetworkNode = exNetworkNodes.get(i); buildExNetwork(exNetworkNode); } } private void buildExNetwork(JsonNode exNetwork){ String nodeId = exNetwork.get(NODE_ID).asText(); String portId = exNetwork.get(PORT_ID).asText(); String peerMac = exNetwork.get(MAC_ADDRESS).asText(); PhysicalNodeId accessNodeId = new PhysicalNodeId(nodeId); PhysicalPortId accessPortId = new PhysicalPortId(portId); MacAddress macAddress = new MacAddress(peerMac); externalNetworkMac.put(accessNodeId,accessPortId,macAddress); } */ private List buildNodes(JsonNode nodesRoot) { List physicalNodes = new ArrayList(); JsonNode nodes = nodesRoot.path(NODES); log.debug("Build nodes: {} .", nodes); for (int i = 0; i < nodes.size(); i++) { log.debug("build physical node execution body"); JsonNode node = nodes.get(i); PhysicalNode phyNode = buildNode(node); if (phyNode != null) { physicalNodes.add(phyNode); } } return physicalNodes; } private PhysicalNode buildNode(JsonNode node) { PhysicalNodeBuilder nodeBuilder = new PhysicalNodeBuilder(); String node_id = node.get(NODE_ID).asText(); if (node_id.equals("")) return null; nodeBuilder.setNodeId(new PhysicalNodeId(node_id)); PhysicalNodeKey key = new PhysicalNodeKey(nodeBuilder.getNodeId()); nodeBuilder.setKey(key); String strType = node.get(NODE_TYPE).asText(); PhysicalNodeInstance.NodeType nodeType = Utils.getNodeType(strType); nodeBuilder.setNodeType(nodeType); JsonNode ports = node.path(PORTS); List phyPortList = buildPorts(ports); nodeBuilder.setPhysicalPort(phyPortList); JsonNode attributes = node.path(ATTRIBUTES); nodeBuilder.setAttribute(buildNodeAttributes(attributes)); return nodeBuilder.build(); } private List buildPorts(JsonNode ports) { List phyPortList = new ArrayList(); for (int j = 0; j < ports.size(); j++) { //JsonNode port = portIt.next(); JsonNode port = ports.get(j); PhysicalPort physicalPort = buildPort(port); if (physicalPort != null) { phyPortList.add(physicalPort); } } return phyPortList; } private PhysicalPort buildPort(JsonNode port) { PhysicalPortBuilder physicalPortBuilder = new PhysicalPortBuilder(); String strPortId = port.get(PORT_ID).asText(); physicalPortBuilder.setPortId(new PhysicalPortId(strPortId)); String strType = port.get(PORT_TYPE).asText(); PhysicalPortInstance.PortType portType = Utils.getPortType(strType); physicalPortBuilder.setPortType(portType); //lsx port mac if(!(port.get("port-mac-address").asText().equals(""))){ MacAddress mac = new MacAddress(port.get("port-mac-address").asText()); physicalPortBuilder.setMacAddress(mac); } //lsx port bandwidth if(port.get("bandwidth").asText().equals("")) { physicalPortBuilder.setBandwidth(DEFAULT_PORT_BANDWIDTH); } else{ physicalPortBuilder.setBandwidth((long) (port.get("bandwidth").asInt())); } JsonNode portAttributes = port.path(ATTRIBUTES); List attributes = buildPortAttributes(portAttributes); physicalPortBuilder.setAttribute(attributes); return physicalPortBuilder.build(); } private List buildPortAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); Attribute attribute = buildPortAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private Attribute buildPortAttribute(JsonNode attribute) { AttributeBuilder attributeBuilder = new AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildNodeAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute attribute = buildNodeAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute buildNodeAttribute(JsonNode attribute) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder attributeBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildLinks(JsonNode linksRoot) { List physicalLinks = new ArrayList(); JsonNode links = linksRoot.path(LINKS); log.debug("Build links: {} .", links); for (int i = 0; i < links.size(); i++) { log.debug("build physical node execution body"); JsonNode link = links.get(i); PhysicalLink phyLink = buildLink(link); if (phyLink != null) { physicalLinks.add(phyLink); } } return physicalLinks; } private PhysicalLink buildLink(JsonNode linkNode) { PhysicalLinkBuilder linkBuilder = new PhysicalLinkBuilder(); String strLinkId = linkNode.get(LINK_ID).asText(); linkBuilder.setLinkId(new PhysicalLinkId(strLinkId)); //lsx linkBuilder.setSrcNodeId(new PhysicalNodeId(linkNode.get(SRC_NODE_ID).asText())); linkBuilder.setSrcPortId(new PhysicalPortId(linkNode.get(SRC_PORT_ID).asText())); linkBuilder.setDestNodeId(new PhysicalNodeId(linkNode.get(DST_NODE_ID).asText())); linkBuilder.setDestPortId(new PhysicalPortId(linkNode.get(DST_PORT_ID).asText())); //lsx if(linkNode.get("link-bandwidth").asText().equals("")) { linkBuilder.setBandwidth(DEFAULT_LINK_BANDWIDTH); }else{ linkBuilder.setBandwidth((long) (linkNode.get("link-bandwidth").asLong())); } long metric = linkNode.get(METRIC).asLong(); linkBuilder.setMetric(metric); linkBuilder.setDelay(DEFAULT_LINK_DELAY); linkBuilder.setLossRate(DEFAULT_LINK_LOSS_RATE); return linkBuilder.build(); } private List buildHosts(JsonNode hostsNode) { List physicalHosts = new ArrayList(); JsonNode hosts = hostsNode.path(HOSTS); log.debug("Build hosts: {} .", hosts); for (int i = 0; i < hosts.size(); i++) { JsonNode host = hosts.get(i); PhysicalHost physicalHost = buildHost(host); if (physicalHost != null) { physicalHosts.add(physicalHost); } } return physicalHosts; } private PhysicalHost buildHost(JsonNode hostNode) { PhysicalHostBuilder hostBuilder = new PhysicalHostBuilder(); hostBuilder.setHostId(new PhysicalHostId(hostNode.get(HOST_ID).asText())); hostBuilder.setKey(new PhysicalHostKey(hostBuilder.getHostId())); hostBuilder.setHostName(new PhysicalHostName(hostNode.get(HOST_NAME).asText())); IpAddressesBuilder IpAddrBuilder = new IpAddressesBuilder(); List ipList = new ArrayList(); JsonNode ipaddrs = hostNode.path(HOST_IPS); for (int p = 0; p < ipaddrs.size(); p++) { JsonNode ipaddr = ipaddrs.get(p); IpAddress ip = new IpAddress(new Ipv4Address(ipaddr.get(IP_ADDRESS).asText())); ipList.add(ip); } IpAddrBuilder.setIpAddress(ipList); hostBuilder.setIpAddresses(IpAddrBuilder.build()); MacAddress mac = new MacAddress(hostNode.get(MAC_ADDRESS).asText()); hostBuilder.setMacAddress(mac); hostBuilder.setNodeId(new PhysicalNodeId(hostNode.get(NODE_ID).asText())); hostBuilder.setPortId(new PhysicalPortId(hostNode.get(CONNECTOR_ID).asText())); return hostBuilder.build(); } public void close() { physicalLinkHashMap.clear(); physicalPortHashMap.clear(); physicalNodeHashMap.clear(); //externalNetworkMac.clear(); } } \ No newline at end of file diff --git a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/Utils.java b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/Utils.java index 8708fad..9dfd1c9 100644 --- a/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/Utils.java +++ b/nemo-renderers/cli-renderer/src/main/java/org/opendaylight/nemo/renderer/cli/physicalnetwork/Utils.java @@ -54,13 +54,13 @@ public class Utils { } reader.close(); }catch(IOException e){ - e.printStackTrace(); + // TODO Auto-generated catch block }finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { - e.printStackTrace(); + // TODO Auto-generated catch block } } } diff --git a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/FlowTableManager.java b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/FlowTableManager.java index 21e913b..263ae07 100644 --- a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/FlowTableManager.java +++ b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/FlowTableManager.java @@ -98,7 +98,8 @@ public class FlowTableManager implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, userIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()){ LOG.debug("getUser OK"); @@ -127,7 +128,8 @@ public class FlowTableManager implements AutoCloseable { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, virtualNetworkIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.debug("getVirtualNetwork OK"); @@ -150,7 +152,8 @@ public class FlowTableManager implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.CONFIGURATION, userIntentVnMappingIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.debug("getUserIntentVnMapping OK"); @@ -172,7 +175,8 @@ public class FlowTableManager implements AutoCloseable { try { result = readOnlyTransaction.read(LogicalDatastoreType.OPERATIONAL, physicalNetworkIid).get(); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + LOG.error("Exception:",e); } if (result.isPresent()) { LOG.debug("getPhysicalNetwork OK"); diff --git a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/DataBrokerAdapter.java b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/DataBrokerAdapter.java index 4885762..3495d3a 100644 --- a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/DataBrokerAdapter.java +++ b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/DataBrokerAdapter.java @@ -88,7 +88,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } @@ -114,7 +115,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } return addResult[0]; } @@ -140,7 +142,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } return removeResult[0]; } @@ -166,7 +169,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } protected void removePhysicalLink(final PhysicalLinkKey physicalLinkKey){ @@ -190,7 +194,8 @@ public class DataBrokerAdapter { }); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } diff --git a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhyConfigLoader.java b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhyConfigLoader.java index 1f365fd..dc5cd7e 100644 --- a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhyConfigLoader.java +++ b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhyConfigLoader.java @@ -1 +1 @@ -/* * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.nemo.renderer.openflow.physicalnetwork; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class PhyConfigLoader { private static final Logger log = LoggerFactory.getLogger(PhyConfigLoader.class); public final static String NODE_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-node-resource.json"; public final static String LINK_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-link-resource.json"; public final static String EXTERNAL_NETWORK_MAC_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-external-resource.json"; public final static String HOST_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-host-resource.json"; private final static String NODES = "node"; private final static String NODE_ID = "node-id"; private final static String NODE_TYPE = "node-type"; private final static String PORTS = "port"; private final static String PORT_ID = "port-id"; private final static String PORT_TYPE = "port-type"; private final static String PORT_IP = "port-ip-address"; private final static String PORT_MAC = "port-mac-address"; private final static String LINKS = "link"; private final static String LINK_ID = "link-id"; private final static String SRC_NODE_ID = "left-node-id"; private final static String SRC_PORT_ID = "left-port-id"; private final static String DST_NODE_ID = "right-node-id"; private final static String DST_PORT_ID = "right-port-id"; private final static String BANDWIDTH = "link-bandwidth"; private final static String METRIC = "metric"; private final static String HOSTS = "host"; private final static String HOST_NAME = "name"; private final static String HOST_ID = "id"; private final static String HOST_IPS = "ip-addresses"; private final static String CONNECTOR_ID = "connector-id"; private final static String EXTERNAL_NETWORK_MAC = "external-network-mac"; private final static String IP_ADDRESS = "ip-address"; private final static String MAC_ADDRESS = "mac-address"; private final static String ATTRIBUTES = "attribute"; private final static String ATTRIBUTE_NAME = "name"; private final static String ATTRIBUTE_VALUE = "value"; protected final static long DEFAULT_LINK_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_DELAY = 1; protected final static short DEFAULT_LINK_LOSS_RATE = 1; private HashMap physicalNodeHashMap; private HashMap physicalPortHashMap; private HashMap physicalLinkHashMap; private Table externalNetworkMac; // final private DataBroker dataProvider; private ObjectMapper objectMapper = null; private DataBrokerAdapter dataBrokerAdapter = null; public PhyConfigLoader(DataBroker dataBroker) { physicalNodeHashMap = new HashMap(); physicalPortHashMap = new HashMap(); physicalLinkHashMap = new HashMap(); externalNetworkMac = HashBasedTable.create(); this.objectMapper = new ObjectMapper(); this.dataBrokerAdapter = new DataBrokerAdapter(dataBroker); loadNodesPorts(); loadLinks(); loadHosts(); loadExternals(); } public Table getExternalNetworkMac() { return externalNetworkMac; } protected PhysicalNode getPhysicalNode(PhysicalNodeId physicalNodeId){ return physicalNodeHashMap.get(physicalNodeId); } protected PhysicalPort getPhysicalPort(PhysicalPortId physicalPortId){ return physicalPortHashMap.get(physicalPortId); } protected PhysicalLink getPhysicalLink(PhysicalLinkId physicalLinkId){ return physicalLinkHashMap.get(physicalLinkId); } private void loadNodesPorts() { String nodesStr = Utils.readFile(NODE_PROPERTY); try { JsonNode nodeRoot = objectMapper.readTree(nodesStr); List physicalNodes = buildNodes(nodeRoot); for (PhysicalNode physicalNode : physicalNodes) { physicalNodeHashMap.put(physicalNode.getNodeId(), physicalNode); for (PhysicalPort physicalPort : physicalNode.getPhysicalPort()) { physicalPortHashMap.put(physicalPort.getPortId(), physicalPort); } } } catch (IOException e) { e.printStackTrace(); } } private void loadLinks() { String linkStr = Utils.readFile(LINK_PROPERTY); try { JsonNode linkRoot = objectMapper.readTree(linkStr); List physicalLinks = buildLinks(linkRoot); for (PhysicalLink physicalLink : physicalLinks) { physicalLinkHashMap.put(physicalLink.getLinkId(), physicalLink); } } catch (IOException e) { e.printStackTrace(); } } private void loadExternals() { String externalStr = Utils.readFile(EXTERNAL_NETWORK_MAC_PROPERTY); try { JsonNode externalRoot = objectMapper.readTree(externalStr); buildExternals(externalRoot); } catch (IOException e) { e.printStackTrace(); } } private void loadHosts() { String hostStr = Utils.readFile(HOST_PROPERTY); try { JsonNode hostRoot = objectMapper.readTree(hostStr); List physicalHosts = buildHosts(hostRoot); for (PhysicalHost physicalHost : physicalHosts) { dataBrokerAdapter.addPhysicalHost(physicalHost); } } catch (IOException e) { e.printStackTrace(); } } private void buildExternals(JsonNode externalRoot) { JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC); log.debug("Build external network mac : {} .", exNetworkNodes); for (int i = 0; i < exNetworkNodes.size(); i++) { log.debug("Build external network execution body"); JsonNode exNetworkNode = exNetworkNodes.get(i); buildExNetwork(exNetworkNode); } } private void buildExNetwork(JsonNode exNetwork){ String nodeId = exNetwork.get(NODE_ID).asText(); String portId = exNetwork.get(PORT_ID).asText(); String peerMac = exNetwork.get(MAC_ADDRESS).asText(); PhysicalNodeId accessNodeId = new PhysicalNodeId(nodeId); PhysicalPortId accessPortId = new PhysicalPortId(portId); MacAddress macAddress = new MacAddress(peerMac); externalNetworkMac.put(accessNodeId,accessPortId,macAddress); } private List buildNodes(JsonNode nodesRoot) { List physicalNodes = new ArrayList(); JsonNode nodes = nodesRoot.path(NODES); log.debug("Build nodes: {} .", nodes); for (int i = 0; i < nodes.size(); i++) { log.debug("build physical node execution body"); JsonNode node = nodes.get(i); PhysicalNode phyNode = buildNode(node); if (phyNode != null) { physicalNodes.add(phyNode); } } return physicalNodes; } private PhysicalNode buildNode(JsonNode node) { PhysicalNodeBuilder nodeBuilder = new PhysicalNodeBuilder(); String node_id = node.get(NODE_ID).asText(); if (node_id.equals("")) return null; nodeBuilder.setNodeId(new PhysicalNodeId(node_id)); PhysicalNodeKey key = new PhysicalNodeKey(nodeBuilder.getNodeId()); nodeBuilder.setKey(key); String strType = node.get(NODE_TYPE).asText(); PhysicalNodeInstance.NodeType nodeType = Utils.getNodeType(strType); nodeBuilder.setNodeType(nodeType); JsonNode ports = node.path(PORTS); List phyPortList = buildPorts(ports); nodeBuilder.setPhysicalPort(phyPortList); JsonNode attributes = node.path(ATTRIBUTES); nodeBuilder.setAttribute(buildNodeAttributes(attributes)); return nodeBuilder.build(); } private List buildPorts(JsonNode ports) { List phyPortList = new ArrayList(); for (int j = 0; j < ports.size(); j++) { //JsonNode port = portIt.next(); JsonNode port = ports.get(j); PhysicalPort physicalPort = buildPort(port); if (physicalPort != null) { phyPortList.add(physicalPort); } } return phyPortList; } private PhysicalPort buildPort(JsonNode port) { PhysicalPortBuilder physicalPortBuilder = new PhysicalPortBuilder(); String strPortId = port.get(PORT_ID).asText(); physicalPortBuilder.setPortId(new PhysicalPortId(strPortId)); String strType = port.get(PORT_TYPE).asText(); PhysicalPortInstance.PortType portType = Utils.getPortType(strType); physicalPortBuilder.setPortType(portType); // String strMac = port.get(PORT_MAC).asText(); // MacAddress macAddress = new MacAddress(strMac); // physicalPortBuilder.setMacAddress(macAddress); // // long bandWidth = port.get(BANDWIDTH).asLong(); // physicalPortBuilder.setBandwidth(bandWidth); JsonNode portAttributes = port.path(ATTRIBUTES); List attributes = buildPortAttributes(portAttributes); physicalPortBuilder.setAttribute(attributes); return physicalPortBuilder.build(); } private List buildPortAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); Attribute attribute = buildPortAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private Attribute buildPortAttribute(JsonNode attribute) { AttributeBuilder attributeBuilder = new AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildNodeAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute attribute = buildNodeAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute buildNodeAttribute(JsonNode attribute) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder attributeBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildLinks(JsonNode linksRoot) { List physicalLinks = new ArrayList(); JsonNode links = linksRoot.path(LINKS); log.debug("Build links: {} .", links); for (int i = 0; i < links.size(); i++) { log.debug("build physical node execution body"); JsonNode link = links.get(i); PhysicalLink phyLink = buildLink(link); if (phyLink != null) { physicalLinks.add(phyLink); } } return physicalLinks; } private PhysicalLink buildLink(JsonNode linkNode) { PhysicalLinkBuilder linkBuilder = new PhysicalLinkBuilder(); String strLinkId = linkNode.get(LINK_ID).asText(); long metric = linkNode.get(METRIC).asLong(); linkBuilder.setLinkId(new PhysicalLinkId(strLinkId)); linkBuilder.setMetric(metric); linkBuilder.setBandwidth(DEFAULT_LINK_BANDWIDTH); linkBuilder.setDelay(DEFAULT_LINK_DELAY); linkBuilder.setLossRate(DEFAULT_LINK_LOSS_RATE); return linkBuilder.build(); } private List buildHosts(JsonNode hostsNode) { List physicalHosts = new ArrayList(); JsonNode hosts = hostsNode.path(HOSTS); log.debug("Build hosts: {} .", hosts); for (int i = 0; i < hosts.size(); i++) { JsonNode host = hosts.get(i); PhysicalHost physicalHost = buildHost(host); if (physicalHost != null) { physicalHosts.add(physicalHost); } } return physicalHosts; } private PhysicalHost buildHost(JsonNode hostNode) { PhysicalHostBuilder hostBuilder = new PhysicalHostBuilder(); hostBuilder.setHostId(new PhysicalHostId(hostNode.get(HOST_ID).asText())); hostBuilder.setKey(new PhysicalHostKey(hostBuilder.getHostId())); hostBuilder.setHostName(new PhysicalHostName(hostNode.get(HOST_NAME).asText())); IpAddressesBuilder IpAddrBuilder = new IpAddressesBuilder(); List ipList = new ArrayList(); JsonNode ipaddrs = hostNode.path(HOST_IPS); for (int p = 0; p < ipaddrs.size(); p++) { JsonNode ipaddr = ipaddrs.get(p); IpAddress ip = new IpAddress(new Ipv4Address(ipaddr.get(IP_ADDRESS).asText())); ipList.add(ip); } IpAddrBuilder.setIpAddress(ipList); hostBuilder.setIpAddresses(IpAddrBuilder.build()); MacAddress mac = new MacAddress(hostNode.get(MAC_ADDRESS).asText()); hostBuilder.setMacAddress(mac); hostBuilder.setNodeId(new PhysicalNodeId(hostNode.get(NODE_ID).asText())); hostBuilder.setPortId(new PhysicalPortId(hostNode.get(CONNECTOR_ID).asText())); return hostBuilder.build(); } public void close() { physicalLinkHashMap.clear(); physicalPortHashMap.clear(); physicalNodeHashMap.clear(); externalNetworkMac.clear(); } } \ No newline at end of file +/* * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.nemo.renderer.openflow.physicalnetwork; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class PhyConfigLoader { private static final Logger log = LoggerFactory.getLogger(PhyConfigLoader.class); public final static String NODE_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-node-resource.json"; public final static String LINK_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-link-resource.json"; public final static String EXTERNAL_NETWORK_MAC_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-external-resource.json"; public final static String HOST_PROPERTY = "etc/opendaylight/karaf/nemo-openflow-renderer-host-resource.json"; private final static String NODES = "node"; private final static String NODE_ID = "node-id"; private final static String NODE_TYPE = "node-type"; private final static String PORTS = "port"; private final static String PORT_ID = "port-id"; private final static String PORT_TYPE = "port-type"; private final static String PORT_IP = "port-ip-address"; private final static String PORT_MAC = "port-mac-address"; private final static String LINKS = "link"; private final static String LINK_ID = "link-id"; private final static String SRC_NODE_ID = "left-node-id"; private final static String SRC_PORT_ID = "left-port-id"; private final static String DST_NODE_ID = "right-node-id"; private final static String DST_PORT_ID = "right-port-id"; private final static String BANDWIDTH = "link-bandwidth"; private final static String METRIC = "metric"; private final static String HOSTS = "host"; private final static String HOST_NAME = "name"; private final static String HOST_ID = "id"; private final static String HOST_IPS = "ip-addresses"; private final static String CONNECTOR_ID = "connector-id"; private final static String EXTERNAL_NETWORK_MAC = "external-network-mac"; private final static String IP_ADDRESS = "ip-address"; private final static String MAC_ADDRESS = "mac-address"; private final static String ATTRIBUTES = "attribute"; private final static String ATTRIBUTE_NAME = "name"; private final static String ATTRIBUTE_VALUE = "value"; protected final static long DEFAULT_LINK_BANDWIDTH = 10240; protected final static long DEFAULT_LINK_DELAY = 1; protected final static short DEFAULT_LINK_LOSS_RATE = 1; private HashMap physicalNodeHashMap; private HashMap physicalPortHashMap; private HashMap physicalLinkHashMap; private Table externalNetworkMac; // final private DataBroker dataProvider; private ObjectMapper objectMapper = null; private DataBrokerAdapter dataBrokerAdapter = null; public PhyConfigLoader(DataBroker dataBroker) { physicalNodeHashMap = new HashMap(); physicalPortHashMap = new HashMap(); physicalLinkHashMap = new HashMap(); externalNetworkMac = HashBasedTable.create(); this.objectMapper = new ObjectMapper(); this.dataBrokerAdapter = new DataBrokerAdapter(dataBroker); loadNodesPorts(); loadLinks(); loadHosts(); loadExternals(); } public Table getExternalNetworkMac() { return externalNetworkMac; } protected PhysicalNode getPhysicalNode(PhysicalNodeId physicalNodeId){ return physicalNodeHashMap.get(physicalNodeId); } protected PhysicalPort getPhysicalPort(PhysicalPortId physicalPortId){ return physicalPortHashMap.get(physicalPortId); } protected PhysicalLink getPhysicalLink(PhysicalLinkId physicalLinkId){ return physicalLinkHashMap.get(physicalLinkId); } private void loadNodesPorts() { String nodesStr = Utils.readFile(NODE_PROPERTY); try { JsonNode nodeRoot = objectMapper.readTree(nodesStr); List physicalNodes = buildNodes(nodeRoot); for (PhysicalNode physicalNode : physicalNodes) { physicalNodeHashMap.put(physicalNode.getNodeId(), physicalNode); for (PhysicalPort physicalPort : physicalNode.getPhysicalPort()) { physicalPortHashMap.put(physicalPort.getPortId(), physicalPort); } } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;",e); } } private void loadLinks() { String linkStr = Utils.readFile(LINK_PROPERTY); try { JsonNode linkRoot = objectMapper.readTree(linkStr); List physicalLinks = buildLinks(linkRoot); for (PhysicalLink physicalLink : physicalLinks) { physicalLinkHashMap.put(physicalLink.getLinkId(), physicalLink); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;",e); } } private void loadExternals() { String externalStr = Utils.readFile(EXTERNAL_NETWORK_MAC_PROPERTY); try { JsonNode externalRoot = objectMapper.readTree(externalStr); buildExternals(externalRoot); } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;",e); } } private void loadHosts() { String hostStr = Utils.readFile(HOST_PROPERTY); try { JsonNode hostRoot = objectMapper.readTree(hostStr); List physicalHosts = buildHosts(hostRoot); for (PhysicalHost physicalHost : physicalHosts) { dataBrokerAdapter.addPhysicalHost(physicalHost); } } catch (IOException e) { // TODO Auto-generated catch block log.error("Exception;",e); } } private void buildExternals(JsonNode externalRoot) { JsonNode exNetworkNodes = externalRoot.path(EXTERNAL_NETWORK_MAC); log.debug("Build external network mac : {} .", exNetworkNodes); for (int i = 0; i < exNetworkNodes.size(); i++) { log.debug("Build external network execution body"); JsonNode exNetworkNode = exNetworkNodes.get(i); buildExNetwork(exNetworkNode); } } private void buildExNetwork(JsonNode exNetwork){ String nodeId = exNetwork.get(NODE_ID).asText(); String portId = exNetwork.get(PORT_ID).asText(); String peerMac = exNetwork.get(MAC_ADDRESS).asText(); PhysicalNodeId accessNodeId = new PhysicalNodeId(nodeId); PhysicalPortId accessPortId = new PhysicalPortId(portId); MacAddress macAddress = new MacAddress(peerMac); externalNetworkMac.put(accessNodeId,accessPortId,macAddress); } private List buildNodes(JsonNode nodesRoot) { List physicalNodes = new ArrayList(); JsonNode nodes = nodesRoot.path(NODES); log.debug("Build nodes: {} .", nodes); for (int i = 0; i < nodes.size(); i++) { log.debug("build physical node execution body"); JsonNode node = nodes.get(i); PhysicalNode phyNode = buildNode(node); if (phyNode != null) { physicalNodes.add(phyNode); } } return physicalNodes; } private PhysicalNode buildNode(JsonNode node) { PhysicalNodeBuilder nodeBuilder = new PhysicalNodeBuilder(); String node_id = node.get(NODE_ID).asText(); if (node_id.equals("")) return null; nodeBuilder.setNodeId(new PhysicalNodeId(node_id)); PhysicalNodeKey key = new PhysicalNodeKey(nodeBuilder.getNodeId()); nodeBuilder.setKey(key); String strType = node.get(NODE_TYPE).asText(); PhysicalNodeInstance.NodeType nodeType = Utils.getNodeType(strType); nodeBuilder.setNodeType(nodeType); JsonNode ports = node.path(PORTS); List phyPortList = buildPorts(ports); nodeBuilder.setPhysicalPort(phyPortList); JsonNode attributes = node.path(ATTRIBUTES); nodeBuilder.setAttribute(buildNodeAttributes(attributes)); return nodeBuilder.build(); } private List buildPorts(JsonNode ports) { List phyPortList = new ArrayList(); for (int j = 0; j < ports.size(); j++) { //JsonNode port = portIt.next(); JsonNode port = ports.get(j); PhysicalPort physicalPort = buildPort(port); if (physicalPort != null) { phyPortList.add(physicalPort); } } return phyPortList; } private PhysicalPort buildPort(JsonNode port) { PhysicalPortBuilder physicalPortBuilder = new PhysicalPortBuilder(); String strPortId = port.get(PORT_ID).asText(); physicalPortBuilder.setPortId(new PhysicalPortId(strPortId)); String strType = port.get(PORT_TYPE).asText(); PhysicalPortInstance.PortType portType = Utils.getPortType(strType); physicalPortBuilder.setPortType(portType); // String strMac = port.get(PORT_MAC).asText(); // MacAddress macAddress = new MacAddress(strMac); // physicalPortBuilder.setMacAddress(macAddress); // // long bandWidth = port.get(BANDWIDTH).asLong(); // physicalPortBuilder.setBandwidth(bandWidth); JsonNode portAttributes = port.path(ATTRIBUTES); List attributes = buildPortAttributes(portAttributes); physicalPortBuilder.setAttribute(attributes); return physicalPortBuilder.build(); } private List buildPortAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); Attribute attribute = buildPortAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private Attribute buildPortAttribute(JsonNode attribute) { AttributeBuilder attributeBuilder = new AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildNodeAttributes(JsonNode attributes) { List attributeList = new ArrayList(); for (int i = 0; i < attributes.size(); i++) { log.debug("build physical port attribute execution body."); JsonNode portAttribute = attributes.get(i); org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute attribute = buildNodeAttribute(portAttribute); if (attribute != null) { attributeList.add(attribute); } } return attributeList; } private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.Attribute buildNodeAttribute(JsonNode attribute) { org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder attributeBuilder = new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeBuilder(); String strName = attribute.path(ATTRIBUTE_NAME).asText(); if (strName.equals("")) { return null; } attributeBuilder.setAttributeName(new AttributeName(strName)); AttributeValueBuilder attributeValueBuilder = new AttributeValueBuilder(); String strValue = attribute.path(ATTRIBUTE_VALUE).asText(); attributeValueBuilder.setStringValue(strValue); attributeBuilder.setKey(new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.AttributeKey(attributeBuilder.getAttributeName())); attributeBuilder.setAttributeValue(attributeValueBuilder.build()); return attributeBuilder.build(); } private List buildLinks(JsonNode linksRoot) { List physicalLinks = new ArrayList(); JsonNode links = linksRoot.path(LINKS); log.debug("Build links: {} .", links); for (int i = 0; i < links.size(); i++) { log.debug("build physical node execution body"); JsonNode link = links.get(i); PhysicalLink phyLink = buildLink(link); if (phyLink != null) { physicalLinks.add(phyLink); } } return physicalLinks; } private PhysicalLink buildLink(JsonNode linkNode) { PhysicalLinkBuilder linkBuilder = new PhysicalLinkBuilder(); String strLinkId = linkNode.get(LINK_ID).asText(); long metric = linkNode.get(METRIC).asLong(); linkBuilder.setLinkId(new PhysicalLinkId(strLinkId)); linkBuilder.setMetric(metric); linkBuilder.setBandwidth(DEFAULT_LINK_BANDWIDTH); linkBuilder.setDelay(DEFAULT_LINK_DELAY); linkBuilder.setLossRate(DEFAULT_LINK_LOSS_RATE); return linkBuilder.build(); } private List buildHosts(JsonNode hostsNode) { List physicalHosts = new ArrayList(); JsonNode hosts = hostsNode.path(HOSTS); log.debug("Build hosts: {} .", hosts); for (int i = 0; i < hosts.size(); i++) { JsonNode host = hosts.get(i); PhysicalHost physicalHost = buildHost(host); if (physicalHost != null) { physicalHosts.add(physicalHost); } } return physicalHosts; } private PhysicalHost buildHost(JsonNode hostNode) { PhysicalHostBuilder hostBuilder = new PhysicalHostBuilder(); hostBuilder.setHostId(new PhysicalHostId(hostNode.get(HOST_ID).asText())); hostBuilder.setKey(new PhysicalHostKey(hostBuilder.getHostId())); hostBuilder.setHostName(new PhysicalHostName(hostNode.get(HOST_NAME).asText())); IpAddressesBuilder IpAddrBuilder = new IpAddressesBuilder(); List ipList = new ArrayList(); JsonNode ipaddrs = hostNode.path(HOST_IPS); for (int p = 0; p < ipaddrs.size(); p++) { JsonNode ipaddr = ipaddrs.get(p); IpAddress ip = new IpAddress(new Ipv4Address(ipaddr.get(IP_ADDRESS).asText())); ipList.add(ip); } IpAddrBuilder.setIpAddress(ipList); hostBuilder.setIpAddresses(IpAddrBuilder.build()); MacAddress mac = new MacAddress(hostNode.get(MAC_ADDRESS).asText()); hostBuilder.setMacAddress(mac); hostBuilder.setNodeId(new PhysicalNodeId(hostNode.get(NODE_ID).asText())); hostBuilder.setPortId(new PhysicalPortId(hostNode.get(CONNECTOR_ID).asText())); return hostBuilder.build(); } public void close() { physicalLinkHashMap.clear(); physicalPortHashMap.clear(); physicalNodeHashMap.clear(); externalNetworkMac.clear(); } } \ No newline at end of file diff --git a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhysicalNetworkAdapter.java b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhysicalNetworkAdapter.java index cdcbe51..9329ba9 100644 --- a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhysicalNetworkAdapter.java +++ b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/PhysicalNetworkAdapter.java @@ -314,7 +314,8 @@ public class PhysicalNetworkAdapter { try { downLatch.await(); } catch (InterruptedException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } return flowCapableNodeConnector[0]; diff --git a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/Utils.java b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/Utils.java index ea7760d..ec20e53 100644 --- a/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/Utils.java +++ b/nemo-renderers/openflow-renderer/src/main/java/org/opendaylight/nemo/renderer/openflow/physicalnetwork/Utils.java @@ -15,12 +15,13 @@ import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Created by hj on 11/5/15. */ public class Utils { - + private static final Logger log = LoggerFactory.getLogger(Utils.class); protected static PhysicalNodeInstance.NodeType getNodeType(String strType){ switch (strType){ case "switch": @@ -54,13 +55,15 @@ public class Utils { } reader.close(); }catch(IOException e){ - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); }finally{ if(reader != null){ try { reader.close(); } catch (IOException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error("Exception:",e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/CmdExecutor.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/CmdExecutor.java index 592c82b..2f23835 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/CmdExecutor.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/CmdExecutor.java @@ -22,6 +22,7 @@ import java.io.InputStreamReader; * Created by hj on 12/9/15. */ public class CmdExecutor { + private static Logger log = LoggerFactory.getLogger(CmdExecutor.class); private static Connection sshConnection; private static String strHostName = Config.getHostName(); private static String strUserName = Config.getHostUser(); @@ -37,7 +38,8 @@ public class CmdExecutor { return true; } } catch (IOException objException) { - objException.printStackTrace(); +// objException.printStackTrace(); + log.error(objException); if (null != sshConnection) { sshConnection.close(); sshConnection = null; @@ -75,7 +77,8 @@ public class CmdExecutor { return result; } catch (IOException objException) { - objException.printStackTrace(); +// objException.printStackTrace(); + log.error(objException); } finally { if (null != session) { session.close(); diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/EasyCli.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/EasyCli.java index 54ada93..d1f08cd 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/EasyCli.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/EasyCli.java @@ -17,6 +17,7 @@ import java.io.IOException; * Created by hj on 12/9/15. */ public class EasyCli { + private static Logger log = LoggerFactory.getLogger(EasyCli.class); private Shell shell; public EasyCli(String prompt,String hint,Object handler){ @@ -31,7 +32,7 @@ public class EasyCli { try { shell.commandLoop(); } catch (IOException e) { - e.printStackTrace(); + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Cache.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Cache.java index 6f4709d..c0491db 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Cache.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Cache.java @@ -17,6 +17,7 @@ import java.util.List; * Created by hj on 12/8/15. */ public class Cache extends Host { + private static Logger log = LoggerFactory.getLogger(Cache.class); public Cache(String name,String uuId) { super(NodeType.CACHE,name, uuId); } @@ -39,7 +40,8 @@ public class Cache extends Host { try { CmdExecutor.sshExecute("ip netns del " + getName()); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Firewall.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Firewall.java index 0baadf5..fbc3c8d 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Firewall.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Firewall.java @@ -17,6 +17,7 @@ import java.util.List; * Created by hj on 12/8/15. */ public class Firewall extends Host { + private static Logger log = LoggerFactory.getLogger(Firewall.class); public Firewall(String name,String uuId) { super(NodeType.FIREWALL, name,uuId); } @@ -39,7 +40,8 @@ public class Firewall extends Host { try { CmdExecutor.sshExecute("ip netns del " + getName()); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Link.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Link.java index 774df91..3f896a2 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Link.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Link.java @@ -14,6 +14,7 @@ import org.opendaylight.nemo.tool.sandbox.CmdExecutor; * Created by hj on 12/8/15. */ public class Link { + private static Logger log = LoggerFactory.getLogger(Link.class); private final Connector srcInterface; private final Connector dstInterface; @@ -35,7 +36,8 @@ public class Link { try { CmdExecutor.sshExecute(linkAddCmd); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } @@ -44,7 +46,8 @@ public class Link { try { CmdExecutor.sshExecute(linkDelCmd); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Network.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Network.java index 9149268..dd0dbae 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Network.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Network.java @@ -168,7 +168,8 @@ public class Network { root.put("node", nodeArray); return root; } catch (JSONException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } return null; } @@ -199,7 +200,8 @@ public class Network { nodeJson.put("port", ports); return nodeJson; } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } return null; } @@ -246,7 +248,8 @@ public class Network { root.put("external-network-mac", externalMacs); return root; } catch (JSONException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } return null; } @@ -284,7 +287,8 @@ public class Network { root.put("host", hostArray); return root; } catch (JSONException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } return null; } @@ -325,7 +329,8 @@ public class Network { } root.put("link", linkArray); } catch (JSONException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); return null; } return root; @@ -344,7 +349,8 @@ public class Network { linkNode.put("loss-rate", ""); return linkNode; } catch (JSONException e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } return null; diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Node.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Node.java index 1749354..28c9b34 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Node.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/Node.java @@ -57,7 +57,8 @@ abstract public class Node { CmdExecutor.sshExecute(command); } catch (Exception e) { log.error("Error while execute [{}].", command, name, nodeType); - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachine.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachine.java index 8dc1647..1f74de6 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachine.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachine.java @@ -17,6 +17,7 @@ import java.util.List; * Created by hj on 12/8/15. */ public class VirtualMachine extends Host { + private static Logger log = LoggerFactory.getLogger(VirtualMachine.class); public VirtualMachine(String name,String uuId) { super(NodeType.VM, name,uuId); } @@ -39,7 +40,8 @@ public class VirtualMachine extends Host { try { CmdExecutor.sshExecute("ip netns del " + getName()); } catch (Exception e) { - e.printStackTrace(); + // TODO Auto-generated catch block + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/utils/FileUtils.java b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/utils/FileUtils.java index 270d064..644f79e 100644 --- a/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/utils/FileUtils.java +++ b/nemo-tools/sandbox/src/main/java/org/opendaylight/nemo/tool/sandbox/utils/FileUtils.java @@ -14,6 +14,7 @@ import java.io.*; * Created by hj on 12/11/15. */ public class FileUtils { + private static Logger log = LoggerFactory.getLogger(FileUtils.class); public static void write(String path, String content) { try { File f = new File(path); @@ -26,7 +27,7 @@ public class FileUtils { output.write(content); output.close(); } catch (Exception e) { - e.printStackTrace(); + log.error(e); } } } diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CliTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CliTest.java deleted file mode 100644 index 8503373..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CliTest.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox; - -import junit.framework.TestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; -import asg.cliche.Command; -import asg.cliche.Shell; -import asg.cliche.ShellDependent; -import static org.mockito.Mockito.*; -/** - * Created by zhangmeng on 2016/1/14. - */ -public class CliTest extends TestCase { - private Cli cli; - @Before - public void setUp() throws Exception { - cli = new Cli(); - } - - @Test - public void testCliSetShell() throws Exception { - Shell shell = mock(Shell.class); - cli.cliSetShell(shell); - } - - @Test - public void testInstall() throws Exception { - cli.install(); - } - - @Test - public void testUninstall() throws Exception { - cli.uninstall(); - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CmdExecutorTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CmdExecutorTest.java deleted file mode 100644 index 32ab69e..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/CmdExecutorTest.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.awt.color.CMMException; - -import static org.junit.Assert.*; -import ch.ethz.ssh2.Connection; -import ch.ethz.ssh2.Session; -import ch.ethz.ssh2.StreamGobbler; -import org.junit.runner.RunWith; -import org.opendaylight.nemo.tool.sandbox.utils.Config; -import org.opendaylight.nemo.tool.sandbox.utils.FileUtils; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.lang.reflect.Field; -import java.lang.reflect.Method; - -import static org.mockito.Mockito.*; -/** - * Created by zhangmeng on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(ch.ethz.ssh2.Connection.class) -public class CmdExecutorTest extends TestCase { - private CmdExecutor cmdExecutor; - private Class class1; - private Field field_sshConnection; - @Before - public void setUp() throws Exception { - class1 = CmdExecutor.class; - field_sshConnection = class1.getDeclaredField("sshConnection"); - field_sshConnection.setAccessible(true); - - cmdExecutor = new CmdExecutor(); - } - - @Test - public void testOpen() throws Exception { - String command = "cd ~"; - Session session = mock(Session.class); - Connection sshConnection = mock(Connection.class); - - Assert.assertTrue(CmdExecutor.open() == false); - PowerMockito.whenNew(Connection.class).withArguments(any(String.class)).thenReturn(sshConnection); - when(sshConnection.authenticateWithPassword(any(String.class), any(String.class))).thenReturn(true); - Assert.assertTrue(CmdExecutor.open() == false); -// Assert.assertTrue(field_sshConnection.get(class1) == sshConnection); -// verify(sshConnection).authenticateWithPassword(any(String.class), any(String.class)); - - field_sshConnection.set(class1, sshConnection); - when(sshConnection.openSession()).thenReturn(session); - doNothing().when(session).execCommand(command); - when(session.getStdout()).thenReturn(mock(InputStream.class)); - Assert.assertTrue(CmdExecutor.sshExecute(command) != null); - verify(session).getStdout(); - CmdExecutor.queryInterfaceMac("eth0"); - CmdExecutor.queryInterfaceMac("1","eth0"); - - CmdExecutor.close(); - } - - @Test - public void testgetMacFromResult() throws Exception { - Method method = class1.getDeclaredMethod("getMacFromResult",new Class[]{ - String.class - }); - method.setAccessible(true); - Assert.assertTrue(method.invoke(class1,"HWaddr") != null); - } - -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/EasyCliTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/EasyCliTest.java deleted file mode 100644 index fe9d689..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/EasyCliTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox; - -import junit.framework.TestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; -import asg.cliche.Shell; -import asg.cliche.ShellFactory; - -import java.io.IOException; -import java.lang.reflect.Field; -import static org.mockito.Mockito.*; - -/** - * Created by zhangmeng on 2016/1/14. - */ -public class EasyCliTest extends TestCase { - private EasyCli easyCli; - @Before - public void setUp() throws Exception { - easyCli = new EasyCli("1","2","3"); - } - - @Test - public void testAdd() throws Exception { - easyCli.add("4"); - } - - @Test - public void testRun() throws Exception { - Field field = EasyCli.class.getDeclaredField("shell"); - field.setAccessible(true); - - Shell shell = mock(Shell.class); - - field.set(easyCli,shell); - doNothing().when(shell).commandLoop(); - easyCli.run(); - verify(shell).commandLoop(); - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/SandBoxManagerTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/SandBoxManagerTest.java deleted file mode 100644 index f534982..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/SandBoxManagerTest.java +++ /dev/null @@ -1,252 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox; - -import junit.framework.Assert; -import junit.framework.TestCase; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; -import org.json.JSONObject; -import org.junit.runner.RunWith; -import org.opendaylight.nemo.tool.sandbox.models.*; -import org.opendaylight.nemo.tool.sandbox.utils.PropertyLoader; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; -/** - * Created by zhangmeng on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest(CmdExecutor.class) -public class SandBoxManagerTest extends TestCase { - private SandBoxManager sandBoxManager; - private Method method; - @Before - public void setUp() throws Exception { - sandBoxManager = SandBoxManager.getInstance(); - } - - @Test - public void testGetInstance() throws Exception { - sandBoxManager = SandBoxManager.getInstance(); - } - - @Test - public void testGets() throws Exception { - Field field = SandBoxManager.class.getDeclaredField("network"); - field.setAccessible(true); - - Network network = mock(Network.class); - JSONObject jsonObject = mock(JSONObject.class); - - //no powermock and network=null - field.set(sandBoxManager, null); - org.junit.Assert.assertTrue(!sandBoxManager.createNetwork(null)); - org.junit.Assert.assertTrue(!sandBoxManager.createNetwork()); - org.junit.Assert.assertTrue(sandBoxManager.getHosts() == null); - org.junit.Assert.assertTrue(sandBoxManager.getExternals() == null); - org.junit.Assert.assertTrue(sandBoxManager.getSwitches() == null); - org.junit.Assert.assertTrue(sandBoxManager.getLinks() == null); - org.junit.Assert.assertTrue(sandBoxManager.destroyNetwork()); - - field.set(sandBoxManager, network); - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.open()).thenReturn(true); - when(network.hostJsonNode()).thenReturn(jsonObject); - when(network.externalJsonNode()).thenReturn(jsonObject); - when(network.nodeJsonNode()).thenReturn(jsonObject); - when(network.innerLinkJsonNode()).thenReturn(jsonObject); - when(network.execute(any(String.class), any(String.class))).thenReturn("test"); - org.junit.Assert.assertTrue(sandBoxManager.execute("1","1").equals("test")); - org.junit.Assert.assertTrue(sandBoxManager.getHosts() != null); - org.junit.Assert.assertTrue(sandBoxManager.getExternals() != null); - org.junit.Assert.assertTrue(sandBoxManager.getSwitches() != null); - org.junit.Assert.assertTrue(sandBoxManager.getLinks() != null); - - org.junit.Assert.assertTrue(sandBoxManager.createNetwork(PropertyLoader.readLines("/Network", "Network file does not exist."))); - org.junit.Assert.assertTrue(sandBoxManager.createNetwork()); - org.junit.Assert.assertTrue(sandBoxManager.destroyNetwork()); - } - @Test - public void testGetNodeType() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("getNodeType",new Class[]{String.class}); - method.setAccessible(true); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "Switch") == NodeType.SWITCH); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "Router") == NodeType.ROUTER); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "Cache") == NodeType.CACHE); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "Firewall") == NodeType.FIREWALL); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "Vm") == NodeType.VM); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,"1") == NodeType.UNKNOWN); - - } - - @Test - public void testHandleLink() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("handleLink",new Class[]{String.class}); - method.setAccessible(true); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, "12") == null); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,"1,2,3,4,5") != null); - } - - @Test - public void testSaveConnector() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("saveConnector",new Class[]{ - Connector.class, - Map.class - }); - method.setAccessible(true); - - Connector connector = mock(Connector.class); - Map> connectorMap = mock(Map.class); - - when(connector.getNodeName()).thenReturn("test"); - when(connectorMap.containsKey(any(String.class))) - .thenReturn(true) - .thenReturn(false); - when(connectorMap.get(any(String.class))).thenReturn(mock(List.class)); - method.invoke(sandBoxManager, connector, connectorMap); - method.invoke(sandBoxManager, connector, connectorMap); - verify(connectorMap,times(2)).containsKey(any(String.class)); - } - - @Test - public void testParserHost() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("parserHost",new Class[]{ - String[].class, - Map.class, - Host.class - }); - method.setAccessible(true); - - String[] args = {"1","2","3","1:2"}; - Map> connectorMap = mock(Map.class); - Host host = mock(Host.class); - Connector connector = mock(Connector.class); - List connectors = new ArrayList(); - - connectors.add(connector); - - when(host.getName()).thenReturn("test"); - when(connectorMap.get(host.getName())).thenReturn(connectors); - - method.invoke(sandBoxManager, args, connectorMap, host); - verify(host,times(2)).getName(); - } - - @Test - public void testHandleFirewall() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("handleFirewall",new Class[]{ - String.class, - Map.class - }); - method.setAccessible(true); - - String line = "test"; - Map> connectorMap = mock(Map.class); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) == null); - line = "1,2,3,1:2"; - when(connectorMap.get(any(String.class))).thenReturn(null); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) != null); - } - - @Test - public void testHandleCache() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("handleCache",new Class[]{ - String.class, - Map.class - }); - method.setAccessible(true); - - String line = "test"; - Map> connectorMap = mock(Map.class); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) == null); - line = "1,2,3,1:2"; - when(connectorMap.get(any(String.class))).thenReturn(null); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) != null); - - } - - @Test - public void testHandleVirtualMachine() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("handleVirtualMachine",new Class[]{ - String.class, - Map.class - }); - method.setAccessible(true); - - String line = "test"; - Map> connectorMap = mock(Map.class); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) == null); - line = "1,2,3,1:2"; - when(connectorMap.get(any(String.class))).thenReturn(null); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager,line,connectorMap) != null); - } - - @Test - public void testHandleSwitch() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("handleSwitch",new Class[]{ - String.class, - Map.class - }); - method.setAccessible(true); - - List connectorList = new ArrayList(); - Connector connector = mock(Connector.class); - String line = "test"; - Map> connectorMap = mock(Map.class); - - connectorList.add(connector); - - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, line, connectorMap) == null); - line = "Router,2,3"; - when(connectorMap.get(any(String.class))).thenReturn(connectorList); - org.junit.Assert.assertTrue(method.invoke(sandBoxManager, line, connectorMap) != null); - - } - - @Test - public void testGenerateNetwork() throws Exception { - method = SandBoxManager.class.getDeclaredMethod("generateNetwork",new Class[]{List.class}); - method.setAccessible(true); - - List list = new ArrayList(); - list.add("Switch"); - method.invoke(sandBoxManager, list); - list.clear(); - list.add("Vm"); - method.invoke(sandBoxManager, list); - list.clear(); - list.add("Cache"); - method.invoke(sandBoxManager, list); - list.clear(); - list.add("Firewall"); - method.invoke(sandBoxManager, list); - - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/CacheTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/CacheTest.java deleted file mode 100644 index 37b58b8..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/CacheTest.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; - -import java.lang.reflect.Method; -import java.lang.reflect.Field; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({CmdExecutor.class}) -public class CacheTest extends TestCase { - String name,uuId; - Cache cacheTest; - private Method method; - private Class class1; - private Connector connector; - - - @Before - public void setUp() throws Exception { - name = new String("name1"); - uuId = new String("11111111-1111-1111-1111-111111111111"); - cacheTest = new Cache(name,uuId); - class1 = Cache.class; - org.opendaylight.nemo.tool.sandbox.models.Connector connector = mock(org.opendaylight.nemo.tool.sandbox.models.Connector.class); - cacheTest.addConnectors(connector); - } - - @Test - public void testGenerateCommands() throws Exception { - method = class1.getDeclaredMethod("generateCommands",new Class[]{}); - method.setAccessible(true); - Assert.assertNotNull(method.invoke(cacheTest)); - } - - @Test - public void testUninstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - cacheTest.uninstall(); - - } - -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/ConnectorTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/ConnectorTest.java deleted file mode 100644 index d7fe784..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/ConnectorTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.opendaylight.nemo.tool.sandbox.models.Connector; - -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import static org.junit.Assert.*; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -public class ConnectorTest extends TestCase { - private int order; - private String nodeName; - private Connector connectorTest; - - @Before - public void setUp() throws Exception { - order = 10; - nodeName = "connector"; - connectorTest = new Connector(nodeName,order); - - } - - @Test - public void testGetConnectorName() throws Exception { - Assert.assertNotNull(connectorTest.getConnectorName()); - } - - @Test - public void testGetOrder() throws Exception { - Assert.assertNotNull(connectorTest.getOrder()); - } - - @Test - public void testGetNodeName() throws Exception { - Assert.assertNotNull(connectorTest.getNodeName()); - } - - @Test - public void testEquals() throws Exception { - Assert.assertNotNull(connectorTest.equals(this)); - - } - - @Test - public void testHashCode() throws Exception { - Assert.assertNotNull(connectorTest.hashCode()); - } - - @Test - public void testToString() throws Exception { - Assert.assertNotNull(connectorTest.toString()); - } - - @Test - public void testCompareTo() throws Exception { - Assert.assertNotNull(connectorTest.compareTo(new Connector("connector2",5))); - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/FirewallTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/FirewallTest.java deleted file mode 100644 index 6d2ad88..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/FirewallTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; -import org.opendaylight.nemo.tool.sandbox.models.Firewall; - -import java.lang.reflect.Method; -import java.lang.reflect.Field; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({CmdExecutor.class}) -public class FirewallTest extends TestCase { - - String name,uuId; - Firewall firewallTest; - private Method method; - private Class class1; - private Connector connector; - - @Before - public void setUp() throws Exception { - name = new String("name1"); - uuId = new String("11111111-1111-1111-1111-111111111111"); - firewallTest = new Firewall(name,uuId); - class1 = Firewall.class; - org.opendaylight.nemo.tool.sandbox.models.Connector connector = mock(org.opendaylight.nemo.tool.sandbox.models.Connector.class); - firewallTest.addConnectors(connector); - - } - - @Test - public void testGenerateCommands() throws Exception { - method = class1.getDeclaredMethod("generateCommands",new Class[]{}); - method.setAccessible(true); - Assert.assertNotNull(method.invoke(firewallTest)); - - } - - @Test - public void testUninstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - firewallTest.uninstall(); - - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/LinkTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/LinkTest.java deleted file mode 100644 index 2faf1a5..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/LinkTest.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ - -package org.opendaylight.nemo.tool.sandbox.models; -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; -import org.opendaylight.nemo.tool.sandbox.models.Connector; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({CmdExecutor.class}) -public class LinkTest extends TestCase { - - private Connector srcInterface; - private Connector dstInterface; - private Link likeTest; - - @Before - public void setUp() throws Exception { - srcInterface = new Connector("src",1); - dstInterface = new Connector("dst",2); - likeTest = new Link(srcInterface,dstInterface); - - } - - @Test - public void testGetSrcConnector() throws Exception { - Assert.assertNotNull(likeTest.getSrcConnector()); - } - - @Test - public void testGetDstConnector() throws Exception { - Assert.assertNotNull(likeTest.getDstConnector()); - } - - @Test - public void testInstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - likeTest.install(); - } - - @Test - public void testUninstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - likeTest.uninstall(); - } - - @Test - public void testEquals() throws Exception { - Assert.assertNotNull(likeTest.equals(this)); - } - - @Test - public void testHashCode() throws Exception { - Assert.assertNotNull(likeTest.hashCode()); - } - - @Test - public void testToString() throws Exception { - Assert.assertNotNull(likeTest.toString()); - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/NetworkTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/NetworkTest.java deleted file mode 100644 index b8b9032..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/NetworkTest.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; -import org.opendaylight.nemo.tool.sandbox.models.Cache; -import org.opendaylight.nemo.tool.sandbox.models.Connector; -import org.opendaylight.nemo.tool.sandbox.models.Network; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; -import org.opendaylight.nemo.tool.sandbox.utils.Config; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - - -/** - * Created by Thomas Liu on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({CmdExecutor.class}) -public class NetworkTest extends TestCase { - private Network networkTest; - - @Before - public void setUp() throws Exception { - networkTest = new Network(); - - } - - @Test - public void testAddHost() throws Exception { - networkTest.addHost(new Cache("src1","1")); - networkTest.addHost(new Cache("dst2","1")); - } - - @Test - public void testAddSwitch() throws Exception { - networkTest.addSwitch(new Switch("dst1", 1L)); - networkTest.addSwitch(new Switch("src2", 1L)); - networkTest.addSwitch(new Switch("src3", 1L)); - networkTest.addSwitch(new Switch("dst3", 1L)); - } - - @Test - public void testAddLink() throws Exception { - networkTest.addLink(new Link(new Connector("src1", 1), new Connector("dst1", 2))); - networkTest.addLink(new Link(new Connector("src2", 1), new Connector("dst2", 2))); - networkTest.addLink(new Link(new Connector("src3", 1), new Connector("dst3", 2))); - - } - - @Test - public void testExecute() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.open()).thenReturn(true); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.doNothing().when(CmdExecutor.class); - CmdExecutor.close(); - Assert.assertNotNull(networkTest.execute("cache", "command")); - - } - - @Test - public void testInstall() throws Exception { - //test traversal - networkTest.install(); - - } - - @Test - public void testUninstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - - networkTest.uninstall(); - - } - - @Test - public void testEchoConfig() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - - networkTest.echoConfig(); - - //test hostJsonNode() - - - - } - - @Test - public void testInnerLinkJsonNode() throws Exception { - - Assert.assertNotNull(networkTest.innerLinkJsonNode()); - - - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/RouterTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/RouterTest.java deleted file mode 100644 index 8326a78..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/RouterTest.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -public class RouterTest extends TestCase { - private String name; - private long dataPathId; - private Router routerTest; - @Before - public void setUp() throws Exception { - - } - - @Test - public void test1() throws Exception { - name = new String("name1"); - dataPathId = 1L; - routerTest = new Router(name,dataPathId); - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/SwitchTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/SwitchTest.java deleted file mode 100644 index d94055f..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/SwitchTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import org.opendaylight.nemo.tool.sandbox.utils.Config; -import org.opendaylight.nemo.tool.sandbox.utils.HexString; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import java.lang.reflect.Method; -import java.lang.reflect.Field; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -public class SwitchTest extends TestCase { - - private String name; - private long dataPathId; - private Switch switchTest; - private Class class1; - private Method method; - - - @Before - public void setUp() throws Exception { - name = new String("name1"); - dataPathId = 1L; - switchTest = new Switch(name,dataPathId); - class1 = Switch.class; - org.opendaylight.nemo.tool.sandbox.models.Connector connector = new org.opendaylight.nemo.tool.sandbox.models.Connector("connectorname",1); - switchTest.addConnectors(connector); - } - - @Test - public void testGetDataPathId() throws Exception { - Assert.assertNotNull(switchTest.getDataPathId()); - } - - @Test - public void testGenerateCommands() throws Exception { - method = class1.getDeclaredMethod("generateCommands",new Class[]{}); - method.setAccessible(true); - Assert.assertNotNull(method.invoke(switchTest)); - - } - - @Test - public void testToString() throws Exception { - Assert.assertNotNull(switchTest.toString()); - - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachineTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachineTest.java deleted file mode 100644 index f405927..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/models/VirtualMachineTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.models; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import static org.mockito.Mockito.*; -import static org.junit.Assert.*; - -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; -import org.opendaylight.nemo.tool.sandbox.models.VirtualMachine; - -import java.lang.reflect.Method; -import java.lang.reflect.Field; - -import org.junit.runner.RunWith; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import org.opendaylight.nemo.tool.sandbox.CmdExecutor; - -/** - * Created by Thomas Liu on 2016/1/14. - */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({CmdExecutor.class}) -public class VirtualMachineTest extends TestCase { - String name,uuId; - Cache cacheTest; - private VirtualMachine virtualMachineTest; - private Connector connector; - private Class class1; - private Method method; - @Before - public void setUp() throws Exception { - name = new String("name1"); - uuId = new String("11111111-1111-1111-1111-111111111111"); - virtualMachineTest = new VirtualMachine(name,uuId); - org.opendaylight.nemo.tool.sandbox.models.Connector connector = mock(org.opendaylight.nemo.tool.sandbox.models.Connector.class); - virtualMachineTest.addConnectors(connector); - class1 = VirtualMachine.class; - - } - - @Test - public void testGenerateCommands() throws Exception { - method = class1.getDeclaredMethod("generateCommands",new Class[]{}); - method.setAccessible(true); - Assert.assertNotNull(method.invoke(virtualMachineTest)); - } - - @Test - public void testUninstall() throws Exception { - PowerMockito.mockStatic(CmdExecutor.class); - PowerMockito.when(CmdExecutor.sshExecute(any(String.class))).thenReturn("null"); - virtualMachineTest.uninstall(); - - } -} \ No newline at end of file diff --git a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/northbound/SandboxNorthboundTest.java b/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/northbound/SandboxNorthboundTest.java deleted file mode 100644 index d15f0cb..0000000 --- a/nemo-tools/sandbox/src/test/java/org/opendaylight/nemo/tool/sandbox/northbound/SandboxNorthboundTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2016 Huawei, Inc. and others. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/epl-v10.html - */ -package org.opendaylight.nemo.tool.sandbox.northbound; - -import junit.framework.TestCase; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; -import org.codehaus.enunciate.jaxrs.ResponseCode; -import org.codehaus.enunciate.jaxrs.StatusCodes; -import org.codehaus.enunciate.jaxrs.TypeHint; -import org.junit.runner.RunWith; -import org.opendaylight.nemo.tool.sandbox.SandBoxManager; - -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.UriInfo; -import java.util.ArrayList; -import java.util.List; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.api.support.membermodification.MemberMatcher; -import org.powermock.api.support.membermodification.MemberModifier; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; -import static org.mockito.Mockito.*; -/** - * Created by zhangmeng on 2016/1/14. - */ - -public class SandboxNorthboundTest extends TestCase { - private SandboxNorthbound sandboxNorthbound; - @Before - public void setUp() throws Exception { - sandboxNorthbound = new SandboxNorthbound(); - } - - @Test - public void testCreate() throws Exception { - ExecuteRequest executeRequest = new ExecuteRequest(); - Assert.assertTrue(sandboxNorthbound.create(null,"test") != null); - Assert.assertTrue(sandboxNorthbound.create(null,executeRequest) != null); - } - - @Test - public void testDestroy() throws Exception { - Assert.assertTrue(sandboxNorthbound.destroy(null) != null); - } - - @Test - public void testGetXml() throws Exception { - Assert.assertTrue(sandboxNorthbound.getXml() != null); - } - - @Test - public void testGetHosts() throws Exception { - Assert.assertTrue(sandboxNorthbound.getHosts() == null); - } - - @Test - public void testGetSwitches() throws Exception { - Assert.assertTrue(sandboxNorthbound.getSwitches() != null); - } - - @Test - public void testGetLinks() throws Exception { - Assert.assertTrue(sandboxNorthbound.getLinks() != null); - } - - @Test - public void testGetExternals() throws Exception { - Assert.assertTrue(sandboxNorthbound.getExternals() == null); - } -} \ No newline at end of file -- 2.36.6