Skip to content

Commit

Permalink
Add mac address field into virtual router
Browse files Browse the repository at this point in the history
Change-Id: I387507756d88732f9e1733b9d1037f8d0861f848
(cherry picked from commit c49017d)
  • Loading branch information
gunine committed Mar 16, 2021
1 parent a19ca36 commit 9756cec
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.onlab.packet.MacAddress;

import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -37,6 +38,7 @@ public final class DefaultKubevirtRouter implements KubevirtRouter {
private final String name;
private final String description;
private final boolean enableSnat;
private final MacAddress mac;
private final Set<String> internal;
private final Map<String, String> external;
private final KubevirtPeerRouter peerRouter;
Expand All @@ -48,19 +50,22 @@ public final class DefaultKubevirtRouter implements KubevirtRouter {
* @param name router name
* @param description router description
* @param enableSnat snat use indicator
* @param mac MAC address
* @param internal internal networks
* @param external external network
* @param peerRouter external peer router
* @param gateway elected gateway node id
*/
public DefaultKubevirtRouter(String name, String description, boolean enableSnat,
public DefaultKubevirtRouter(String name, String description,
boolean enableSnat, MacAddress mac,
Set<String> internal,
Map<String, String> external,
KubevirtPeerRouter peerRouter,
String gateway) {
this.name = name;
this.description = description;
this.enableSnat = enableSnat;
this.mac = mac;
this.internal = internal;
this.external = external;
this.peerRouter = peerRouter;
Expand All @@ -82,6 +87,11 @@ public boolean enableSnat() {
return enableSnat;
}

@Override
public MacAddress mac() {
return mac;
}

@Override
public Set<String> internal() {
if (internal == null) {
Expand Down Expand Up @@ -116,6 +126,7 @@ public KubevirtRouter updatePeerRouter(KubevirtPeerRouter updated) {
.name(name)
.enableSnat(enableSnat)
.description(description)
.mac(mac)
.internal(internal)
.external(external)
.peerRouter(updated)
Expand All @@ -129,6 +140,7 @@ public KubevirtRouter updatedElectedGateway(String updated) {
.name(name)
.enableSnat(enableSnat)
.description(description)
.mac(mac)
.internal(internal)
.external(external)
.peerRouter(peerRouter)
Expand All @@ -146,14 +158,14 @@ public boolean equals(Object o) {
}
DefaultKubevirtRouter that = (DefaultKubevirtRouter) o;
return enableSnat == that.enableSnat && name.equals(that.name) &&
description.equals(that.description) && internal.equals(that.internal) &&
external.equals(that.external) && peerRouter.equals(that.peerRouter) &&
gateway.equals(that.electedGateway());
description.equals(that.description) && mac.equals(that.mac) &&
internal.equals(that.internal) && external.equals(that.external) &&
peerRouter.equals(that.peerRouter) && gateway.equals(that.electedGateway());
}

@Override
public int hashCode() {
return Objects.hash(name, description, enableSnat,
return Objects.hash(name, description, mac, enableSnat,
internal, external, peerRouter, gateway);
}

Expand All @@ -163,6 +175,7 @@ public String toString() {
.add("name", name)
.add("description", description)
.add("enableSnat", enableSnat)
.add("mac", mac)
.add("internal", internal)
.add("external", external)
.add("peerRouter", peerRouter)
Expand All @@ -184,6 +197,7 @@ public static final class Builder implements KubevirtRouter.Builder {
private String name;
private String description;
private boolean enableSnat;
private MacAddress mac;
private Set<String> internal;
private Map<String, String> external;
private KubevirtPeerRouter peerRouter;
Expand All @@ -193,7 +207,7 @@ public static final class Builder implements KubevirtRouter.Builder {
public KubevirtRouter build() {
checkArgument(name != null, NOT_NULL_MSG, "name");

return new DefaultKubevirtRouter(name, description, enableSnat,
return new DefaultKubevirtRouter(name, description, enableSnat, mac,
internal, external, peerRouter, gateway);
}

Expand All @@ -215,6 +229,12 @@ public Builder enableSnat(boolean flag) {
return this;
}

@Override
public Builder mac(MacAddress mac) {
this.mac = mac;
return this;
}

@Override
public Builder internal(Set<String> internal) {
this.internal = Objects.requireNonNullElseGet(internal, HashSet::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.onosproject.kubevirtnetworking.api;

import org.onlab.packet.MacAddress;

import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -44,6 +46,13 @@ public interface KubevirtRouter {
*/
boolean enableSnat();

/**
* Returns the MAC address.
*
* @return mac address
*/
MacAddress mac();

/**
* Returns a set of internal networks.
*
Expand Down Expand Up @@ -123,6 +132,14 @@ interface Builder {
*/
Builder enableSnat(boolean flag);

/**
* Returns kubevirt router builder with supplied MAC address.
*
* @param mac MAC address
* @return router builder
*/
Builder mac(MacAddress mac);

/**
* Returns kubevirt router builder with supplied internal networks.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class DefaultKubevirtRouterTest {
private static final String DESCRIPTION_2 = "dummy router 2";
private static final boolean ENABLE_SNAT_1 = false;
private static final boolean ENABLE_SNAT_2 = true;
private static final MacAddress MAC_ADDRESS_1 = MacAddress.valueOf("11:22:33:44:55:66");
private static final MacAddress MAC_ADDRESS_2 = MacAddress.valueOf("22:33:44:55:66:77");
private static final KubevirtNetwork.Type TYPE_1 = FLAT;
private static final KubevirtNetwork.Type TYPE_2 = VXLAN;
private static final String NETWORK_NAME_1 = "net-1";
Expand Down Expand Up @@ -74,6 +76,7 @@ public void setUp() {
.name(NAME_1)
.description(DESCRIPTION_1)
.enableSnat(ENABLE_SNAT_1)
.mac(MAC_ADDRESS_1)
.internal(ImmutableSet.of(NETWORK_NAME_1))
.external(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1))
.peerRouter(PEER_ROUTER_1)
Expand All @@ -83,6 +86,7 @@ public void setUp() {
.name(NAME_1)
.description(DESCRIPTION_1)
.enableSnat(ENABLE_SNAT_1)
.mac(MAC_ADDRESS_1)
.internal(ImmutableSet.of(NETWORK_NAME_1))
.external(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1))
.peerRouter(PEER_ROUTER_1)
Expand All @@ -92,6 +96,7 @@ public void setUp() {
.name(NAME_2)
.description(DESCRIPTION_2)
.enableSnat(ENABLE_SNAT_2)
.mac(MAC_ADDRESS_2)
.internal(ImmutableSet.of(NETWORK_NAME_2))
.external(ImmutableMap.of("20.20.20.20", NETWORK_NAME_2))
.peerRouter(PEER_ROUTER_2)
Expand Down Expand Up @@ -119,6 +124,7 @@ public void testConstruction() {
assertEquals(NAME_1, router.name());
assertEquals(DESCRIPTION_1, router.description());
assertEquals(ENABLE_SNAT_1, router.enableSnat());
assertEquals(MAC_ADDRESS_1, router.mac());
assertEquals(ImmutableSet.of(NETWORK_NAME_1), router.internal());
assertEquals(ImmutableMap.of("10.10.10.10", NETWORK_NAME_1), router.external());
assertEquals(PEER_ROUTER_1, router.peerRouter());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public ObjectNode encode(KubevirtRouter router, CodecContext context) {

ObjectNode result = context.mapper().createObjectNode()
.put(NAME, router.name())
.put(ENABLE_SNAT, router.enableSnat());
.put(ENABLE_SNAT, router.enableSnat())
.put(MAC_ADDRESS, router.mac().toString());

if (router.description() != null) {
result.put(DESCRIPTION, router.description());
Expand Down Expand Up @@ -113,8 +114,12 @@ public KubevirtRouter decode(ObjectNode json, CodecContext context) {
String name = nullIsIllegal(json.get(NAME).asText(),
NAME + MISSING_MESSAGE);

String vrouterMac = nullIsIllegal(json.get(MAC_ADDRESS).asText(),
MAC_ADDRESS + MISSING_MESSAGE);

KubevirtRouter.Builder builder = DefaultKubevirtRouter.builder()
.name(name);
.name(name)
.mac(MacAddress.valueOf(vrouterMac));

JsonNode descriptionJson = json.get(DESCRIPTION);
if (descriptionJson != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"required": [
"name",
"enableSnat",
"mac",
"internal",
"external",
"peerRouter"
Expand All @@ -32,6 +33,11 @@
"example": "true",
"description": "Enable SNAT flag."
},
"mac": {
"type": "string",
"example": "11:22:33:44:55:66",
"description": "The MAC address of virtual router."
},
"internal": {
"type": "array",
"xml": {
Expand Down Expand Up @@ -77,7 +83,7 @@
"example": "10.10.10.1",
"description": "IP address of the peer router"
},
"macaddress": {
"mac": {
"type": "string",
"example": "11:22:33:44:55:66",
"description": "MAC address of the peer router"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void testKubevirtRouterEncode() {
KubevirtRouter router = DefaultKubevirtRouter.builder()
.name("router-1")
.enableSnat(true)
.mac(MacAddress.valueOf("11:22:33:44:55:66"))
.description("router-1")
.internal(ImmutableSet.of("vlan-1"))
.external(ImmutableMap.of("10.10.10.20", "flat-1"))
Expand All @@ -97,6 +98,7 @@ public void testKubevirtRouterDecode() throws IOException {
assertEquals("router-1", router.name());
assertEquals("Example Virtual Router", router.description());
assertTrue(router.enableSnat());
assertEquals("11:22:33:44:55:66", router.mac().toString());
assertEquals("192.168.10.5",
router.external().keySet().stream().findAny().orElse(null));
assertEquals("external-network", router.external().get("192.168.10.5"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
}
}

// check vrouter MAC
String jsonMac = jsonNode.get(MAC_ADDRESS).asText();
String mac = router.mac().toString();
if (!jsonMac.equals(mac)) {
description.appendText("MAC was " + jsonMac);
return false;
}

// check internal
JsonNode jsonInternal = jsonNode.get(INTERNAL);
if (jsonInternal != null) {
Expand Down Expand Up @@ -134,10 +142,10 @@ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
}
}

JsonNode jsonMac = jsonPeerRouter.get(MAC_ADDRESS);
JsonNode jsonProuterMac = jsonPeerRouter.get(MAC_ADDRESS);

if (jsonMac != null) {
if (!jsonMac.asText().equals(router.peerRouter().macAddress().toString())) {
if (jsonProuterMac != null) {
if (!jsonProuterMac.asText().equals(router.peerRouter().macAddress().toString())) {
description.appendText("Peer router MAC was " + jsonMac);
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "router-1",
"description": "Example Virtual Router",
"enableSnat": true,
"mac": "11:22:33:44:55:66",
"external": {
"ip": "192.168.10.5",
"network": "external-network"
Expand Down

0 comments on commit 9756cec

Please sign in to comment.