@Test public void testConnectGatewayToVpc() { Prefix vpcPrefix = Prefix.parse("10.10.0.0/16"); Vpc vpc = getTestVpc("vpc", ImmutableSet.of(vpcPrefix)); Configuration vpcCfg = new Configuration(vpc.getId(), ConfigurationFormat.AWS); Configuration gatewayCfg = Utils.newAwsConfiguration("gateway", "aws"); Region region = Region.builder("r1").setVpcs(ImmutableMap.of(vpcCfg.getHostname(), vpc)).build(); String vrfNameOnVpc = vrfNameForLink(gatewayCfg.getHostname()); vpcCfg .getVrfs() .put(vrfNameOnVpc, Vrf.builder().setName(vrfNameOnVpc).setOwner(vpcCfg).build()); ConvertedConfiguration awsConfiguration = new ConvertedConfiguration(ImmutableList.of(vpcCfg)); Interface gatewayIface = connectGatewayToVpc( gatewayCfg.getHostname(), gatewayCfg, vpcCfg.getHostname(), awsConfiguration, region, new Warnings()); assertThat( gatewayCfg.getAllInterfaces().keySet(), equalTo(ImmutableSet.of(Utils.interfaceNameToRemote(vpcCfg)))); assertThat( vpcCfg.getAllInterfaces().keySet(), equalTo(ImmutableSet.of(Utils.interfaceNameToRemote(gatewayCfg)))); Interface vpcIface = vpcCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(gatewayCfg)); assertThat( awsConfiguration.getLayer1Edges(), hasItems( new Layer1Edge( vpcCfg.getHostname(), vpcIface.getName(), gatewayCfg.getHostname(), gatewayIface.getName()))); assertThat( gatewayCfg.getDefaultVrf().getStaticRoutes(), equalTo( ImmutableSortedSet.of( toStaticRoute( vpcPrefix, gatewayIface.getName(), ((LinkLocalAddress) vpcIface.getAddress()).getIp())))); assertThat( vpcCfg.getVrfs().get(vrfNameForLink(gatewayCfg.getHostname())).getStaticRoutes(), equalTo( ImmutableSortedSet.of( toStaticRoute( Prefix.ZERO, vpcIface.getName(), ((LinkLocalAddress) gatewayIface.getAddress()).getIp())))); }
@Nullable static Interface connectGatewayToVpc( String gatewayId, Configuration gatewayCfg, String vpcId, ConvertedConfiguration awsConfiguration, Region region, Warnings warnings) { Vpc vpc = region.getVpcs().get(vpcId); if (vpc == null) { warnings.redFlag( String.format("VPC with id %s not found in region %s", vpcId, region.getName())); return null; } Configuration vpcCfg = awsConfiguration.getNode(Vpc.nodeName(vpc.getId())); if (vpcCfg == null) { warnings.redFlag(String.format("Configuration for VPC with id %s not found", vpcId)); return null; } String vrfNameOnVpc = Vpc.vrfNameForLink(gatewayId); if (!vpcCfg.getVrfs().containsKey(vrfNameOnVpc)) { warnings.redFlag(String.format("VRF %s not found on VPC %s", vrfNameOnVpc, vpcId)); return null; } connect(awsConfiguration, gatewayCfg, DEFAULT_VRF_NAME, vpcCfg, vrfNameOnVpc, ""); addStaticRoute( vpcCfg.getVrfs().get(vrfNameOnVpc), toStaticRoute( Prefix.ZERO, Utils.interfaceNameToRemote(gatewayCfg), Utils.getInterfaceLinkLocalIp(gatewayCfg, Utils.interfaceNameToRemote(vpcCfg)))); vpc.getCidrBlockAssociations() .forEach( prefix -> addStaticRoute( gatewayCfg, toStaticRoute( prefix, Utils.interfaceNameToRemote(vpcCfg), Utils.getInterfaceLinkLocalIp( vpcCfg, Utils.interfaceNameToRemote(gatewayCfg))))); return gatewayCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(vpcCfg)); }
Utils { @Nullable static Interface connectGatewayToVpc( String gatewayId, Configuration gatewayCfg, String vpcId, ConvertedConfiguration awsConfiguration, Region region, Warnings warnings) { Vpc vpc = region.getVpcs().get(vpcId); if (vpc == null) { warnings.redFlag( String.format("VPC with id %s not found in region %s", vpcId, region.getName())); return null; } Configuration vpcCfg = awsConfiguration.getNode(Vpc.nodeName(vpc.getId())); if (vpcCfg == null) { warnings.redFlag(String.format("Configuration for VPC with id %s not found", vpcId)); return null; } String vrfNameOnVpc = Vpc.vrfNameForLink(gatewayId); if (!vpcCfg.getVrfs().containsKey(vrfNameOnVpc)) { warnings.redFlag(String.format("VRF %s not found on VPC %s", vrfNameOnVpc, vpcId)); return null; } connect(awsConfiguration, gatewayCfg, DEFAULT_VRF_NAME, vpcCfg, vrfNameOnVpc, ""); addStaticRoute( vpcCfg.getVrfs().get(vrfNameOnVpc), toStaticRoute( Prefix.ZERO, Utils.interfaceNameToRemote(gatewayCfg), Utils.getInterfaceLinkLocalIp(gatewayCfg, Utils.interfaceNameToRemote(vpcCfg)))); vpc.getCidrBlockAssociations() .forEach( prefix -> addStaticRoute( gatewayCfg, toStaticRoute( prefix, Utils.interfaceNameToRemote(vpcCfg), Utils.getInterfaceLinkLocalIp( vpcCfg, Utils.interfaceNameToRemote(gatewayCfg))))); return gatewayCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(vpcCfg)); } }
Utils { @Nullable static Interface connectGatewayToVpc( String gatewayId, Configuration gatewayCfg, String vpcId, ConvertedConfiguration awsConfiguration, Region region, Warnings warnings) { Vpc vpc = region.getVpcs().get(vpcId); if (vpc == null) { warnings.redFlag( String.format("VPC with id %s not found in region %s", vpcId, region.getName())); return null; } Configuration vpcCfg = awsConfiguration.getNode(Vpc.nodeName(vpc.getId())); if (vpcCfg == null) { warnings.redFlag(String.format("Configuration for VPC with id %s not found", vpcId)); return null; } String vrfNameOnVpc = Vpc.vrfNameForLink(gatewayId); if (!vpcCfg.getVrfs().containsKey(vrfNameOnVpc)) { warnings.redFlag(String.format("VRF %s not found on VPC %s", vrfNameOnVpc, vpcId)); return null; } connect(awsConfiguration, gatewayCfg, DEFAULT_VRF_NAME, vpcCfg, vrfNameOnVpc, ""); addStaticRoute( vpcCfg.getVrfs().get(vrfNameOnVpc), toStaticRoute( Prefix.ZERO, Utils.interfaceNameToRemote(gatewayCfg), Utils.getInterfaceLinkLocalIp(gatewayCfg, Utils.interfaceNameToRemote(vpcCfg)))); vpc.getCidrBlockAssociations() .forEach( prefix -> addStaticRoute( gatewayCfg, toStaticRoute( prefix, Utils.interfaceNameToRemote(vpcCfg), Utils.getInterfaceLinkLocalIp( vpcCfg, Utils.interfaceNameToRemote(gatewayCfg))))); return gatewayCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(vpcCfg)); } private  Utils(); }
Utils { @Nullable static Interface connectGatewayToVpc( String gatewayId, Configuration gatewayCfg, String vpcId, ConvertedConfiguration awsConfiguration, Region region, Warnings warnings) { Vpc vpc = region.getVpcs().get(vpcId); if (vpc == null) { warnings.redFlag( String.format("VPC with id %s not found in region %s", vpcId, region.getName())); return null; } Configuration vpcCfg = awsConfiguration.getNode(Vpc.nodeName(vpc.getId())); if (vpcCfg == null) { warnings.redFlag(String.format("Configuration for VPC with id %s not found", vpcId)); return null; } String vrfNameOnVpc = Vpc.vrfNameForLink(gatewayId); if (!vpcCfg.getVrfs().containsKey(vrfNameOnVpc)) { warnings.redFlag(String.format("VRF %s not found on VPC %s", vrfNameOnVpc, vpcId)); return null; } connect(awsConfiguration, gatewayCfg, DEFAULT_VRF_NAME, vpcCfg, vrfNameOnVpc, ""); addStaticRoute( vpcCfg.getVrfs().get(vrfNameOnVpc), toStaticRoute( Prefix.ZERO, Utils.interfaceNameToRemote(gatewayCfg), Utils.getInterfaceLinkLocalIp(gatewayCfg, Utils.interfaceNameToRemote(vpcCfg)))); vpc.getCidrBlockAssociations() .forEach( prefix -> addStaticRoute( gatewayCfg, toStaticRoute( prefix, Utils.interfaceNameToRemote(vpcCfg), Utils.getInterfaceLinkLocalIp( vpcCfg, Utils.interfaceNameToRemote(gatewayCfg))))); return gatewayCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(vpcCfg)); } private  Utils(); @Nullable static IpProtocol toIpProtocol(String ipProtocolAsString); static TraceElement getTraceElementForRule(@Nullable String ruleDescription); static TraceElement getTraceElementForSecurityGroup(String securityGroupName); }
Utils { @Nullable static Interface connectGatewayToVpc( String gatewayId, Configuration gatewayCfg, String vpcId, ConvertedConfiguration awsConfiguration, Region region, Warnings warnings) { Vpc vpc = region.getVpcs().get(vpcId); if (vpc == null) { warnings.redFlag( String.format("VPC with id %s not found in region %s", vpcId, region.getName())); return null; } Configuration vpcCfg = awsConfiguration.getNode(Vpc.nodeName(vpc.getId())); if (vpcCfg == null) { warnings.redFlag(String.format("Configuration for VPC with id %s not found", vpcId)); return null; } String vrfNameOnVpc = Vpc.vrfNameForLink(gatewayId); if (!vpcCfg.getVrfs().containsKey(vrfNameOnVpc)) { warnings.redFlag(String.format("VRF %s not found on VPC %s", vrfNameOnVpc, vpcId)); return null; } connect(awsConfiguration, gatewayCfg, DEFAULT_VRF_NAME, vpcCfg, vrfNameOnVpc, ""); addStaticRoute( vpcCfg.getVrfs().get(vrfNameOnVpc), toStaticRoute( Prefix.ZERO, Utils.interfaceNameToRemote(gatewayCfg), Utils.getInterfaceLinkLocalIp(gatewayCfg, Utils.interfaceNameToRemote(vpcCfg)))); vpc.getCidrBlockAssociations() .forEach( prefix -> addStaticRoute( gatewayCfg, toStaticRoute( prefix, Utils.interfaceNameToRemote(vpcCfg), Utils.getInterfaceLinkLocalIp( vpcCfg, Utils.interfaceNameToRemote(gatewayCfg))))); return gatewayCfg.getAllInterfaces().get(Utils.interfaceNameToRemote(vpcCfg)); } private  Utils(); @Nullable static IpProtocol toIpProtocol(String ipProtocolAsString); static TraceElement getTraceElementForRule(@Nullable String ruleDescription); static TraceElement getTraceElementForSecurityGroup(String securityGroupName);  }