Support for SNAT and DNAT features in L3 forwarding services.
[vpnservice.git] / natservice / natservice-impl / src / test / java / org / opendaylight / vpnservice / natservice / internal / test / ExternalNetworksChangeListenerTest.java
diff --git a/natservice/natservice-impl/src/test/java/org/opendaylight/vpnservice/natservice/internal/test/ExternalNetworksChangeListenerTest.java b/natservice/natservice-impl/src/test/java/org/opendaylight/vpnservice/natservice/internal/test/ExternalNetworksChangeListenerTest.java
new file mode 100644 (file)
index 0000000..a284db1
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. 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.vpnservice.natservice.internal.test;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.mock;
+
+import java.math.BigInteger;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.vpnservice.mdsalutil.ActionInfo;
+import org.opendaylight.vpnservice.mdsalutil.ActionType;
+import org.opendaylight.vpnservice.mdsalutil.BucketInfo;
+import org.opendaylight.vpnservice.mdsalutil.FlowEntity;
+import org.opendaylight.vpnservice.mdsalutil.GroupEntity;
+import org.opendaylight.vpnservice.mdsalutil.InstructionInfo;
+import org.opendaylight.vpnservice.mdsalutil.InstructionType;
+import org.opendaylight.vpnservice.mdsalutil.MDSALUtil;
+import org.opendaylight.vpnservice.mdsalutil.MatchFieldType;
+import org.opendaylight.vpnservice.mdsalutil.MatchInfo;
+import org.opendaylight.vpnservice.mdsalutil.interfaces.IMdsalApiManager;
+import org.opendaylight.vpnservice.natservice.internal.ExternalNetworksChangeListener;
+import org.opendaylight.vpnservice.natservice.internal.NatUtil;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(MDSALUtil.class)
+public class ExternalNetworksChangeListenerTest {
+
+    @Mock DataBroker dataBroker;
+    @Mock ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
+    @Mock IMdsalApiManager mdsalManager;
+    @Mock FlowEntity flowMock;
+    @Mock GroupEntity groupMock;
+    InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.external.networks.Networks> id = null;
+    org.opendaylight.yang.gen.v1.urn.opendaylight.vpnservice.natservice.rev160111.external.networks.Networks networks = null;
+    private ExternalNetworksChangeListener extNetworks;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        when(dataBroker.registerDataChangeListener(
+                any(LogicalDatastoreType.class),
+                any(InstanceIdentifier.class),
+                any(DataChangeListener.class),
+                any(DataChangeScope.class)))
+                .thenReturn(dataChangeListenerRegistration);
+        extNetworks = new ExternalNetworksChangeListener(dataBroker);
+
+        PowerMockito.mockStatic(MDSALUtil.class);
+    }
+
+
+    @Test
+    public void testSnatFlowEntity() {
+        FlowEntity flowMock = mock(FlowEntity.class);
+        final short SNAT_TABLE = 40;
+        final int DEFAULT_SNAT_FLOW_PRIORITY = 0;
+        final String FLOWID_SEPARATOR = ".";
+        String SNAT_FLOWID_PREFIX = "SNAT.";
+
+
+        BigInteger dpnId = new BigInteger("100");
+        String routerName = new String("200");
+        long routerId = 200;
+        long groupId = 300;
+        List<BucketInfo> bucketInfo = new ArrayList<BucketInfo>();
+        List<ActionInfo> listActionInfoPrimary = new ArrayList<ActionInfo>();
+        listActionInfoPrimary.add(new ActionInfo(ActionType.output,
+                new String[] {"3"}));
+        BucketInfo bucketPrimary = new BucketInfo(listActionInfoPrimary);
+        List<ActionInfo> listActionInfoSecondary = new ArrayList<ActionInfo>();
+        listActionInfoSecondary.add(new ActionInfo(ActionType.output,
+                new String[] {"4"}));
+        BucketInfo bucketSecondary = new BucketInfo(listActionInfoPrimary);
+        bucketInfo.add(0, bucketPrimary);
+        bucketInfo.add(1, bucketSecondary);
+
+        List<MatchInfo> matches = new ArrayList<MatchInfo>();
+        matches.add(new MatchInfo(MatchFieldType.eth_type,
+                new long[] { 0x0800L }));
+
+        List<InstructionInfo> instructions = new ArrayList<InstructionInfo>();
+        List<ActionInfo> actionsInfos = new ArrayList<ActionInfo>();
+        actionsInfos.add(new ActionInfo(ActionType.group, new String[] {String.valueOf(groupId)}));
+        instructions.add(new InstructionInfo(InstructionType.write_actions, actionsInfos));
+
+
+        String flowRef =  new StringBuffer().append(SNAT_FLOWID_PREFIX).append(dpnId).append(FLOWID_SEPARATOR).
+                append(SNAT_TABLE).append(FLOWID_SEPARATOR).append(routerId).toString();
+
+        BigInteger cookieSnat = NatUtil.getCookieSnatFlow(routerId);
+        try {
+            PowerMockito.when(MDSALUtil.class, "buildFlowEntity", dpnId, SNAT_TABLE, flowRef,
+                    DEFAULT_SNAT_FLOW_PRIORITY, flowRef, 0, 0,
+                    cookieSnat, matches, instructions ).thenReturn(flowMock);
+        } catch (Exception e) {
+            // Test failed anyways
+            assertEquals("true", "false");
+        }
+        /* TODO : Fix this to mock it properly when it reads DS
+        extNetworks.buildSnatFlowEntity(dpnId, routerName, groupId);
+        PowerMockito.verifyStatic(); */
+
+    }
+
+}