From a00e19c17474ee3b378e455aeed1a07c3257c7ad Mon Sep 17 00:00:00 2001 From: kiwiflyer Date: Thu, 19 Nov 2026 12:44:46 +0000 Subject: [PATCH] extensions: let NetworkOrchestrator extensions handle VPC gateways and routes NetworkExtensionElement implements every other VpcProvider method by delegating to the extension script, but four of them throw UnsupportedOperationException: createPrivateGateway, deletePrivateGateway, applyStaticRoutes and applyACLItemsToPrivateGw. The restriction is at the framework level, so it applies to every network extension whatever its backend can do, and an extension-backed VPC cannot reach parity with the VPC virtual router. This wires them up the same way as their neighbours, adding four script commands: create-private-gateway delete-private-gateway apply-static-routes apply-private-gw-acl Private gateway operations resolve the owning VPC and go through the existing VPC-level script path, carrying the gateway's id, uuid, address, netmask, broadcast URI and derived VLAN alongside the usual VPC fields. applyStaticRoutes and applyACLItemsToPrivateGw are full rebuilds that send the complete non-revoked set on every call, matching applyFWRules and applyNetworkACLs rather than the per-object style of add-static-nat; a backend reconciles instead of tracking deltas. An extension that does not implement these verbs is unaffected: the commands are new, and before this change the operations failed with UnsupportedOperationException regardless of the script, so no previously working behaviour changes. addPrivateGatewayToPayload documents gateway_vlan as conditional but emitted it unconditionally whenever a broadcast URI was present. BroadcastDomainType returns null for a URI whose scheme it does not recognise, so a non-VLAN gateway shipped "gateway_vlan": "" to the script. It is now emitted only when it has a value, which is what the javadoc already claimed and what every neighbouring field does. Adds NetworkExtensionElementTest, the first unit test this class has had. 26 cases over the four methods: the payload fields each sends, the VPC and gateway identity the script needs to find the router, the omit-when-blank branch of every optional field, both failure styles (createPrivateGateway and deletePrivateGateway return false, applyStaticRoutes and applyACLItemsToPrivateGw throw ResourceUnavailableException), full serialisation of a static route and of an ACL rule, rule ordering by number and routes by CIDR, and a null CIDR through the route comparator. The command name constants are asserted against their literals, since they are the wire contract with every deployed script. Adds NetworkExtensionElementPayloadContractTest, 6 more. It asserts the keys the script cannot run without -- a transcription of a reference implementation's require_arg calls, with the source named in each test -- and writes the payloads this class actually produces to target/extension-payloads/. The unit tests assert what this class writes and an extension's own tests assert what it reads, each checked against a fixture written by the same hand, so both would pass if a key name drifted between them. Replaying the real payloads against a real backend is what closes that: the six captured payloads pass 21 assertions against a live OVN northbound database, including that ovn-northd resolves the static routes this class emitted. 377 tests pass in framework/extensions. --- .../network/NetworkExtensionElement.java | 211 ++++++- ...rkExtensionElementPayloadContractTest.java | 300 ++++++++++ .../network/NetworkExtensionElementTest.java | 563 ++++++++++++++++++ 3 files changed, 1067 insertions(+), 7 deletions(-) create mode 100644 framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementPayloadContractTest.java create mode 100644 framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementTest.java diff --git a/framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java b/framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java index ff2595d8e215..3711fb3b2ddf 100644 --- a/framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java +++ b/framework/extensions/src/main/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElement.java @@ -76,6 +76,7 @@ import com.cloud.network.element.VpcProvider; import com.cloud.network.vpc.NetworkACLItem; import com.cloud.network.vpc.PrivateGateway; +import com.cloud.network.vpc.StaticRoute; import com.cloud.network.vpc.StaticRouteProfile; import com.cloud.network.vpc.Vpc; import com.cloud.network.lb.LoadBalancingRule; @@ -246,6 +247,10 @@ public class NetworkExtensionElement extends AdapterBase implements public static final String CMD_CUSTOM_ACTION = "custom-action"; public static final String CMD_PREPARE_NIC = "prepare-nic"; public static final String CMD_RELEASE_NIC = "release-nic"; + public static final String CMD_CREATE_PRIVATE_GATEWAY = "create-private-gateway"; + public static final String CMD_DELETE_PRIVATE_GATEWAY = "delete-private-gateway"; + public static final String CMD_APPLY_STATIC_ROUTES = "apply-static-routes"; + public static final String CMD_APPLY_PRIVATE_GW_ACL = "apply-private-gw-acl"; // ---- Network detail key ---- @@ -2459,31 +2464,223 @@ public boolean shutdownVpc(Vpc vpc, ReservationContext context) return result; } + /** + * Resolves the {@link Vpc} a private gateway belongs to, so gateway operations can + * go through the VPC-level script path like every other VPC operation. + */ + private Vpc getVpcForPrivateGateway(PrivateGateway gateway, String action) { + if (gateway == null || gateway.getVpcId() == null) { + logger.warn("{}: private gateway has no VPC", action); + return null; + } + Vpc vpc = vpcDao.findById(gateway.getVpcId()); + if (vpc == null) { + logger.warn("{}: could not find VPC {} for private gateway {}", action, gateway.getVpcId(), gateway.getUuid()); + } + return vpc; + } + + /** + * Creates the private gateway on the external network system via the script's + * {@code create-private-gateway} command. + * + *

Script command: {@code create-private-gateway}

