Support API that configures IPv6 address as sourceAddress 26/57026/1
authorSridhar Gaddam <sgaddam@redhat.com>
Sun, 14 May 2017 20:16:12 +0000 (01:46 +0530)
committerSridhar Gaddam <sgaddam@redhat.com>
Sun, 14 May 2017 20:16:40 +0000 (01:46 +0530)
Change-Id: I23e2f04315ca023be9886d7e1570f78230b65dd7
Signed-off-by: Sridhar Gaddam <sgaddam@redhat.com>
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6.java [new file with mode: 0644]
mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6Test.java [new file with mode: 0644]

diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6.java
new file mode 100644 (file)
index 0000000..6beaf55
--- /dev/null
@@ -0,0 +1,95 @@
+/*
+ * Copyright © 2017 Red Hat, Inc. and others.
+ *
+ * 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.genius.mdsalutil.actions;
+
+import org.opendaylight.genius.mdsalutil.ActionInfo;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
+
+/**
+ * Set source IPv6 action.
+ */
+public class ActionSetSourceIpv6 extends ActionInfo {
+    private final Ipv6Prefix source;
+
+    public ActionSetSourceIpv6(String sourceIp) {
+        this(0, sourceIp, null);
+    }
+
+    public ActionSetSourceIpv6(int actionKey, String sourceIp) {
+        this(actionKey, sourceIp, null);
+    }
+
+    public ActionSetSourceIpv6(String sourceIp, String sourceMask) {
+        this(0, sourceIp, sourceMask);
+    }
+
+    public ActionSetSourceIpv6(int actionKey, String sourceIp, String sourceMask) {
+        this(actionKey, new Ipv6Prefix(sourceIp + "/" + (sourceMask != null ? sourceMask : "128")));
+    }
+
+    public ActionSetSourceIpv6(Ipv6Prefix source) {
+        this(0, source);
+    }
+
+    public ActionSetSourceIpv6(int actionKey, Ipv6Prefix source) {
+        super(actionKey);
+        this.source = source;
+    }
+
+    @Override
+    public Action buildAction() {
+        return buildAction(getActionKey());
+    }
+
+    @Override
+    public Action buildAction(int newActionKey) {
+        return new ActionBuilder()
+            .setAction(new SetFieldCaseBuilder()
+                .setSetField(new SetFieldBuilder()
+                    .setLayer3Match(new Ipv6MatchBuilder()
+                        .setIpv6Source(new Ipv6Prefix(source)).build())
+                    .build())
+                .build())
+            .setKey(new ActionKey(newActionKey))
+            .build();
+    }
+
+    public Ipv6Prefix getSource() {
+        return source;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (this == other) {
+            return true;
+        }
+        if (other == null || getClass() != other.getClass()) {
+            return false;
+        }
+        if (!super.equals(other)) {
+            return false;
+        }
+
+        ActionSetSourceIpv6 that = (ActionSetSourceIpv6) other;
+
+        return source != null ? source.equals(that.source) : that.source == null;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = super.hashCode();
+        result = 31 * result + (source != null ? source.hashCode() : 0);
+        return result;
+    }
+}
diff --git a/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6Test.java b/mdsalutil/mdsalutil-api/src/test/java/org/opendaylight/genius/mdsalutil/actions/ActionSetSourceIpv6Test.java
new file mode 100644 (file)
index 0000000..8ef767b
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright © 2017 Red Hat, Inc. and others.
+ *
+ * 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.genius.mdsalutil.actions;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import ch.vorburger.xtendbeans.XtendBeanGenerator;
+import org.junit.Test;
+import org.opendaylight.genius.mdsalutil.ActionInfo;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
+
+/**
+ * Test class for {@link ActionSetSourceIpv6}.
+ */
+public class ActionSetSourceIpv6Test {
+    private static final String IP_ADDRESS = "2001:db8::1";
+    private static final String IP_MASK = "128";
+
+    private XtendBeanGenerator generator = new XtendBeanGenerator();
+
+    @Test
+    public void actionInfoTest() {
+        verifyAction(new ActionSetSourceIpv6(IP_ADDRESS).buildAction());
+        verifyAction(new ActionSetSourceIpv6(IP_ADDRESS, IP_MASK).buildAction());
+        verifyAction(new ActionSetSourceIpv6(new Ipv6Prefix(IP_ADDRESS + "/" + IP_MASK)).buildAction());
+    }
+
+    private void verifyAction(Action action) {
+        assertTrue(action.getAction() instanceof SetFieldCase);
+        SetFieldCase actionCase = (SetFieldCase) action.getAction();
+        assertNotNull(actionCase.getSetField().getLayer3Match());
+        assertTrue(actionCase.getSetField().getLayer3Match() instanceof Ipv6Match);
+        Ipv6Match ipv6Match = (Ipv6Match) actionCase.getSetField().getLayer3Match();
+        assertEquals(new Ipv6Prefix(IP_ADDRESS + "/" + IP_MASK), ipv6Match.getIpv6Source());
+    }
+
+    @Test
+    public void generateAction() {
+        ActionInfo actionInfo = new ActionSetSourceIpv6(IP_ADDRESS);
+        assertEquals("new ActionSetSourceIpv6(0, new Ipv6Prefix(\"" + IP_ADDRESS + "/" + IP_MASK + "\"))",
+                generator.getExpression(actionInfo));
+    }
+}