--- /dev/null
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+public class ExternalMapperTest {
+
+ private ExternalMapper mapper;
+
+ private OfContext ctx;
+ private short tableId;
+ private NodeId nodeId;
+ private PolicyInfo policyInfo;
+ private FlowMap flowMap;
+ private SwitchManager switchManager;
+
+ @Before
+ public void initialisation() {
+ ctx = mock(OfContext.class);
+ tableId = 5;
+ nodeId = mock(NodeId.class);
+ policyInfo = mock(PolicyInfo.class);
+ flowMap = mock(FlowMap.class);
+ switchManager = mock(SwitchManager.class);
+ when(ctx.getSwitchManager()).thenReturn(switchManager);
+
+ mapper = new ExternalMapper(ctx, tableId);
+ }
+
+ @Test
+ public void consturctorTest() {
+ Assert.assertEquals(tableId, mapper.getTableId());
+ }
+
+ @Test
+ public void syncTest() throws Exception {
+ NodeConnectorId nodeConnectorId = mock(NodeConnectorId.class);
+ Set<NodeConnectorId> externalPorts = new HashSet<NodeConnectorId>(Arrays.asList(nodeConnectorId));
+ when(switchManager.getExternalPorts(nodeId)).thenReturn(externalPorts);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+
+ @Test
+ public void syncTestNoExternalPorts() throws Exception {
+ when(switchManager.getExternalPorts(nodeId)).thenReturn(null);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, never()).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+}
-/*\r
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotNull;\r
-\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.GroupTable.BucketCtx;\r
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.GroupTable.GroupCtx;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import com.google.common.base.Objects;\r
-import com.google.common.collect.ImmutableList;\r
-\r
-public class GroupTableTest extends OfTableTest {\r
- protected static final Logger LOG =\r
- LoggerFactory.getLogger(GroupTableTest.class);\r
-\r
- GroupTable table;\r
-\r
- NodeConnectorId tunnelId =\r
- new NodeConnectorId(nodeId.getValue() + ":42");\r
- NodeConnectorId remoteTunnelId =\r
- new NodeConnectorId(remoteNodeId.getValue() + ":101");\r
- PortNumber portNumber = new PortNumber(4789);\r
-\r
- @Before\r
- public void setup() throws Exception {\r
- initCtx();\r
- table = new GroupTable(ctx);\r
- }\r
-\r
- @Test\r
- public void testGroup() throws Exception {\r
- switchManager.addSwitch(\r
- nodeId,\r
- tunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder()\r
- .setIp(new IpAddress(new Ipv4Address("1.2.3.4")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .setPort(portNumber)\r
- .build())).build());\r
- switchManager.addSwitch(\r
- remoteNodeId,\r
- remoteTunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.5")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .setPort(portNumber)\r
- .build())).build());\r
- Endpoint localEp = localEP().build();\r
- endpointManager.addEndpoint(localEp);\r
- Endpoint remoteEp = remoteEP(remoteNodeId).build();\r
- endpointManager.addEndpoint(remoteEp);\r
-\r
-\r
- policyResolver.addTenant(baseTenant().setContract(\r
- ImmutableList.<Contract>of(baseContract(null).build())).build());\r
-\r
- HashMap<GroupId, GroupCtx> groupMap = new HashMap<>();\r
- table.sync(nodeId, ctx.getPolicyResolver().getCurrentPolicy(), groupMap);\r
-\r
- assertEquals(1, groupMap.size());\r
- int fdId = OrdinalFactory.getContextOrdinal(tid, fd);\r
- GroupCtx ctx = groupMap.get(new GroupId(Long.valueOf(fdId)));\r
- assertNotNull(ctx);\r
- long tunBucketId =\r
- OrdinalFactory.getContextOrdinal(remoteNodeId);\r
- tunBucketId |= 1L << 31;\r
-\r
- int count = 0;\r
- for (BucketCtx bctx : ctx.bucketMap.values()) {\r
- if (Objects.equal(Long.valueOf(4),\r
- bctx.newb.getBucketId().getValue())) {\r
- count += 1;\r
- } else if (Objects.equal(Long.valueOf(tunBucketId),\r
- bctx.newb.getBucketId().getValue())) {\r
-\r
- count += 1;\r
- }\r
- }\r
- assertEquals(2, count);\r
- }\r
-}\r
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointManager;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.GroupTable.BucketCtx;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.GroupTable.GroupCtx;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
+import org.opendaylight.groupbasedpolicy.resolver.EgKey;
+import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
+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.inet.types.rev100924.Ipv6Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.BucketId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+
+public class GroupTableTest {
+
+ private GroupTable groupTable;
+
+ private OfContext ofContext;
+ private DataBroker dataBroker;
+ private ReadOnlyTransaction readOnlyTransaction;
+ private CheckedFuture<Optional<Node>, ReadFailedException> checkedFutureRead;
+ private Optional<Node> optional;
+ private Node node;
+ private FlowCapableNode flowCapableNode;
+ private Group group;
+ private Buckets buckets;
+ private Bucket bucket;
+ private WriteTransaction writeTransaction;
+ private CheckedFuture<Void, TransactionCommitFailedException> checkedFutureWrite;
+ private NodeId nodeId;
+ private PolicyInfo policyInfo;
+ private FlowMap flowMap;
+ private GroupId groupId;
+ private GroupCtx groupCtx;
+ private HashMap<GroupId, GroupCtx> groupMap;
+ private Bucket bucketOther;
+ private BucketCtx bucketCtx;
+ private EndpointManager endpointManager;
+ private Endpoint localEp;
+ private EgKey egKey;
+ private OfOverlayContext ofc;
+ private NodeConnectorId nodeConnectorId;
+
+ @SuppressWarnings("unchecked")
+ @Before
+ public void initialisation() throws Exception {
+ ofContext = mock(OfContext.class);
+ groupTable = spy(new GroupTable(ofContext));
+
+ dataBroker = mock(DataBroker.class);
+ when(ofContext.getDataBroker()).thenReturn(dataBroker);
+ readOnlyTransaction = mock(ReadOnlyTransaction.class);
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+ checkedFutureRead = mock(CheckedFuture.class);
+ when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(
+ checkedFutureRead);
+ optional = mock(Optional.class);
+ when(checkedFutureRead.get()).thenReturn(optional);
+ node = mock(Node.class);
+ when(optional.isPresent()).thenReturn(true);
+ when(optional.get()).thenReturn(node);
+
+ writeTransaction = mock(WriteTransaction.class);
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ checkedFutureWrite = mock(CheckedFuture.class);
+ when(writeTransaction.submit()).thenReturn(checkedFutureWrite);
+
+ flowCapableNode = mock(FlowCapableNode.class);
+ when(node.getAugmentation(FlowCapableNode.class)).thenReturn(flowCapableNode);
+
+ group = mock(Group.class);
+ List<Group> groups = Arrays.asList(group);
+ when(flowCapableNode.getGroup()).thenReturn(groups);
+
+ buckets = mock(Buckets.class);
+ when(group.getBuckets()).thenReturn(buckets);
+ bucket = mock(Bucket.class);
+ when(bucket.getAction()).thenReturn(Arrays.asList(mock(Action.class)));
+ List<Bucket> bucketList = Arrays.asList(bucket);
+ when(buckets.getBucket()).thenReturn(bucketList);
+
+ bucketOther = mock(Bucket.class);
+ when(bucketOther.getAction()).thenReturn(Arrays.asList(mock(Action.class)));
+
+ groupId = mock(GroupId.class);
+ groupCtx = new GroupCtx(groupId);
+ groupMap = new HashMap<>();
+ groupMap.put(groupId, groupCtx);
+ bucketCtx = mock(BucketCtx.class);
+ groupCtx.bucketMap.put(mock(BucketId.class), bucketCtx);
+
+ nodeId = mock(NodeId.class);
+ policyInfo = mock(PolicyInfo.class);
+ flowMap = mock(FlowMap.class);
+
+ endpointManager = mock(EndpointManager.class);
+ when(ofContext.getEndpointManager()).thenReturn(endpointManager);
+ localEp = mock(Endpoint.class);
+ when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(Arrays.asList(localEp));
+ PolicyResolver policyResolver = mock(PolicyResolver.class);
+ when(ofContext.getPolicyResolver()).thenReturn(policyResolver);
+ IndexedTenant indexedTenant = mock(IndexedTenant.class);
+ when(policyResolver.getTenant(any(TenantId.class))).thenReturn(indexedTenant);
+ EndpointGroup epg = mock(EndpointGroup.class);
+ when(indexedTenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(epg);
+ egKey = mock(EgKey.class);
+ when(endpointManager.getGroupsForNode(any(NodeId.class))).thenReturn(new HashSet<EgKey>(Arrays.asList(egKey)));
+ ofc = mock(OfOverlayContext.class);
+ when(localEp.getAugmentation(OfOverlayContext.class)).thenReturn(ofc);
+ nodeConnectorId = mock(NodeConnectorId.class);
+ when(ofc.getNodeConnectorId()).thenReturn(nodeConnectorId);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void updateTest() throws Exception {
+ doNothing().when(groupTable).sync(any(NodeId.class), any(PolicyInfo.class), any(HashMap.class));
+ doReturn(true).when(groupTable).syncGroupToStore(any(WriteTransaction.class), any(NodeId.class),
+ any(HashMap.class));
+
+ groupTable.update(nodeId, policyInfo, flowMap);
+ verify(checkedFutureWrite).get();
+ }
+
+ @Test
+ public void updateTestIsPresentFalse() throws Exception {
+ when(optional.isPresent()).thenReturn(false);
+
+ groupTable.update(nodeId, policyInfo, flowMap);
+ verify(checkedFutureWrite, never()).get();
+ }
+
+ @Test
+ public void updateTestIsFcnNull() throws Exception {
+ when(node.getAugmentation(FlowCapableNode.class)).thenReturn(null);
+
+ groupTable.update(nodeId, policyInfo, flowMap);
+ verify(checkedFutureWrite, never()).get();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void updateTestIsFcnGroupNull() throws Exception {
+ doNothing().when(groupTable).sync(any(NodeId.class), any(PolicyInfo.class), any(HashMap.class));
+ doReturn(true).when(groupTable).syncGroupToStore(any(WriteTransaction.class), any(NodeId.class),
+ any(HashMap.class));
+ when(flowCapableNode.getGroup()).thenReturn(null);
+
+ groupTable.update(nodeId, policyInfo, flowMap);
+ verify(checkedFutureWrite).get();
+ }
+
+ @Test
+ public void syncGroupToStoreTestVisitedFalse() {
+ groupCtx.visited = false;
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertTrue(result);
+ }
+
+ @Test
+ public void syncGroupToStoreTestBucketMapEmpty() {
+ groupCtx.visited = true;
+ groupCtx.bucketMap = Collections.emptyMap();
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertFalse(result);
+ }
+
+ @Test
+ public void syncGroupToStoreTestBNullBucketVisitedFalse() {
+ groupCtx.visited = true;
+ bucketCtx.visited = false;
+ bucketCtx.newb = bucket;
+
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertTrue(result);
+ verify(bucket).getBucketId();
+ verify(writeTransaction).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void syncGroupToStoreTestBNullBucketVisitedTrue() {
+ groupCtx.visited = true;
+ bucketCtx.visited = true;
+ bucketCtx.newb = bucket;
+
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertTrue(result);
+ verify(bucket).getBucketId();
+ verify(writeTransaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
+ any(Group.class), anyBoolean());
+ }
+
+ @Test
+ public void syncGroupToStoreTestBucketVisitedFalse() {
+ groupCtx.visited = true;
+ bucketCtx.visited = false;
+ bucketCtx.newb = bucket;
+ bucketCtx.b = bucketOther;
+
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertTrue(result);
+ verify(bucketOther).getBucketId();
+ verify(writeTransaction).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Test
+ public void syncGroupToStoreTestBucketVisitedTrueActionsEqualFalse() {
+ groupCtx.visited = true;
+ bucketCtx.visited = true;
+ bucketCtx.newb = bucket;
+ bucketCtx.b = bucketOther;
+
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertTrue(result);
+ verify(bucketOther).getBucketId();
+ verify(writeTransaction).merge(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
+ any(Group.class), anyBoolean());
+ }
+
+ @Test
+ public void syncGroupToStoreTestBucketVisitedTrueActionsEqualTrue() {
+ groupCtx.visited = true;
+ bucketCtx.visited = true;
+ bucketCtx.newb = bucket;
+ bucketCtx.b = bucket;
+
+ boolean result = groupTable.syncGroupToStore(writeTransaction, nodeId, groupMap);
+ Assert.assertFalse(result);
+ verify(bucket).getBucketId();
+ verifyNoMoreInteractions(writeTransaction);
+ }
+
+ @Test
+ public void syncTestNodeEqualsTrue() throws Exception {
+ groupMap = new HashMap<>();
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeId)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value:5");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertEquals(1, resultGroup.bucketMap.size());
+ BucketCtx result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[0];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ }
+
+ @Test
+ public void syncTestNodeEqualsTruePortNumberException() throws Exception {
+ groupMap = new HashMap<>();
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeId)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertTrue(resultGroup.bucketMap.isEmpty());
+ }
+
+ @Test
+ public void syncTestNodeEqualsTrueLocalEpExternal() throws Exception {
+ groupMap = new HashMap<>();
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeId)));
+ when(ofc.getLocationType()).thenReturn(LocationType.External);
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertTrue(resultGroup.bucketMap.isEmpty());
+ }
+
+ @Test
+ public void syncTestNodeEqualsFalse() throws Exception {
+ groupMap = new HashMap<>();
+
+ NodeId nodeIdOther = mock(NodeId.class);
+ when(nodeIdOther.getValue()).thenReturn("5");
+ SwitchManager switchManager = mock(SwitchManager.class);
+ when(ofContext.getSwitchManager()).thenReturn(switchManager);
+ IpAddress tunDst = mock(IpAddress.class);
+ when(switchManager.getTunnelIP(nodeIdOther, TunnelTypeVxlan.class)).thenReturn(tunDst);
+ NodeConnectorId tunPort = mock(NodeConnectorId.class);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(tunPort);
+ Ipv4Address ipv4Address = mock(Ipv4Address.class);
+ when(tunDst.getIpv4Address()).thenReturn(ipv4Address);
+ when(ipv4Address.getValue()).thenReturn("127.0.0.1");
+ when(tunPort.getValue()).thenReturn("127.0.0.1");
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeIdOther)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value:5");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertEquals(2, resultGroup.bucketMap.size());
+ BucketCtx result;
+ result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[0];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[1];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ }
+
+ @Test
+ public void syncTestNodeEqualsFalseIpv4Null() throws Exception {
+ groupMap = new HashMap<>();
+
+ NodeId nodeIdOther = mock(NodeId.class);
+ when(nodeIdOther.getValue()).thenReturn("5");
+ SwitchManager switchManager = mock(SwitchManager.class);
+ when(ofContext.getSwitchManager()).thenReturn(switchManager);
+ IpAddress tunDst = mock(IpAddress.class);
+ when(switchManager.getTunnelIP(nodeIdOther, TunnelTypeVxlan.class)).thenReturn(tunDst);
+ NodeConnectorId tunPort = mock(NodeConnectorId.class);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(tunPort);
+ when(tunDst.getIpv4Address()).thenReturn(null);
+ Ipv6Address ipv6Address = mock(Ipv6Address.class);
+ when(tunDst.getIpv6Address()).thenReturn(ipv6Address);
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeIdOther)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value:5");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertEquals(1, resultGroup.bucketMap.size());
+ BucketCtx result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[0];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ }
+
+ @Test
+ public void syncTestNodeEqualsFalseTunDstNull() throws Exception {
+ groupMap = new HashMap<>();
+
+ NodeId nodeIdOther = mock(NodeId.class);
+ when(nodeIdOther.getValue()).thenReturn("5");
+ SwitchManager switchManager = mock(SwitchManager.class);
+ when(ofContext.getSwitchManager()).thenReturn(switchManager);
+ when(switchManager.getTunnelIP(nodeIdOther, TunnelTypeVxlan.class)).thenReturn(null);
+ NodeConnectorId tunPort = mock(NodeConnectorId.class);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(tunPort);
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeIdOther)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value:5");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertEquals(1, resultGroup.bucketMap.size());
+ BucketCtx result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[0];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ }
+
+ @Test
+ public void syncTestNodeEqualsFalseTunPortNull() throws Exception {
+ groupMap = new HashMap<>();
+
+ NodeId nodeIdOther = mock(NodeId.class);
+ when(nodeIdOther.getValue()).thenReturn("5");
+ SwitchManager switchManager = mock(SwitchManager.class);
+ when(ofContext.getSwitchManager()).thenReturn(switchManager);
+ IpAddress tunDst = mock(IpAddress.class);
+ when(switchManager.getTunnelIP(nodeIdOther, TunnelTypeVxlan.class)).thenReturn(tunDst);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
+
+ when(endpointManager.getNodesForGroup(egKey)).thenReturn(new HashSet<NodeId>(Arrays.asList(nodeIdOther)));
+ when(ofc.getLocationType()).thenReturn(LocationType.Internal);
+ when(nodeConnectorId.getValue()).thenReturn("value:5");
+
+ groupTable.sync(nodeId, policyInfo, groupMap);
+ Assert.assertEquals(1, groupMap.size());
+ GroupCtx resultGroup = groupMap.values().toArray(new GroupCtx[0])[0];
+ Assert.assertEquals(1, resultGroup.bucketMap.size());
+ BucketCtx result = resultGroup.bucketMap.values().toArray(new BucketCtx[0])[0];
+ Assert.assertTrue(result.visited);
+ Assert.assertNotNull(result.newb);
+ }
+}
-/*\r
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;\r
-\r
-import com.google.common.collect.ImmutableList;\r
-import com.google.common.collect.ImmutableMap;\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;\r
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;\r
-import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;\r
-import org.opendaylight.groupbasedpolicy.resolver.EgKey;\r
-import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.classifier.refs.ClassifierRef;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Objects;\r
-\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertNotEquals;\r
-import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;\r
-import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;\r
-import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;\r
-\r
-public class PolicyEnforcerTest extends FlowTableTest {\r
- protected static final Logger LOG =\r
- LoggerFactory.getLogger(PolicyEnforcerTest.class);\r
-\r
- @Override\r
- @Before\r
- public void setup() throws Exception {\r
- initCtx();\r
- table = new PolicyEnforcer(ctx,ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER());\r
- super.setup();\r
-\r
- switchManager.addSwitch(\r
- nodeId,\r
- tunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .build())).build());\r
- }\r
-\r
- @Test\r
- public void testNoEps() throws Exception {\r
- FlowMap fm = dosync(null);\r
- assertEquals(2, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- }\r
-\r
- @Test\r
- public void testSameEg() throws Exception {\r
- Endpoint ep1 = localEP().build();\r
- endpointManager.addEndpoint(ep1);\r
- Endpoint ep2 = localEP()\r
- .setMacAddress(new MacAddress("00:00:00:00:00:02"))\r
- .build();\r
- endpointManager.addEndpoint(ep2);\r
- policyResolver.addTenant(baseTenant().setContract(\r
- ImmutableList.<Contract>of(baseContract(null).build())).build());\r
-\r
- FlowMap fm = dosync(null);\r
- assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- int count = 0;\r
- HashMap<String, Flow> flowMap = new HashMap<>();\r
- for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {\r
- flowMap.put(f.getId().getValue(), f);\r
- if (isAllowSameEpg(f)) {\r
- count += 1;\r
- }\r
- }\r
- assertEquals(1, count);\r
- assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- fm = dosync(flowMap);\r
- assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- }\r
-\r
- @Test\r
- public void testDifferentEg() throws Exception {\r
- assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(null).build())));\r
- assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Bidirectional).build())));\r
- assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.In).build())));\r
- assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Out).build())));\r
- }\r
-\r
- @Test\r
- public void doTestRule() throws Exception {\r
- Rule rule1 = new RuleBuilder().setActionRef(\r
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))\r
- .setClassifierRef(\r
- createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In,\r
- "tcp_src_80", Direction.In)))\r
- .build();\r
- Rule rule2 = new RuleBuilder().setActionRef(\r
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))\r
- .setClassifierRef(\r
- createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In,\r
- "tcp_src_80", Direction.Out)))\r
- .build();\r
- Rule rule3 = new RuleBuilder().setActionRef(\r
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))\r
- .setClassifierRef(\r
- createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In,\r
- "tcp_src_80", Direction.Out,\r
- "ether_type", Direction.In)))\r
- .build();\r
- Rule rule4 = new RuleBuilder().setActionRef(\r
- ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))\r
- .setClassifierRef(\r
- createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In,\r
- "tcp_dst_90", Direction.In)))\r
- .build();\r
-\r
- assertEquals(5,\r
- doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s1", ImmutableList.<Rule>of(rule1)))));\r
- assertEquals(7,\r
- doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s2", ImmutableList.<Rule>of(rule2)))));\r
- assertEquals(6,\r
- doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s3", ImmutableList.<Rule>of(rule3)))));\r
- assertEquals(3,\r
- doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s4", ImmutableList.<Rule>of(rule4)))));\r
- }\r
-\r
- private int doTestDifferentEg(List<Subject> subjects) throws Exception {\r
- Endpoint ep1 = localEP().build();\r
- endpointManager.addEndpoint(ep1);\r
- Endpoint ep2 = localEP()\r
- .setMacAddress(new MacAddress("00:00:00:00:00:02"))\r
- .setEndpointGroup(eg2)\r
- .build();\r
- endpointManager.addEndpoint(ep2);\r
- policyResolver.addTenant(baseTenant().setContract(\r
- ImmutableList.<Contract>of(baseContract(subjects).build())).build());\r
-\r
- FlowMap fm = dosync(null);\r
- assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- int count = 0;\r
- HashMap<String, Flow> flowMap = new HashMap<>();\r
- for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {\r
- flowMap.put(f.getId().getValue(), f);\r
- if (isAllowSameEpg(f)) {\r
- count += 1;\r
- } else if (f.getMatch() != null &&\r
- Objects.equals(tunnelId, f.getMatch().getInPort())) {\r
- assertEquals(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))),\r
- f.getInstructions());\r
- count += 1;\r
- } else if (f.getMatch() != null &&\r
- f.getMatch().getEthernetMatch() != null &&\r
- Objects.equals(FlowUtils.IPv4,\r
- f.getMatch().getEthernetMatch()\r
- .getEthernetType().getType().getValue()) &&\r
- f.getMatch().getIpMatch() != null &&\r
- Objects.equals(Short.valueOf((short)6),\r
- f.getMatch().getIpMatch().getIpProtocol()) &&\r
- f.getMatch().getLayer4Match() != null &&\r
- (\r
- Objects.equals(new PortNumber(Integer.valueOf(80)),\r
- ((TcpMatch)f.getMatch().getLayer4Match())\r
- .getTcpSourcePort())\r
- ||\r
- Objects.equals(new PortNumber(Integer.valueOf(80)),\r
- ((TcpMatch)f.getMatch().getLayer4Match())\r
- .getTcpDestinationPort())\r
- )) {\r
- count += 1;\r
- } else if (f.getMatch() != null &&\r
- f.getMatch().getEthernetMatch() != null &&\r
- Objects.equals(FlowUtils.IPv6,\r
- f.getMatch().getEthernetMatch()\r
- .getEthernetType().getType().getValue()) &&\r
- f.getMatch().getIpMatch() != null &&\r
- Objects.equals(Short.valueOf((short)6),\r
- f.getMatch().getIpMatch().getIpProtocol()) &&\r
- f.getMatch().getLayer4Match() != null &&\r
- (\r
- Objects.equals(new PortNumber(Integer.valueOf(80)),\r
- ((TcpMatch)f.getMatch().getLayer4Match())\r
- .getTcpSourcePort())\r
- ||\r
- Objects.equals(new PortNumber(Integer.valueOf(80)),\r
- ((TcpMatch)f.getMatch().getLayer4Match())\r
- .getTcpDestinationPort())\r
- )) {\r
- count += 1;\r
- }\r
- }\r
- return count;\r
- }\r
-\r
- @Test\r
- public void testConditions() throws Exception {\r
- Condition cond1 = new ConditionBuilder()\r
- .setName(new ConditionName("cond1"))\r
- .build();\r
- Condition cond2 = new ConditionBuilder()\r
- .setName(new ConditionName("cond2"))\r
- .build();\r
-\r
- Endpoint ep1 = localEP()\r
- .setCondition(ImmutableList.of(cond1.getName()))\r
- .build();\r
- endpointManager.addEndpoint(ep1);\r
- Endpoint ep2 = localEP()\r
- .setMacAddress(new MacAddress("00:00:00:00:00:02"))\r
- .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))\r
- .setEndpointGroup(eg2)\r
- .build();\r
- endpointManager.addEndpoint(ep2);\r
-\r
- TenantBuilder tb = baseTenant()\r
- .setContract(ImmutableList.of(new ContractBuilder()\r
- .setId(cid)\r
- .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))\r
- .setClause(ImmutableList.of(new ClauseBuilder()\r
- .setName(new ClauseName("test"))\r
- .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))\r
- .setConsumerMatchers(new ConsumerMatchersBuilder()\r
- .setConditionMatcher(ImmutableList.of(new ConditionMatcherBuilder()\r
- .setName(new ConditionMatcherName("m1"))\r
- .setCondition(ImmutableList.of(cond1, cond2))\r
- .setMatchType(MatchType.Any)\r
- .build()))\r
- .build())\r
- .setProviderMatchers(new ProviderMatchersBuilder()\r
- .setConditionMatcher(ImmutableList.of(new ConditionMatcherBuilder()\r
- .setName(new ConditionMatcherName("m2"))\r
- .setCondition(ImmutableList.of(cond1, cond2))\r
- .setMatchType(MatchType.All)\r
- .build()))\r
- .build())\r
- .build()))\r
- .build()));\r
- policyResolver.addTenant(tb.build());\r
-\r
- PolicyInfo policy = policyResolver.getCurrentPolicy();\r
- List<ConditionName> ep1c = endpointManager.getCondsForEndpoint(ep1);\r
- ConditionGroup cg1 =\r
- policy.getEgCondGroup(new EgKey(tb.getId(),\r
- ep1.getEndpointGroup()),\r
- ep1c);\r
- List<ConditionName> ep2c = endpointManager.getCondsForEndpoint(ep2);\r
- ConditionGroup cg2 =\r
- policy.getEgCondGroup(new EgKey(tb.getId(),\r
- ep2.getEndpointGroup()),\r
- ep2c);\r
- int cg1Id = OrdinalFactory.getCondGroupOrdinal(cg1);\r
- int cg2Id = OrdinalFactory.getCondGroupOrdinal(cg2);\r
- int eg1Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(),\r
- ep1.getEndpointGroup());\r
- int eg2Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(),\r
- ep2.getEndpointGroup());\r
-\r
- assertNotEquals(cg1Id, cg2Id);\r
-\r
- MatchBuilder mb = new MatchBuilder();\r
- FlowUtils.addNxRegMatch(mb,\r
- RegMatch.of(NxmNxReg0.class, Long.valueOf(eg1Id)),\r
- RegMatch.of(NxmNxReg1.class, Long.valueOf(cg1Id)),\r
- RegMatch.of(NxmNxReg2.class, Long.valueOf(eg2Id)),\r
- RegMatch.of(NxmNxReg3.class, Long.valueOf(cg2Id)));\r
- GeneralAugMatchNodesNodeTableFlow m1 =\r
- mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);\r
- FlowUtils.addNxRegMatch(mb,\r
- RegMatch.of(NxmNxReg0.class, Long.valueOf(eg2Id)),\r
- RegMatch.of(NxmNxReg1.class, Long.valueOf(cg2Id)),\r
- RegMatch.of(NxmNxReg2.class, Long.valueOf(eg1Id)),\r
- RegMatch.of(NxmNxReg3.class, Long.valueOf(cg1Id)));\r
- GeneralAugMatchNodesNodeTableFlow m2 =\r
- mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);\r
- int count = 0;\r
- FlowMap fm = dosync(null);\r
- assertEquals(7, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- HashMap<String, Flow> flowMap = new HashMap<>();\r
- for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {\r
- flowMap.put(f.getId().getValue(), f);\r
- if (f.getMatch() != null &&\r
- f.getMatch().getEthernetMatch() != null) {\r
- count++;\r
- }\r
- }\r
- assertEquals(3, count);\r
- fm = dosync(flowMap);\r
- int numberOfFlows = fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size();\r
- fm = dosync(flowMap);\r
- assertEquals(numberOfFlows, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow().size());\r
- }\r
-\r
- private boolean isAllowSameEpg(Flow flow) {\r
- // flow has to have exactly 2 registers set, namely NxmNxReg0 and NxmNxReg2\r
- // (these register values don't have to be equal)\r
- boolean res = false;\r
- if (flow != null && flow.getMatch() != null) {\r
- GeneralAugMatchNodesNodeTableFlow genAug =\r
- flow.getMatch().getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);\r
- if (genAug != null) {\r
- List<ExtensionList> extensions = genAug.getExtensionList();\r
- if (extensions != null && extensions.size() == 2) {\r
- Long reg0 = null;\r
- Long reg2 = null;\r
- for (ExtensionList extensionList : extensions) {\r
- Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();\r
- Extension extension = extensionList.getExtension();\r
- if (extensionKey != null && extension != null) {\r
- NxAugMatchNodesNodeTableFlow nxAugMatch =\r
- extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);\r
- if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {\r
- if (extensionKey.equals(NxmNxReg0Key.class)) {\r
- reg0 = nxAugMatch.getNxmNxReg().getValue();\r
- } else if (extensionKey.equals(NxmNxReg2Key.class)) {\r
- reg2 = nxAugMatch.getNxmNxReg().getValue();\r
- }\r
- }\r
- }\r
- }\r
- if (reg0 != null && reg2 != null) {\r
- res = true;\r
- }\r
- }\r
- }\r
- }\r
- return res;\r
- }\r
-}\r
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.applyActionIns;
+import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.instructions;
+import static org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.nxOutputRegAction;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Objects;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.FlowUtils.RegMatch;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow.PolicyEnforcer.PolicyPair;
+import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;
+import org.opendaylight.groupbasedpolicy.resolver.EgKey;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
+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.IpPrefix;
+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.inet.types.rev100924.PortNumber;
+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.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClauseName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionMatcherName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.Matcher.MatchType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.action.refs.ActionRefBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.condition.matchers.ConditionMatcherBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.Condition;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.conditions.ConditionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.TenantBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.ContractBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.ClauseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.Subject;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ConsumerMatchersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.clause.ProviderMatchersBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.Rule;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.contract.subject.RuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg2;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg7;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.GeneralAugMatchNodesNodeTableFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.list.grouping.ExtensionList;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+public class PolicyEnforcerTest extends FlowTableTest {
+
+ protected static final Logger LOG = LoggerFactory.getLogger(PolicyEnforcerTest.class);
+
+ @Override
+ @Before
+ public void setup() throws Exception {
+ initCtx();
+ table = new PolicyEnforcer(ctx, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER());
+ super.setup();
+
+ switchManager.addSwitch(
+ nodeId,
+ tunnelId,
+ Collections.<NodeConnectorId>emptySet(),
+ new OfOverlayNodeConfigBuilder().setTunnel(
+ ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))
+ .setTunnelType(TunnelTypeVxlan.class)
+ .setNodeConnectorId(tunnelId)
+ .build())).build());
+ }
+
+ @Test
+ public void testNoEps() throws Exception {
+ FlowMap fm = dosync(null);
+ assertEquals(2, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ }
+
+ @Test
+ public void testSameEg() throws Exception {
+ Endpoint ep1 = localEP().build();
+ endpointManager.addEndpoint(ep1);
+ Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02")).build();
+ endpointManager.addEndpoint(ep2);
+ policyResolver.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(null).build()))
+ .build());
+
+ FlowMap fm = dosync(null);
+ assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ int count = 0;
+ HashMap<String, Flow> flowMap = new HashMap<>();
+ for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
+ flowMap.put(f.getId().getValue(), f);
+ if (isAllowSameEpg(f)) {
+ count += 1;
+ }
+ }
+ assertEquals(1, count);
+ assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ fm = dosync(flowMap);
+ assertEquals(3, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ }
+
+ @Test
+ public void testDifferentEg() throws Exception {
+ assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(null).build())));
+ assertEquals(7, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Bidirectional).build())));
+ assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.In).build())));
+ assertEquals(5, doTestDifferentEg(ImmutableList.<Subject>of(baseSubject(Direction.Out).build())));
+ }
+
+ @Test
+ public void doTestRule() throws Exception {
+ Rule rule1 = new RuleBuilder().setActionRef(
+ ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
+ .setClassifierRef(
+ createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
+ Direction.In)))
+ .build();
+ Rule rule2 = new RuleBuilder().setActionRef(
+ ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
+ .setClassifierRef(
+ createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
+ Direction.Out)))
+ .build();
+ Rule rule3 = new RuleBuilder().setActionRef(
+ ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
+ .setClassifierRef(
+ createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_src_80",
+ Direction.Out, "ether_type", Direction.In)))
+ .build();
+ Rule rule4 = new RuleBuilder().setActionRef(
+ ImmutableList.of(new ActionRefBuilder().setName(new ActionName("allow")).build()))
+ .setClassifierRef(
+ createClassifierRefs(ImmutableMap.<String, Direction>of("tcp_dst_80", Direction.In, "tcp_dst_90",
+ Direction.In)))
+ .build();
+
+ assertEquals(5,
+ doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s1", ImmutableList.<Rule>of(rule1)))));
+ assertEquals(7,
+ doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s2", ImmutableList.<Rule>of(rule2)))));
+ assertEquals(6,
+ doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s3", ImmutableList.<Rule>of(rule3)))));
+ assertEquals(3,
+ doTestDifferentEg(ImmutableList.<Subject>of(createSubject("s4", ImmutableList.<Rule>of(rule4)))));
+ }
+
+ private int doTestDifferentEg(List<Subject> subjects) throws Exception {
+ Endpoint ep1 = localEP().build();
+ endpointManager.addEndpoint(ep1);
+ Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02")).setEndpointGroup(eg2).build();
+ endpointManager.addEndpoint(ep2);
+ policyResolver.addTenant(baseTenant().setContract(ImmutableList.<Contract>of(baseContract(subjects).build()))
+ .build());
+
+ FlowMap fm = dosync(null);
+ assertNotEquals(0, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ int count = 0;
+ HashMap<String, Flow> flowMap = new HashMap<>();
+ for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
+ flowMap.put(f.getId().getValue(), f);
+ if (isAllowSameEpg(f)) {
+ count += 1;
+ } else if (f.getMatch() != null && Objects.equals(tunnelId, f.getMatch().getInPort())) {
+ assertEquals(instructions(applyActionIns(nxOutputRegAction(NxmNxReg7.class))), f.getInstructions());
+ count += 1;
+ } else if (f.getMatch() != null
+ && f.getMatch().getEthernetMatch() != null
+ && Objects.equals(FlowUtils.IPv4, f.getMatch()
+ .getEthernetMatch()
+ .getEthernetType()
+ .getType()
+ .getValue())
+ && f.getMatch().getIpMatch() != null
+ && Objects.equals(Short.valueOf((short) 6), f.getMatch().getIpMatch().getIpProtocol())
+ && f.getMatch().getLayer4Match() != null
+ && (Objects.equals(new PortNumber(Integer.valueOf(80)),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
+ new PortNumber(Integer.valueOf(80)),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
+ count += 1;
+ } else if (f.getMatch() != null
+ && f.getMatch().getEthernetMatch() != null
+ && Objects.equals(FlowUtils.IPv6, f.getMatch()
+ .getEthernetMatch()
+ .getEthernetType()
+ .getType()
+ .getValue())
+ && f.getMatch().getIpMatch() != null
+ && Objects.equals(Short.valueOf((short) 6), f.getMatch().getIpMatch().getIpProtocol())
+ && f.getMatch().getLayer4Match() != null
+ && (Objects.equals(new PortNumber(Integer.valueOf(80)),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpSourcePort()) || Objects.equals(
+ new PortNumber(Integer.valueOf(80)),
+ ((TcpMatch) f.getMatch().getLayer4Match()).getTcpDestinationPort()))) {
+ count += 1;
+ }
+ }
+ return count;
+ }
+
+ @Test
+ public void testConditions() throws Exception {
+ Condition cond1 = new ConditionBuilder().setName(new ConditionName("cond1")).build();
+ Condition cond2 = new ConditionBuilder().setName(new ConditionName("cond2")).build();
+
+ Endpoint ep1 = localEP().setCondition(ImmutableList.of(cond1.getName())).build();
+ endpointManager.addEndpoint(ep1);
+ Endpoint ep2 = localEP().setMacAddress(new MacAddress("00:00:00:00:00:02"))
+ .setCondition(ImmutableList.of(cond1.getName(), cond2.getName()))
+ .setEndpointGroup(eg2)
+ .build();
+ endpointManager.addEndpoint(ep2);
+
+ TenantBuilder tb = baseTenant().setContract(
+ ImmutableList.of(new ContractBuilder().setId(cid)
+ .setSubject(ImmutableList.of(baseSubject(Direction.Out).build()))
+ .setClause(
+ ImmutableList.of(new ClauseBuilder().setName(new ClauseName("test"))
+ .setSubjectRefs(ImmutableList.of(new SubjectName("s1")))
+ .setConsumerMatchers(
+ new ConsumerMatchersBuilder().setConditionMatcher(
+ ImmutableList.of(new ConditionMatcherBuilder().setName(
+ new ConditionMatcherName("m1"))
+ .setCondition(ImmutableList.of(cond1, cond2))
+ .setMatchType(MatchType.Any)
+ .build())).build())
+ .setProviderMatchers(
+ new ProviderMatchersBuilder().setConditionMatcher(
+ ImmutableList.of(new ConditionMatcherBuilder().setName(
+ new ConditionMatcherName("m2"))
+ .setCondition(ImmutableList.of(cond1, cond2))
+ .setMatchType(MatchType.All)
+ .build())).build())
+ .build()))
+ .build()));
+ policyResolver.addTenant(tb.build());
+
+ PolicyInfo policy = policyResolver.getCurrentPolicy();
+ List<ConditionName> ep1c = endpointManager.getCondsForEndpoint(ep1);
+ ConditionGroup cg1 = policy.getEgCondGroup(new EgKey(tb.getId(), ep1.getEndpointGroup()), ep1c);
+ List<ConditionName> ep2c = endpointManager.getCondsForEndpoint(ep2);
+ ConditionGroup cg2 = policy.getEgCondGroup(new EgKey(tb.getId(), ep2.getEndpointGroup()), ep2c);
+ int cg1Id = OrdinalFactory.getCondGroupOrdinal(cg1);
+ int cg2Id = OrdinalFactory.getCondGroupOrdinal(cg2);
+ int eg1Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep1.getEndpointGroup());
+ int eg2Id = OrdinalFactory.getContextOrdinal(ep1.getTenant(), ep2.getEndpointGroup());
+
+ assertNotEquals(cg1Id, cg2Id);
+
+ MatchBuilder mb = new MatchBuilder();
+ FlowUtils.addNxRegMatch(mb, RegMatch.of(NxmNxReg0.class, Long.valueOf(eg1Id)),
+ RegMatch.of(NxmNxReg1.class, Long.valueOf(cg1Id)), RegMatch.of(NxmNxReg2.class, Long.valueOf(eg2Id)),
+ RegMatch.of(NxmNxReg3.class, Long.valueOf(cg2Id)));
+ GeneralAugMatchNodesNodeTableFlow m1 = mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
+ FlowUtils.addNxRegMatch(mb, RegMatch.of(NxmNxReg0.class, Long.valueOf(eg2Id)),
+ RegMatch.of(NxmNxReg1.class, Long.valueOf(cg2Id)), RegMatch.of(NxmNxReg2.class, Long.valueOf(eg1Id)),
+ RegMatch.of(NxmNxReg3.class, Long.valueOf(cg1Id)));
+ GeneralAugMatchNodesNodeTableFlow m2 = mb.getAugmentation(GeneralAugMatchNodesNodeTableFlow.class);
+ int count = 0;
+ FlowMap fm = dosync(null);
+ assertEquals(7, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ HashMap<String, Flow> flowMap = new HashMap<>();
+ for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER()).getFlow()) {
+ flowMap.put(f.getId().getValue(), f);
+ if (f.getMatch() != null && f.getMatch().getEthernetMatch() != null) {
+ count++;
+ }
+ }
+ assertEquals(3, count);
+ fm = dosync(flowMap);
+ int numberOfFlows = fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size();
+ fm = dosync(flowMap);
+ assertEquals(numberOfFlows, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_POLICY_ENFORCER())
+ .getFlow()
+ .size());
+ }
+
+ private boolean isAllowSameEpg(Flow flow) {
+ // flow has to have exactly 2 registers set, namely NxmNxReg0 and NxmNxReg2
+ // (these register values don't have to be equal)
+ boolean res = false;
+ if (flow != null && flow.getMatch() != null) {
+ GeneralAugMatchNodesNodeTableFlow genAug = flow.getMatch().getAugmentation(
+ GeneralAugMatchNodesNodeTableFlow.class);
+ if (genAug != null) {
+ List<ExtensionList> extensions = genAug.getExtensionList();
+ if (extensions != null && extensions.size() == 2) {
+ Long reg0 = null;
+ Long reg2 = null;
+ for (ExtensionList extensionList : extensions) {
+ Class<? extends ExtensionKey> extensionKey = extensionList.getExtensionKey();
+ Extension extension = extensionList.getExtension();
+ if (extensionKey != null && extension != null) {
+ NxAugMatchNodesNodeTableFlow nxAugMatch = extension.getAugmentation(NxAugMatchNodesNodeTableFlow.class);
+ if (nxAugMatch != null && nxAugMatch.getNxmNxReg() != null) {
+ if (extensionKey.equals(NxmNxReg0Key.class)) {
+ reg0 = nxAugMatch.getNxmNxReg().getValue();
+ } else if (extensionKey.equals(NxmNxReg2Key.class)) {
+ reg2 = nxAugMatch.getNxmNxReg().getValue();
+ }
+ }
+ }
+ }
+ if (reg0 != null && reg2 != null) {
+ res = true;
+ }
+ }
+ }
+ }
+ return res;
+ }
+
+ PolicyPair policyPair;
+ int consumerEpgId;
+ int providerEpgId;
+ int consumerCondGrpId;
+ int providerCondGrpId;
+ Set<IpPrefix> consumerEicIpPrefixes;
+ Set<IpPrefix> providerEicIpPrefixes;
+ IpPrefix consumerEicIp;
+ IpPrefix providerEicIp;
+ NodeId consumerEpNodeId;
+ NodeId providerEpNodeId;
+
+ @Before
+ public void PolicyPairInitialisation() {
+ consumerEpgId = 5;
+ providerEpgId = 8;
+ consumerCondGrpId = 5;
+ providerCondGrpId = 8;
+ consumerEicIp = mock(IpPrefix.class);
+ providerEicIp = mock(IpPrefix.class);
+ consumerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(consumerEicIp));
+ providerEicIpPrefixes = new HashSet<IpPrefix>(Arrays.asList(providerEicIp));
+ consumerEpNodeId = mock(NodeId.class);
+ when(consumerEpNodeId.getValue()).thenReturn("consumerValue");
+ providerEpNodeId = mock(NodeId.class);
+ when(providerEpNodeId.getValue()).thenReturn("providerValue");
+ }
+
+ @Test
+ public void PolicyPairConstructorTest() {
+ policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
+ consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
+ Assert.assertEquals(consumerEpgId, policyPair.getConsumerEpgId());
+ Assert.assertEquals(providerEpgId, policyPair.getProviderEpgId());
+ Assert.assertEquals(consumerEpNodeId, policyPair.getConsumerEpNodeId());
+ Assert.assertEquals(providerEpNodeId, policyPair.getProviderEpNodeId());
+ Assert.assertNotNull(policyPair.toString());
+ }
+
+ @Test
+ public void PolicyPairEqualsTest() {
+ policyPair = new PolicyPair(0, 0, 0, 0, null, null, null, null);
+ PolicyPair other;
+ other = new PolicyPair(0, 0, 0, 0, null, null, null, null);
+ Assert.assertTrue(policyPair.equals(other));
+
+ policyPair = new PolicyPair(consumerEpgId, providerEpgId, consumerCondGrpId, providerCondGrpId,
+ consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, providerEpNodeId);
+ Assert.assertTrue(policyPair.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(null));
+ Assert.assertFalse(policyPair.equals(new Object()));
+
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 0, 0, 0, null, providerEicIpPrefixes, null, null);
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, null, null);
+ Assert.assertFalse(other.equals(policyPair));
+
+ other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, providerEpNodeId, null);
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId, null);
+ Assert.assertFalse(other.equals(policyPair));
+
+ other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ consumerEpNodeId);
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 0, 0, 0, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ providerEpNodeId);
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 0, 0, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ providerEpNodeId);
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 8, 0, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ providerEpNodeId);
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(0, 8, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ providerEpNodeId);
+ Assert.assertFalse(other.equals(policyPair));
+ Assert.assertFalse(policyPair.equals(other));
+
+ other = new PolicyPair(5, 8, 5, 8, consumerEicIpPrefixes, providerEicIpPrefixes, consumerEpNodeId,
+ providerEpNodeId);
+ Assert.assertTrue(policyPair.equals(other));
+ }
+}
-/*\r
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.groupbasedpolicy.renderer.ofoverlay.flow;\r
-\r
-import java.math.BigInteger;\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Objects;\r
-\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;\r
-import org.opendaylight.groupbasedpolicy.resolver.ConditionGroup;\r
-import org.opendaylight.groupbasedpolicy.resolver.EgKey;\r
-import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;\r
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayNodeConfigBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.nodes.node.TunnelBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg1;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg4;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg5;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg6;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-import com.google.common.collect.ImmutableList;\r
-\r
-import static org.junit.Assert.*;\r
-\r
-public class SourceMapperTest extends FlowTableTest {\r
-\r
- protected static final Logger LOG = LoggerFactory.getLogger(SourceMapperTest.class);\r
-\r
- NodeConnectorId remoteTunnelId =\r
- new NodeConnectorId(remoteNodeId.getValue() + ":101");\r
-\r
- @Override\r
- @Before\r
- public void setup() throws Exception {\r
- initCtx();\r
- table = new SourceMapper(ctx,ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER());\r
- super.setup();\r
- }\r
-\r
- private void addSwitches() {\r
- switchManager.addSwitch(\r
- nodeId,\r
- tunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .build())).build());\r
- switchManager.addSwitch(\r
- remoteNodeId,\r
- remoteTunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.5")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .build())).build());\r
- }\r
-\r
- @Test\r
- public void testNoPolicy() throws Exception {\r
- endpointManager.addEndpoint(localEP().build());\r
- FlowMap fm = dosync(null);\r
- assertEquals(1, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()).getFlow().size());\r
- }\r
-\r
- @Test\r
- public void testMap() throws Exception {\r
- switchManager.addSwitch(\r
- nodeId,\r
- tunnelId,\r
- Collections.<NodeConnectorId>emptySet(),\r
- new OfOverlayNodeConfigBuilder().setTunnel(\r
- ImmutableList.of(new TunnelBuilder().setIp(new IpAddress(new Ipv4Address("1.2.3.4")))\r
- .setTunnelType(TunnelTypeVxlan.class)\r
- .setNodeConnectorId(tunnelId)\r
- .build())).build());\r
- Endpoint ep = localEP().build();\r
- switchManager.addSwitch(nodeId, null,\r
- Collections.<NodeConnectorId> emptySet(),\r
- null);\r
- endpointManager.addEndpoint(ep);\r
- policyResolver.addTenant(baseTenant().build());\r
-\r
- FlowMap fm = dosync(null);\r
- assertEquals(2, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()).getFlow().size());\r
-\r
- int count = 0;\r
- HashMap<String, Flow> flowMap = new HashMap<>();\r
- for (Flow f : fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()).getFlow()) {\r
- flowMap.put(f.getId().getValue(), f);\r
- if (f.getMatch() == null || f.getMatch().getEthernetMatch() == null) {\r
- assertEquals(FlowUtils.dropInstructions(), f.getInstructions());\r
- count += 1;\r
- } else if ((f.getMatch() !=null && f.getMatch().getEthernetMatch() != null)\r
- && (Objects.equals(ep.getMacAddress(), f.getMatch()\r
- .getEthernetMatch()\r
- .getEthernetSource()\r
- .getAddress()))) {\r
- PolicyInfo pi = policyResolver.getCurrentPolicy();\r
- List<ConditionName> cset = endpointManager.getCondsForEndpoint(ep);\r
- ConditionGroup cg = pi.getEgCondGroup(new EgKey(tid, eg), cset);\r
-\r
- Instruction ins = f.getInstructions().getInstruction().get(0);\r
- assertTrue(ins.getInstruction() instanceof ApplyActionsCase);\r
- List<Action> actions = ((ApplyActionsCase) ins.getInstruction()).getApplyActions().getAction();\r
- int v = OrdinalFactory.getContextOrdinal(ep);\r
- assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg0.class, BigInteger.valueOf(v)), actions.get(0)\r
- .getAction());\r
- v = OrdinalFactory.getCondGroupOrdinal(cg);\r
- assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg1.class, BigInteger.valueOf(v)), actions.get(1)\r
- .getAction());\r
- v = OrdinalFactory.getContextOrdinal(tid, bd);\r
- assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg4.class, BigInteger.valueOf(v)), actions.get(2)\r
- .getAction());\r
- v = OrdinalFactory.getContextOrdinal(tid, fd);\r
- assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg5.class, BigInteger.valueOf(v)), actions.get(3)\r
- .getAction());\r
- v = OrdinalFactory.getContextOrdinal(tid, l3c);\r
- assertEquals(FlowUtils.nxLoadRegAction(NxmNxReg6.class, BigInteger.valueOf(v)), actions.get(4)\r
- .getAction());\r
- count += 1;\r
- }\r
- }\r
- assertEquals(2, count);\r
- int numberOfFlows = fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()).getFlow().size();\r
- fm = dosync(flowMap);\r
- assertEquals(numberOfFlows, fm.getTableForNode(nodeId, ctx.getPolicyManager().getTABLEID_SOURCE_MAPPER()).getFlow().size());\r
- }\r
-\r
-}\r
+/*
+ * Copyright (c) 2015 Cisco Systems, 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.groupbasedpolicy.renderer.ofoverlay.flow;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.EndpointManager;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.OfContext;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.PolicyManager.FlowMap;
+import org.opendaylight.groupbasedpolicy.renderer.ofoverlay.node.SwitchManager;
+import org.opendaylight.groupbasedpolicy.resolver.EgKey;
+import org.opendaylight.groupbasedpolicy.resolver.IndexedTenant;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyInfo;
+import org.opendaylight.groupbasedpolicy.resolver.PolicyResolver;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.EndpointLocation.LocationType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.ofoverlay.rev140528.OfOverlayContext;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.EndpointGroup;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.overlay.rev150105.TunnelTypeVxlan;
+
+public class SourceMapperTest {
+
+ private SourceMapper mapper;
+
+ private OfContext ctx;
+ private short tableId;
+ private NodeId nodeId;
+ private PolicyInfo policyInfo;
+ private FlowMap flowMap;
+ private Endpoint endpoint;
+ private EndpointManager endpointManager;
+ private IndexedTenant tenant;
+ private PolicyResolver policyResolver;
+ private TenantId tenantId;
+ private PolicyManager policyManager;
+ private OfOverlayContext ofOverlayContext;
+ private NodeConnectorId nodeConnectorId;
+ private EndpointGroupId endpointGroupIdSingle;
+ private EndpointGroupId endpointGroupIdList;
+ private EndpointGroup endpointGroup;
+ private SwitchManager switchManager;
+
+ @Before
+ public void initialisation() {
+ ctx = mock(OfContext.class);
+ tableId = 5;
+ nodeId = mock(NodeId.class);
+ policyInfo = mock(PolicyInfo.class);
+ flowMap = mock(FlowMap.class);
+
+ mapper = new SourceMapper(ctx, tableId);
+
+ endpointManager = mock(EndpointManager.class);
+ when(ctx.getEndpointManager()).thenReturn(endpointManager);
+ endpoint = mock(Endpoint.class);
+ List<Endpoint> endpointsForNode = Arrays.asList(endpoint);
+ when(endpointManager.getEndpointsForNode(nodeId)).thenReturn(endpointsForNode);
+
+ ofOverlayContext = mock(OfOverlayContext.class);
+ when(endpoint.getAugmentation(OfOverlayContext.class)).thenReturn(ofOverlayContext);
+ nodeConnectorId = mock(NodeConnectorId.class);
+ when(ofOverlayContext.getNodeConnectorId()).thenReturn(nodeConnectorId);
+ when(ofOverlayContext.getLocationType()).thenReturn(LocationType.Internal);
+
+ tenantId = mock(TenantId.class);
+ when(endpoint.getTenant()).thenReturn(tenantId);
+ policyResolver = mock(PolicyResolver.class);
+ when(ctx.getPolicyResolver()).thenReturn(policyResolver);
+ tenant = mock(IndexedTenant.class);
+ when(policyResolver.getTenant(tenantId)).thenReturn(tenant);
+ policyManager = mock(PolicyManager.class);
+ when(ctx.getPolicyManager()).thenReturn(policyManager);
+
+ endpointGroup = mock(EndpointGroup.class);
+ when(tenant.getEndpointGroup(any(EndpointGroupId.class))).thenReturn(endpointGroup);
+
+ Set<NodeId> nodeIdPeers = new HashSet<NodeId>(Arrays.asList(nodeId));
+ when(endpointManager.getNodesForGroup(any(EgKey.class))).thenReturn(nodeIdPeers);
+
+ switchManager = mock(SwitchManager.class);
+ when(ctx.getSwitchManager()).thenReturn(switchManager);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(nodeConnectorId);
+ }
+
+ @Test
+ public void constructorTest() {
+ Assert.assertEquals(tableId, mapper.getTableId());
+ }
+
+ @Test
+ public void syncTestEndpointGroup() throws Exception {
+ endpointGroupIdSingle = mock(EndpointGroupId.class);
+ when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
+ when(endpoint.getEndpointGroups()).thenReturn(null);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+
+ @Test
+ public void syncTestEndpointGroups() throws Exception {
+ endpointGroupIdList = mock(EndpointGroupId.class);
+ List<EndpointGroupId> endpointGroups = Arrays.asList(endpointGroupIdList);
+ when(endpoint.getEndpointGroups()).thenReturn(endpointGroups);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+
+ @Test
+ public void syncTestEndpointGroupPeers() throws Exception {
+ endpointGroupIdSingle = mock(EndpointGroupId.class);
+ when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
+ when(endpoint.getEndpointGroups()).thenReturn(null);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(4)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+
+ @Test
+ public void syncTestEndpointGroupTunPortNull() throws Exception {
+ endpointGroupIdSingle = mock(EndpointGroupId.class);
+ when(endpoint.getEndpointGroup()).thenReturn(endpointGroupIdSingle);
+ when(endpoint.getEndpointGroups()).thenReturn(null);
+ when(switchManager.getTunnelPort(nodeId, TunnelTypeVxlan.class)).thenReturn(null);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(2)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+
+ @Test
+ public void syncTestTenantNull() throws Exception {
+ when(policyResolver.getTenant(tenantId)).thenReturn(null);
+
+ mapper.sync(nodeId, policyInfo, flowMap);
+ verify(flowMap, times(1)).writeFlow(any(NodeId.class), any(Short.class), any(Flow.class));
+ }
+}