+ */ @Override public boolean createPrivateGateway(PrivateGateway gateway) throws ConcurrentOperationException, ResourceUnavailableException { - throw new UnsupportedOperationException("Private gateways are not supported by the network extension element."); + final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_CREATE_PRIVATE_GATEWAY); + if (vpc == null) { + return false; + } + + logger.info("Creating private gateway {} for VPC {}", gateway.getUuid(), vpc); + + final JsonObject payload = new JsonObject(); + addVpcToPayload(payload, vpc); + addPrivateGatewayToPayload(payload, gateway); + + return executeVpcScript(vpc, CMD_CREATE_PRIVATE_GATEWAY, payload); } - /** Private gateways are not supported by the network extension element. */ + /** + * Removes the private gateway via the script's {@code delete-private-gateway} command. + * + *

Script command: {@code delete-private-gateway}

+ */ @Override public boolean deletePrivateGateway(PrivateGateway gateway) throws ConcurrentOperationException, ResourceUnavailableException { - throw new UnsupportedOperationException("Private gateways are not supported by the network extension element."); + final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_DELETE_PRIVATE_GATEWAY); + if (vpc == null) { + return false; + } + + logger.info("Deleting private gateway {} for VPC {}", gateway.getUuid(), vpc); + + final JsonObject payload = new JsonObject(); + addVpcToPayload(payload, vpc); + addPrivateGatewayToPayload(payload, gateway); + + return executeVpcScript(vpc, CMD_DELETE_PRIVATE_GATEWAY, payload); } - /** Static routes are not supported by the network extension element. */ + /** + * Applies the VPC's static routes via the script's {@code apply-static-routes} + * command. + * + *

Like {@link #applyFWRules} and {@link #applyNetworkACLs}, this is a full + * rebuild: the complete set of non-revoked routes is sent on every call, so the + * script can reconcile rather than track individual additions and removals.

+ * + *

Script command: {@code apply-static-routes}

+ */ @Override public boolean applyStaticRoutes(Vpc vpc, List routes) throws ResourceUnavailableException { - throw new UnsupportedOperationException("Static routes are not supported by the network extension element."); + if (vpc == null) { + logger.warn("applyStaticRoutes: no VPC given"); + return false; + } + + final List activeRoutes = routes == null ? List.of() : + routes.stream() + .filter(r -> !StaticRoute.State.Revoke.equals(r.getState())) + .collect(Collectors.toList()); + + logger.info("applyStaticRoutes: vpc={} activeRoutes={}", vpc, activeRoutes.size()); + + final JsonObject payload = new JsonObject(); + addVpcToPayload(payload, vpc); + payload.add("static_routes", buildStaticRoutesArray(activeRoutes)); + + final boolean result = executeVpcScript(vpc, CMD_APPLY_STATIC_ROUTES, payload); + if (!result) { + throw new ResourceUnavailableException( + "Failed to apply static routes for VPC " + vpc.getUuid(), + Vpc.class, vpc.getId()); + } + return true; } - /** ACL items on private gateways are not supported by the network extension element. */ + /** + * Applies network ACL items to a private gateway via the script's + * {@code apply-private-gw-acl} command. + * + *

A full rebuild, matching {@link #applyNetworkACLs}.

+ * + *

Script command: {@code apply-private-gw-acl}

+ */ @Override public boolean applyACLItemsToPrivateGw(PrivateGateway gateway, List rules) throws ResourceUnavailableException { - throw new UnsupportedOperationException("ACL items on private gateways are not supported by the network extension element."); + final Vpc vpc = getVpcForPrivateGateway(gateway, CMD_APPLY_PRIVATE_GW_ACL); + if (vpc == null) { + return false; + } + + final List activeRules = rules == null ? List.of() : + rules.stream() + .filter(r -> !NetworkACLItem.State.Revoke.equals(r.getState())) + .collect(Collectors.toList()); + + logger.info("applyACLItemsToPrivateGw: gateway={} activeRules={}", gateway.getUuid(), activeRules.size()); + + final JsonObject payload = new JsonObject(); + addVpcToPayload(payload, vpc); + addPrivateGatewayToPayload(payload, gateway); + payload.add("acl_rules", buildAclRulesArray(activeRules)); + + final boolean result = executeVpcScript(vpc, CMD_APPLY_PRIVATE_GW_ACL, payload); + if (!result) { + throw new ResourceUnavailableException( + "Failed to apply ACL items to private gateway " + gateway.getUuid(), + Vpc.class, vpc.getId()); + } + return true; + } + + /** + * Adds the private gateway fields to the payload. + * Always present: gateway_id, gateway_uuid, gateway_network_id, gateway_zone_id, + * gateway_physical_network_id, gateway_source_nat, gateway_network_acl_id. + * Conditional (only when non-blank): gateway_ip_address, gateway_gateway, + * gateway_netmask, gateway_broadcast_uri, gateway_vlan, gateway_state. + */ + private void addPrivateGatewayToPayload(JsonObject payload, PrivateGateway gateway) { + if (payload == null || gateway == null) { + return; + } + payload.addProperty("gateway_id", String.valueOf(gateway.getId())); + payload.addProperty("gateway_uuid", safeStr(gateway.getUuid())); + payload.addProperty("gateway_network_id", String.valueOf(gateway.getNetworkId())); + payload.addProperty("gateway_zone_id", String.valueOf(gateway.getZoneId())); + payload.addProperty("gateway_physical_network_id", String.valueOf(gateway.getPhysicalNetworkId())); + payload.addProperty("gateway_source_nat", String.valueOf(gateway.getSourceNat())); + payload.addProperty("gateway_network_acl_id", String.valueOf(gateway.getNetworkACLId())); + if (StringUtils.isNotBlank(gateway.getIp4Address())) { + payload.addProperty("gateway_ip_address", safeStr(gateway.getIp4Address())); + } + if (StringUtils.isNotBlank(gateway.getGateway())) { + payload.addProperty("gateway_gateway", safeStr(gateway.getGateway())); + } + if (StringUtils.isNotBlank(gateway.getNetmask())) { + payload.addProperty("gateway_netmask", safeStr(gateway.getNetmask())); + } + if (StringUtils.isNotBlank(gateway.getBroadcastUri())) { + payload.addProperty("gateway_broadcast_uri", safeStr(gateway.getBroadcastUri())); + try { + String vlan = Networks.BroadcastDomainType.getValue(new URI(gateway.getBroadcastUri())); + if (StringUtils.isNotBlank(vlan)) { + payload.addProperty("gateway_vlan", vlan); + } + } catch (Exception e) { + logger.debug("Could not derive a VLAN from broadcast URI {} of private gateway {}", + gateway.getBroadcastUri(), gateway.getUuid()); + } + } + if (gateway.getState() != null) { + payload.addProperty("gateway_state", gateway.getState().toString().toLowerCase()); + } + } + + /** + * Serialises static routes to a {@link JsonArray} for the + * {@code apply-static-routes} script command. Routes are sorted by CIDR so the + * payload is stable between calls that carry the same set. + */ + private JsonArray buildStaticRoutesArray(List routes) { + JsonArray array = new JsonArray(); + List sorted = routes.stream() + .sorted(java.util.Comparator.comparing( + r -> StringUtils.defaultString(r.getCidr()))) + .collect(Collectors.toList()); + for (StaticRouteProfile route : sorted) { + JsonObject routeObj = new JsonObject(); + routeObj.addProperty("id", route.getId()); + routeObj.addProperty("cidr", safeStr(route.getCidr())); + routeObj.addProperty("next_hop", safeStr(route.getNextHop())); + if (route.getState() != null) { + routeObj.addProperty("state", route.getState().toString().toLowerCase()); + } + if (route.getVpcGatewayId() != null) { + routeObj.addProperty("vpc_gateway_id", String.valueOf(route.getVpcGatewayId())); + } + if (StringUtils.isNotBlank(route.getIp4Address())) { + routeObj.addProperty("gateway_ip_address", safeStr(route.getIp4Address())); + } + if (StringUtils.isNotBlank(route.getGateway())) { + routeObj.addProperty("gateway", safeStr(route.getGateway())); + } + if (StringUtils.isNotBlank(route.getNetmask())) { + routeObj.addProperty("netmask", safeStr(route.getNetmask())); + } + if (StringUtils.isNotBlank(route.getVlanTag())) { + routeObj.addProperty("vlan", safeStr(route.getVlanTag())); + } + array.add(routeObj); + } + return array; } @Override diff --git a/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementPayloadContractTest.java b/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementPayloadContractTest.java new file mode 100644 index 000000000000..b675e213263e --- /dev/null +++ b/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementPayloadContractTest.java @@ -0,0 +1,300 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.framework.extensions.network; + +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.List; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.network.Network; +import com.cloud.network.vpc.NetworkACLItem; +import com.cloud.network.vpc.PrivateGateway; +import com.cloud.network.vpc.StaticRoute; +import com.cloud.network.vpc.StaticRouteProfile; +import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.VpcGateway; +import com.cloud.network.vpc.VpcVO; +import com.cloud.network.vpc.dao.VpcDao; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; + +/** + * Pins the wire contract between this class and the extension script for the four + * VPC gateway / static route commands, and writes the payloads it actually produces + * to {@code target/extension-payloads/} so they can be replayed against a real + * backend. + * + *

The sibling unit test asserts what this class writes. The script's own test + * asserts what the script reads. Neither notices if the two drift apart, because + * each half is checked against a fixture written by the same hand. This test closes + * that gap from the Java side by asserting the keys the script treats as mandatory, + * and it produces the input for closing it from the other side: the extension's + * {@code test-with-ovn.sh --payloads target/extension-payloads} replays these exact + * files against a live OVN northbound database.

+ * + *

The required-key lists below are a transcription of the script's + * {@code require_arg} calls. If the script starts needing a new field, this test + * should be updated in the same change and will fail until the payload carries it.

+ */ +@RunWith(MockitoJUnitRunner.class) +public class NetworkExtensionElementPayloadContractTest { + + /** Where the replayable payloads land; see the class javadoc. */ + private static final File OUTPUT_DIR = new File("target/extension-payloads"); + + private static final long VPC_ID = 7L; + private static final long TIER_ID = 100L; + private static final long ZONE_ID = 1L; + private static final long GATEWAY_ID = 31L; + + @Spy + @InjectMocks + NetworkExtensionElement element = new NetworkExtensionElement(); + + @Mock + VpcDao vpcDao; + + private VpcVO vpc; + + @BeforeClass + public static void prepareOutputDir() { + if (!OUTPUT_DIR.isDirectory() && !OUTPUT_DIR.mkdirs()) { + throw new IllegalStateException("could not create " + OUTPUT_DIR); + } + } + + @Before + public void setUp() { + vpc = mock(VpcVO.class); + lenient().when(vpc.getId()).thenReturn(VPC_ID); + lenient().when(vpc.getUuid()).thenReturn("c0ffee00-0000-4000-8000-0000000000c7"); + lenient().when(vpc.getZoneId()).thenReturn(ZONE_ID); + lenient().when(vpc.getCidr()).thenReturn("10.2.0.0/16"); + lenient().when(vpc.getState()).thenReturn(Vpc.State.Enabled); + lenient().when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + doReturn(true).when(element).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + private PrivateGateway privateGateway() { + PrivateGateway gateway = mock(PrivateGateway.class); + lenient().when(gateway.getVpcId()).thenReturn(VPC_ID); + lenient().when(gateway.getId()).thenReturn(GATEWAY_ID); + lenient().when(gateway.getUuid()).thenReturn("4f1d9f6e-0b7a-4a1e-9c2d-1a2b3c4d5e6f"); + lenient().when(gateway.getNetworkId()).thenReturn(55L); + lenient().when(gateway.getZoneId()).thenReturn(ZONE_ID); + lenient().when(gateway.getPhysicalNetworkId()).thenReturn(200L); + lenient().when(gateway.getIp4Address()).thenReturn("172.16.0.5"); + lenient().when(gateway.getGateway()).thenReturn("172.16.0.1"); + lenient().when(gateway.getNetmask()).thenReturn("255.255.255.0"); + lenient().when(gateway.getBroadcastUri()).thenReturn("vlan://900"); + lenient().when(gateway.getSourceNat()).thenReturn(false); + lenient().when(gateway.getNetworkACLId()).thenReturn(3L); + lenient().when(gateway.getState()).thenReturn(VpcGateway.State.Ready); + return gateway; + } + + /** + * Captures the payload the element produced for {@code command}, checks the keys + * the script cannot run without, and writes the whole thing out in the envelope + * buildVpcScriptPayload wraps it in, so the file + * is directly consumable by the script. + */ + private void captureAndWrite(String command, String... requiredKeys) { + ArgumentCaptor captor = ArgumentCaptor.forClass(JsonObject.class); + verify(element).executeVpcScript(any(Vpc.class), eq(command), captor.capture()); + writeEnvelope(command, captor.getValue(), requiredKeys); + } + + /** + * Tier-level commands go out through {@code executeScript(Network, ...)} rather + * than the VPC path, so they need their own capture point. + */ + private void captureNetworkCommandAndWrite(String command, String... requiredKeys) { + ArgumentCaptor captor = ArgumentCaptor.forClass(JsonObject.class); + verify(element).executeScript(any(Network.class), eq(command), captor.capture()); + writeEnvelope(command, captor.getValue(), requiredKeys); + } + + private void writeEnvelope(String command, JsonObject payload, String... requiredKeys) { + for (String key : requiredKeys) { + assertTrue(command + " must carry '" + key + "': the script's require_arg fails without it", + payload.has(key)); + } + + JsonObject envelope = new JsonObject(); + // An empty object here is a placeholder: the replay harness substitutes the + // details of whatever backend it is pointed at. The KEY names are the contract. + envelope.add(NetworkExtensionElement.ARG_PHYSICAL_NETWORK_EXTENSION_DETAILS, new JsonObject()); + envelope.add(NetworkExtensionElement.ARG_NETWORK_EXTENSION_DETAILS, new JsonObject()); + envelope.add(NetworkExtensionElement.ARG_PAYLOAD, payload); + + File out = new File(OUTPUT_DIR, command + ".json"); + try { + Files.write(out.toPath(), + new GsonBuilder().setPrettyPrinting().create().toJson(envelope) + .getBytes(StandardCharsets.UTF_8)); + } catch (IOException e) { + throw new UncheckedIOException("could not write " + out, e); + } + } + + /** A VPC tier: its own logical switch, a leg on the shared VPC router. */ + private Network vpcTier() { + Network tier = mock(Network.class); + lenient().when(tier.getId()).thenReturn(TIER_ID); + lenient().when(tier.getUuid()).thenReturn("9b2c7a10-5d3e-4c8f-a1b2-c3d4e5f60718"); + lenient().when(tier.getVpcId()).thenReturn(VPC_ID); + lenient().when(tier.getCidr()).thenReturn("10.2.1.0/24"); + lenient().when(tier.getGateway()).thenReturn("10.2.1.1"); + lenient().when(tier.getDataCenterId()).thenReturn(ZONE_ID); + lenient().when(tier.getGuestType()).thenReturn(Network.GuestType.Isolated); + lenient().when(tier.getState()).thenReturn(Network.State.Implemented); + return tier; + } + + @Test + public void testImplementVpcPayloadMeetsTheScriptContract() throws Exception { + doNothing().when(element).ensureExtensionDetails(any(Vpc.class)); + // No source NAT IP allocated yet: the common state when a VPC is first + // implemented. The address arrives later via update-vpc-source-nat-ip. + doReturn(null).when(element).getVpcSourceNatIp(VPC_ID); + + assertTrue(element.implementVpc(vpc, null, null)); + // ovn.sh cmd_implement_vpc: require_arg vpc_id; vpc_cidr sizes the VPC-wide + // source NAT rule once an address exists. + captureAndWrite(NetworkExtensionElement.CMD_IMPLEMENT_VPC, "vpc_id", "vpc_cidr"); + } + + @Test + public void testApplyNetworkAclPayloadMeetsTheScriptContract() throws Exception { + doReturn(true).when(element).canHandle(any(Network.class), any()); + doReturn(true).when(element).executeScript(any(Network.class), anyString(), any(JsonObject.class)); + + NetworkACLItem allowHttp = mock(NetworkACLItem.class); + lenient().when(allowHttp.getState()).thenReturn(NetworkACLItem.State.Active); + lenient().when(allowHttp.getNumber()).thenReturn(10); + lenient().when(allowHttp.getAction()).thenReturn(NetworkACLItem.Action.Allow); + lenient().when(allowHttp.getTrafficType()).thenReturn(NetworkACLItem.TrafficType.Ingress); + lenient().when(allowHttp.getProtocol()).thenReturn("tcp"); + lenient().when(allowHttp.getSourcePortStart()).thenReturn(80); + lenient().when(allowHttp.getSourcePortEnd()).thenReturn(80); + lenient().when(allowHttp.getIcmpType()).thenReturn(null); + lenient().when(allowHttp.getIcmpCode()).thenReturn(null); + lenient().when(allowHttp.getSourceCidrList()).thenReturn(List.of("0.0.0.0/0")); + + NetworkACLItem denyPeerTier = mock(NetworkACLItem.class); + lenient().when(denyPeerTier.getState()).thenReturn(NetworkACLItem.State.Active); + lenient().when(denyPeerTier.getNumber()).thenReturn(20); + lenient().when(denyPeerTier.getAction()).thenReturn(NetworkACLItem.Action.Deny); + lenient().when(denyPeerTier.getTrafficType()).thenReturn(NetworkACLItem.TrafficType.Egress); + lenient().when(denyPeerTier.getProtocol()).thenReturn("all"); + lenient().when(denyPeerTier.getSourcePortStart()).thenReturn(null); + lenient().when(denyPeerTier.getSourcePortEnd()).thenReturn(null); + lenient().when(denyPeerTier.getIcmpType()).thenReturn(null); + lenient().when(denyPeerTier.getIcmpCode()).thenReturn(null); + lenient().when(denyPeerTier.getSourceCidrList()).thenReturn(List.of("10.2.2.0/24")); + + assertTrue(element.applyNetworkACLs(vpcTier(), List.of(allowHttp, denyPeerTier))); + // ovn.sh cmd_apply_network_acl: require_arg network_id, needs the tier cidr + // for the default policy, and vpc_id to pick the VPC router scope. + captureNetworkCommandAndWrite(NetworkExtensionElement.CMD_APPLY_NETWORK_ACL, + "network_id", "vpc_id", "cidr", "acl_rules"); + } + + @Test + public void testCreatePrivateGatewayPayloadMeetsTheScriptContract() throws Exception { + assertTrue(element.createPrivateGateway(privateGateway())); + // ovn.sh cmd_create_private_gateway: require_arg gateway_id, gateway_ip_address; + // vpc_id selects the VPC router scope; netmask sizes the subnet. + captureAndWrite(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY, + "vpc_id", "gateway_id", "gateway_ip_address", "gateway_netmask"); + } + + @Test + public void testDeletePrivateGatewayPayloadMeetsTheScriptContract() throws Exception { + assertTrue(element.deletePrivateGateway(privateGateway())); + // ovn.sh cmd_delete_private_gateway: require_arg gateway_id. + captureAndWrite(NetworkExtensionElement.CMD_DELETE_PRIVATE_GATEWAY, + "vpc_id", "gateway_id"); + } + + @Test + public void testApplyStaticRoutesPayloadMeetsTheScriptContract() throws Exception { + StaticRouteProfile viaGateway = mock(StaticRouteProfile.class); + lenient().when(viaGateway.getCidr()).thenReturn("10.9.0.0/16"); + lenient().when(viaGateway.getNextHop()).thenReturn("172.16.0.1"); + lenient().when(viaGateway.getState()).thenReturn(StaticRoute.State.Active); + lenient().when(viaGateway.getId()).thenReturn(88L); + lenient().when(viaGateway.getVpcGatewayId()).thenReturn(GATEWAY_ID); + + StaticRouteProfile second = mock(StaticRouteProfile.class); + lenient().when(second.getCidr()).thenReturn("10.8.0.0/16"); + lenient().when(second.getNextHop()).thenReturn("172.16.0.1"); + lenient().when(second.getState()).thenReturn(StaticRoute.State.Add); + lenient().when(second.getId()).thenReturn(89L); + + assertTrue(element.applyStaticRoutes(vpc, List.of(viaGateway, second))); + // ovn.sh cmd_apply_static_routes: require_arg vpc_id, then iterates + // .payload.static_routes[] reading .cidr and .next_hop. + captureAndWrite(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES, + "vpc_id", "static_routes"); + } + + @Test + public void testApplyPrivateGwAclPayloadMeetsTheScriptContract() throws Exception { + NetworkACLItem rule = mock(NetworkACLItem.class); + lenient().when(rule.getState()).thenReturn(NetworkACLItem.State.Active); + lenient().when(rule.getNumber()).thenReturn(10); + lenient().when(rule.getAction()).thenReturn(NetworkACLItem.Action.Allow); + lenient().when(rule.getTrafficType()).thenReturn(NetworkACLItem.TrafficType.Ingress); + lenient().when(rule.getProtocol()).thenReturn("tcp"); + lenient().when(rule.getSourcePortStart()).thenReturn(443); + lenient().when(rule.getSourcePortEnd()).thenReturn(443); + lenient().when(rule.getIcmpType()).thenReturn(null); + lenient().when(rule.getIcmpCode()).thenReturn(null); + lenient().when(rule.getSourceCidrList()).thenReturn(List.of("10.9.0.0/16")); + + assertTrue(element.applyACLItemsToPrivateGw(privateGateway(), List.of(rule))); + // ovn.sh cmd_apply_private_gw_acl: require_arg gateway_id, gateway_ip_address; + // gateway_netmask scopes the rules; acl_rules carries the rule set. + captureAndWrite(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL, + "vpc_id", "gateway_id", "gateway_ip_address", "gateway_netmask", "acl_rules"); + } +} diff --git a/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementTest.java b/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementTest.java new file mode 100644 index 000000000000..3c0da83f9059 --- /dev/null +++ b/framework/extensions/src/test/java/org/apache/cloudstack/framework/extensions/network/NetworkExtensionElementTest.java @@ -0,0 +1,563 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package org.apache.cloudstack.framework.extensions.network; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Spy; +import org.mockito.junit.MockitoJUnitRunner; + +import com.cloud.exception.ResourceUnavailableException; +import com.cloud.network.vpc.NetworkACLItem; +import com.cloud.network.vpc.PrivateGateway; +import com.cloud.network.vpc.StaticRoute; +import com.cloud.network.vpc.StaticRouteProfile; +import com.cloud.network.vpc.Vpc; +import com.cloud.network.vpc.VpcGateway; +import com.cloud.network.vpc.VpcVO; +import com.cloud.network.vpc.dao.VpcDao; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +/** + * Covers the four VpcProvider methods that used to throw UnsupportedOperationException: + * createPrivateGateway, deletePrivateGateway, applyStaticRoutes and applyACLItemsToPrivateGw. + * + *

The payload assertions are deliberately literal. The JSON key names are the wire + * contract with the extension script, so a rename here is a breaking change for every + * deployed script and should fail a test rather than slip through.

+ */ +@RunWith(MockitoJUnitRunner.class) +public class NetworkExtensionElementTest { + + private static final long VPC_ID = 7L; + private static final long ZONE_ID = 1L; + private static final long GATEWAY_ID = 31L; + + @Spy + @InjectMocks + NetworkExtensionElement element = new NetworkExtensionElement(); + + @Mock + VpcDao vpcDao; + + private VpcVO vpc; + + @Before + public void setUp() { + vpc = mock(VpcVO.class); + lenient().when(vpc.getId()).thenReturn(VPC_ID); + lenient().when(vpc.getZoneId()).thenReturn(ZONE_ID); + lenient().when(vpc.getUuid()).thenReturn("vpc-uuid"); + lenient().when(vpc.getCidr()).thenReturn("10.0.0.0/8"); + lenient().when(vpc.getState()).thenReturn(Vpc.State.Enabled); + } + + /** A fully populated gateway: every optional field has a value. */ + private PrivateGateway mockPrivateGateway() { + PrivateGateway gateway = mock(PrivateGateway.class); + lenient().when(gateway.getVpcId()).thenReturn(VPC_ID); + lenient().when(gateway.getId()).thenReturn(GATEWAY_ID); + lenient().when(gateway.getUuid()).thenReturn("gw-uuid"); + lenient().when(gateway.getNetworkId()).thenReturn(55L); + lenient().when(gateway.getZoneId()).thenReturn(ZONE_ID); + lenient().when(gateway.getPhysicalNetworkId()).thenReturn(200L); + lenient().when(gateway.getIp4Address()).thenReturn("172.16.0.5"); + lenient().when(gateway.getGateway()).thenReturn("172.16.0.1"); + lenient().when(gateway.getNetmask()).thenReturn("255.255.255.0"); + lenient().when(gateway.getBroadcastUri()).thenReturn("vlan://900"); + lenient().when(gateway.getSourceNat()).thenReturn(false); + lenient().when(gateway.getNetworkACLId()).thenReturn(3L); + lenient().when(gateway.getState()).thenReturn(VpcGateway.State.Ready); + return gateway; + } + + /** The minimum a gateway can carry: every optional field is blank or null. */ + private PrivateGateway mockBarePrivateGateway() { + PrivateGateway gateway = mock(PrivateGateway.class); + lenient().when(gateway.getVpcId()).thenReturn(VPC_ID); + lenient().when(gateway.getId()).thenReturn(GATEWAY_ID); + lenient().when(gateway.getUuid()).thenReturn("gw-uuid"); + lenient().when(gateway.getNetworkId()).thenReturn(55L); + lenient().when(gateway.getZoneId()).thenReturn(ZONE_ID); + lenient().when(gateway.getPhysicalNetworkId()).thenReturn(200L); + lenient().when(gateway.getSourceNat()).thenReturn(true); + lenient().when(gateway.getNetworkACLId()).thenReturn(3L); + lenient().when(gateway.getIp4Address()).thenReturn(""); + lenient().when(gateway.getGateway()).thenReturn(null); + lenient().when(gateway.getNetmask()).thenReturn(" "); + lenient().when(gateway.getBroadcastUri()).thenReturn(null); + lenient().when(gateway.getState()).thenReturn(null); + return gateway; + } + + private void stubScript(boolean outcome) { + doReturn(outcome).when(element).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + private JsonObject captureVpcPayload(String expectedCommand) { + ArgumentCaptor captor = ArgumentCaptor.forClass(JsonObject.class); + verify(element).executeVpcScript(any(Vpc.class), eq(expectedCommand), captor.capture()); + return captor.getValue(); + } + + private JsonObject objectAt(JsonArray array, int index) { + return array.get(index).getAsJsonObject(); + } + + // ---- script command names: the wire contract with the extension script ---- + + @Test + public void testScriptCommandNamesAreStable() { + assertEquals("create-private-gateway", NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + assertEquals("delete-private-gateway", NetworkExtensionElement.CMD_DELETE_PRIVATE_GATEWAY); + assertEquals("apply-static-routes", NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + assertEquals("apply-private-gw-acl", NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL); + } + + // ---- createPrivateGateway / deletePrivateGateway ---- + + @Test + public void testCreatePrivateGatewaySendsGatewayPayload() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.createPrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + assertEquals(String.valueOf(GATEWAY_ID), payload.get("gateway_id").getAsString()); + assertEquals("gw-uuid", payload.get("gateway_uuid").getAsString()); + assertEquals("172.16.0.5", payload.get("gateway_ip_address").getAsString()); + assertEquals("172.16.0.1", payload.get("gateway_gateway").getAsString()); + assertEquals("255.255.255.0", payload.get("gateway_netmask").getAsString()); + assertEquals("900", payload.get("gateway_vlan").getAsString()); + assertEquals("ready", payload.get("gateway_state").getAsString()); + } + + @Test + public void testCreatePrivateGatewayCarriesVpcAndRemainingGatewayFields() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.createPrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + // The script resolves the router from the VPC, so these must travel with the gateway. + assertEquals(String.valueOf(VPC_ID), payload.get("vpc_id").getAsString()); + assertEquals(String.valueOf(ZONE_ID), payload.get("zone_id").getAsString()); + assertEquals("enabled", payload.get("vpc_state").getAsString()); + assertEquals("10.0.0.0/8", payload.get("vpc_cidr").getAsString()); + + assertEquals("55", payload.get("gateway_network_id").getAsString()); + assertEquals(String.valueOf(ZONE_ID), payload.get("gateway_zone_id").getAsString()); + assertEquals("200", payload.get("gateway_physical_network_id").getAsString()); + assertEquals("false", payload.get("gateway_source_nat").getAsString()); + assertEquals("3", payload.get("gateway_network_acl_id").getAsString()); + assertEquals("vlan://900", payload.get("gateway_broadcast_uri").getAsString()); + } + + @Test + public void testDeletePrivateGatewayUsesDeleteCommand() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.deletePrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_DELETE_PRIVATE_GATEWAY); + assertEquals(String.valueOf(GATEWAY_ID), payload.get("gateway_id").getAsString()); + } + + @Test + public void testPrivateGatewayPayloadOmitsBlankOptionalFields() throws Exception { + PrivateGateway gateway = mockBarePrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.createPrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + // Blank is not the same as absent: a script that tests for the key must not see + // an empty address and configure a port with it. + assertFalse(payload.has("gateway_ip_address")); + assertFalse(payload.has("gateway_gateway")); + assertFalse(payload.has("gateway_netmask")); + assertFalse(payload.has("gateway_broadcast_uri")); + assertFalse(payload.has("gateway_vlan")); + assertFalse(payload.has("gateway_state")); + // The mandatory fields survive. + assertEquals(String.valueOf(GATEWAY_ID), payload.get("gateway_id").getAsString()); + assertEquals("true", payload.get("gateway_source_nat").getAsString()); + assertEquals("3", payload.get("gateway_network_acl_id").getAsString()); + } + + @Test + public void testPrivateGatewayPayloadOmitsVlanWhenBroadcastUriIsMalformed() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(gateway.getBroadcastUri()).thenReturn("vlan://bad host"); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.createPrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + // An unparsable URI must not abort the call; the raw value still goes out. + assertEquals("vlan://bad host", payload.get("gateway_broadcast_uri").getAsString()); + assertFalse(payload.has("gateway_vlan")); + } + + @Test + public void testPrivateGatewayPayloadOmitsVlanWhenBroadcastUriCarriesNone() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(gateway.getBroadcastUri()).thenReturn("untagged"); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.createPrivateGateway(gateway)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_CREATE_PRIVATE_GATEWAY); + assertEquals("untagged", payload.get("gateway_broadcast_uri").getAsString()); + assertFalse(payload.has("gateway_vlan")); + } + + @Test + public void testPrivateGatewayWithoutVpcIsRejected() throws Exception { + PrivateGateway gateway = mock(PrivateGateway.class); + when(gateway.getVpcId()).thenReturn(null); + + assertFalse(element.createPrivateGateway(gateway)); + + verify(element, never()).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + @Test + public void testPrivateGatewayWithMissingVpcIsRejected() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(null); + + assertFalse(element.deletePrivateGateway(gateway)); + + verify(element, never()).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + @Test + public void testCreatePrivateGatewayReturnsFalseWhenScriptFails() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(false); + + // createPrivateGateway reports failure by return value, unlike the apply* methods. + assertFalse(element.createPrivateGateway(gateway)); + } + + @Test + public void testDeletePrivateGatewayReturnsFalseWhenScriptFails() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(false); + + assertFalse(element.deletePrivateGateway(gateway)); + } + + // ---- applyStaticRoutes ---- + + private StaticRouteProfile mockRoute(String cidr, String nextHop, StaticRoute.State state) { + StaticRouteProfile route = mock(StaticRouteProfile.class); + lenient().when(route.getCidr()).thenReturn(cidr); + lenient().when(route.getNextHop()).thenReturn(nextHop); + lenient().when(route.getState()).thenReturn(state); + return route; + } + + @Test + public void testApplyStaticRoutesSkipsRevokedAndSortsByCidr() throws Exception { + stubScript(true); + + List routes = List.of( + mockRoute("10.2.0.0/16", "172.16.0.9", StaticRoute.State.Active), + mockRoute("10.1.0.0/16", "172.16.0.8", StaticRoute.State.Add), + mockRoute("10.3.0.0/16", "172.16.0.7", StaticRoute.State.Revoke)); + + assertTrue(element.applyStaticRoutes(vpc, routes)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + JsonArray array = payload.getAsJsonArray("static_routes"); + assertEquals(2, array.size()); + assertEquals("10.1.0.0/16", objectAt(array, 0).get("cidr").getAsString()); + assertEquals("10.2.0.0/16", objectAt(array, 1).get("cidr").getAsString()); + } + + @Test + public void testApplyStaticRoutesSerialisesEveryRouteField() throws Exception { + stubScript(true); + + StaticRouteProfile route = mockRoute("10.1.0.0/16", "172.16.0.8", StaticRoute.State.Active); + when(route.getId()).thenReturn(88L); + when(route.getVpcGatewayId()).thenReturn(31L); + when(route.getIp4Address()).thenReturn("172.16.0.5"); + when(route.getGateway()).thenReturn("172.16.0.1"); + when(route.getNetmask()).thenReturn("255.255.255.0"); + when(route.getVlanTag()).thenReturn("900"); + + assertTrue(element.applyStaticRoutes(vpc, List.of(route))); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + JsonObject json = objectAt(payload.getAsJsonArray("static_routes"), 0); + assertEquals(88L, json.get("id").getAsLong()); + assertEquals("10.1.0.0/16", json.get("cidr").getAsString()); + assertEquals("172.16.0.8", json.get("next_hop").getAsString()); + assertEquals("active", json.get("state").getAsString()); + assertEquals("31", json.get("vpc_gateway_id").getAsString()); + assertEquals("172.16.0.5", json.get("gateway_ip_address").getAsString()); + assertEquals("172.16.0.1", json.get("gateway").getAsString()); + assertEquals("255.255.255.0", json.get("netmask").getAsString()); + assertEquals("900", json.get("vlan").getAsString()); + } + + @Test + public void testApplyStaticRoutesOmitsBlankRouteFields() throws Exception { + stubScript(true); + + StaticRouteProfile route = mockRoute("10.1.0.0/16", "172.16.0.8", StaticRoute.State.Active); + when(route.getVpcGatewayId()).thenReturn(null); + when(route.getIp4Address()).thenReturn(""); + when(route.getGateway()).thenReturn(null); + when(route.getNetmask()).thenReturn(" "); + when(route.getVlanTag()).thenReturn(null); + + assertTrue(element.applyStaticRoutes(vpc, List.of(route))); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + JsonObject json = objectAt(payload.getAsJsonArray("static_routes"), 0); + assertFalse(json.has("vpc_gateway_id")); + assertFalse(json.has("gateway_ip_address")); + assertFalse(json.has("gateway")); + assertFalse(json.has("netmask")); + assertFalse(json.has("vlan")); + // A route that is not attached to a private gateway still carries its own fields. + assertEquals("10.1.0.0/16", json.get("cidr").getAsString()); + assertEquals("172.16.0.8", json.get("next_hop").getAsString()); + } + + @Test + public void testApplyStaticRoutesToleratesNullCidrWhenSorting() throws Exception { + stubScript(true); + + List routes = List.of( + mockRoute("10.1.0.0/16", "172.16.0.8", StaticRoute.State.Active), + mockRoute(null, "172.16.0.9", StaticRoute.State.Active)); + + // The comparator must not blow up on a half-built route. + assertTrue(element.applyStaticRoutes(vpc, routes)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + JsonArray array = payload.getAsJsonArray("static_routes"); + assertEquals(2, array.size()); + assertEquals("", objectAt(array, 0).get("cidr").getAsString()); + } + + @Test + public void testApplyStaticRoutesCarriesVpcIdentity() throws Exception { + stubScript(true); + + assertTrue(element.applyStaticRoutes(vpc, List.of())); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + assertEquals(String.valueOf(VPC_ID), payload.get("vpc_id").getAsString()); + assertEquals(String.valueOf(ZONE_ID), payload.get("zone_id").getAsString()); + } + + @Test + public void testApplyStaticRoutesWithNullListSendsEmptyArray() throws Exception { + stubScript(true); + + assertTrue(element.applyStaticRoutes(vpc, null)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_STATIC_ROUTES); + // An empty array means "no routes"; omitting the key would mean "unchanged". + assertTrue(payload.has("static_routes")); + assertEquals(0, payload.getAsJsonArray("static_routes").size()); + } + + @Test + public void testApplyStaticRoutesThrowsWhenScriptFails() { + stubScript(false); + + assertThrows(ResourceUnavailableException.class, + () -> element.applyStaticRoutes(vpc, List.of())); + } + + @Test + public void testApplyStaticRoutesWithoutVpcIsRejected() throws Exception { + assertFalse(element.applyStaticRoutes(null, List.of())); + + verify(element, never()).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + // ---- applyACLItemsToPrivateGw ---- + + private NetworkACLItem mockAclItem(int number, NetworkACLItem.State state) { + NetworkACLItem item = mock(NetworkACLItem.class); + lenient().when(item.getState()).thenReturn(state); + lenient().when(item.getNumber()).thenReturn(number); + lenient().when(item.getAction()).thenReturn(NetworkACLItem.Action.Allow); + lenient().when(item.getTrafficType()).thenReturn(NetworkACLItem.TrafficType.Ingress); + lenient().when(item.getProtocol()).thenReturn("tcp"); + return item; + } + + @Test + public void testApplyAclItemsToPrivateGwSkipsRevoked() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + NetworkACLItem active = mockAclItem(10, NetworkACLItem.State.Active); + NetworkACLItem revoked = mockAclItem(20, NetworkACLItem.State.Revoke); + + assertTrue(element.applyACLItemsToPrivateGw(gateway, List.of(active, revoked))); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL); + assertEquals(1, payload.getAsJsonArray("acl_rules").size()); + assertEquals(String.valueOf(GATEWAY_ID), payload.get("gateway_id").getAsString()); + } + + @Test + public void testApplyAclItemsToPrivateGwSortsByRuleNumber() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + List rules = List.of( + mockAclItem(30, NetworkACLItem.State.Active), + mockAclItem(10, NetworkACLItem.State.Add), + mockAclItem(20, NetworkACLItem.State.Active)); + + assertTrue(element.applyACLItemsToPrivateGw(gateway, rules)); + + JsonArray array = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL) + .getAsJsonArray("acl_rules"); + // Rule order is the evaluation order on the backend, so it has to be deterministic. + assertEquals(3, array.size()); + assertEquals(10, objectAt(array, 0).get("number").getAsInt()); + assertEquals(20, objectAt(array, 1).get("number").getAsInt()); + assertEquals(30, objectAt(array, 2).get("number").getAsInt()); + } + + @Test + public void testApplyAclItemsToPrivateGwSerialisesRuleFields() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + NetworkACLItem rule = mockAclItem(10, NetworkACLItem.State.Active); + when(rule.getAction()).thenReturn(NetworkACLItem.Action.Deny); + when(rule.getTrafficType()).thenReturn(NetworkACLItem.TrafficType.Egress); + when(rule.getSourcePortStart()).thenReturn(80); + when(rule.getSourcePortEnd()).thenReturn(443); + when(rule.getSourceCidrList()).thenReturn(List.of("10.1.0.0/16", "10.2.0.0/16")); + // Mockito hands back 0 rather than null for a boxed Integer, which would make + // the omit-when-null branch look taken when it was not. + when(rule.getIcmpType()).thenReturn(null); + when(rule.getIcmpCode()).thenReturn(null); + + assertTrue(element.applyACLItemsToPrivateGw(gateway, List.of(rule))); + + JsonObject json = objectAt(captureVpcPayload(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL) + .getAsJsonArray("acl_rules"), 0); + assertEquals("deny", json.get("action").getAsString()); + assertEquals("egress", json.get("trafficType").getAsString()); + assertEquals("tcp", json.get("protocol").getAsString()); + assertEquals(80, json.get("portStart").getAsInt()); + assertEquals(443, json.get("portEnd").getAsInt()); + assertFalse(json.has("icmpType")); + assertFalse(json.has("icmpCode")); + JsonArray cidrs = json.getAsJsonArray("sourceCidrs"); + assertEquals(2, cidrs.size()); + assertEquals("10.1.0.0/16", cidrs.get(0).getAsString()); + } + + @Test + public void testApplyAclItemsToPrivateGwCarriesVpcAndGatewayIdentity() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.applyACLItemsToPrivateGw(gateway, List.of())); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL); + // The rules belong to a gateway, but the script addresses the router by VPC. + assertEquals(String.valueOf(VPC_ID), payload.get("vpc_id").getAsString()); + assertEquals("gw-uuid", payload.get("gateway_uuid").getAsString()); + assertEquals("900", payload.get("gateway_vlan").getAsString()); + } + + @Test + public void testApplyAclItemsToPrivateGwWithNullRulesSendsEmptyArray() throws Exception { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(true); + + assertTrue(element.applyACLItemsToPrivateGw(gateway, null)); + + JsonObject payload = captureVpcPayload(NetworkExtensionElement.CMD_APPLY_PRIVATE_GW_ACL); + assertTrue(payload.has("acl_rules")); + assertEquals(0, payload.getAsJsonArray("acl_rules").size()); + } + + @Test + public void testApplyAclItemsToPrivateGwWithoutVpcIsRejected() throws Exception { + PrivateGateway gateway = mock(PrivateGateway.class); + when(gateway.getVpcId()).thenReturn(null); + + assertFalse(element.applyACLItemsToPrivateGw(gateway, List.of())); + + verify(element, never()).executeVpcScript(any(Vpc.class), anyString(), any(JsonObject.class)); + } + + @Test + public void testApplyAclItemsToPrivateGwThrowsWhenScriptFails() { + PrivateGateway gateway = mockPrivateGateway(); + when(vpcDao.findById(VPC_ID)).thenReturn(vpc); + stubScript(false); + + assertThrows(ResourceUnavailableException.class, + () -> element.applyACLItemsToPrivateGw(gateway, List.of())); + } +}