Skip to content

Commit 973a9c1

Browse files
ming416yadvr
authored andcommitted
CLOUDSTACK-10003 automatic configure juniper srx/vsrx nat loopback, (#2184)
Automatic configure juniper srx/vsrx nat loopback, Constraint condition that manual configure source nat in juniper srx,allowed vm vist public network : zone : trust to {trust,untrust} rule : source address {0.0.0.0/0},destination address{0.0.0.0/0} ,do source nat with pool {public network getway ip}. code change for trust to trust destination or static nat: 1. add srxCommand :CHECK_PRIVATE_IF_EXISTS. for add/delete rule to detect whether exist or not contain DestinationNatRule or StaticNatRule (ruleName_private) in trust zone . 2. add DestinationNatRule (ruleName_private) to trust zone when ADD DestinationNatRule to untrust . 3. delete DestinationNatRule (ruleName_private) from trust zone when DELETE DestinationNatRule from untrust. 4. add StaticNatRule (ruleName_private) to trust zone when ADD StaticNatRule to untrust . 5. delete StaticNatRule (ruleName_private) from trust zone when DELETE StaticNatRule from untrust. Check private if exist.
1 parent 566238b commit 973a9c1

1 file changed

Lines changed: 68 additions & 3 deletions

File tree

plugins/network-elements/juniper-srx/src/com/cloud/network/resource/JuniperSrxResource.java

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public String getCountName() {
303303
}
304304

305305
private enum SrxCommand {
306-
LOGIN, OPEN_CONFIGURATION, CLOSE_CONFIGURATION, COMMIT, ROLLBACK, CHECK_IF_EXISTS, CHECK_IF_IN_USE, ADD, DELETE, GET_ALL;
306+
LOGIN, OPEN_CONFIGURATION, CLOSE_CONFIGURATION, COMMIT, ROLLBACK, CHECK_IF_EXISTS, CHECK_IF_IN_USE, ADD, DELETE, GET_ALL, CHECK_PRIVATE_IF_EXISTS;
307307
}
308308

309309
private enum Protocol {
@@ -2011,6 +2011,7 @@ private String genStaticNatRuleName(String publicIp, String privateIp) {
20112011

20122012
private boolean manageStaticNatRule(SrxCommand command, String publicIp, String privateIp) throws ExecutionException {
20132013
String ruleName = genStaticNatRuleName(publicIp, privateIp);
2014+
String ruleName_private = genStaticNatRuleName(privateIp, publicIp);
20142015
String xml;
20152016

20162017
switch (command) {
@@ -2022,7 +2023,13 @@ private boolean manageStaticNatRule(SrxCommand command, String publicIp, String
20222023
xml = replaceXmlValue(xml, "from-zone", _publicZone);
20232024
xml = replaceXmlValue(xml, "rule-name", ruleName);
20242025
return sendRequestAndCheckResponse(command, xml, "name", ruleName);
2025-
2026+
case CHECK_PRIVATE_IF_EXISTS:
2027+
xml = SrxXml.STATIC_NAT_RULE_GETONE.getXml();
2028+
xml = setDelete(xml, false);
2029+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2030+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2031+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2032+
return sendRequestAndCheckResponse(command, xml, "name", ruleName_private);
20262033
case ADD:
20272034
if (manageStaticNatRule(SrxCommand.CHECK_IF_EXISTS, publicIp, privateIp)) {
20282035
return true;
@@ -2038,6 +2045,16 @@ private boolean manageStaticNatRule(SrxCommand command, String publicIp, String
20382045
if (!sendRequestAndCheckResponse(command, xml)) {
20392046
throw new ExecutionException("Failed to add static NAT rule from public IP " + publicIp + " to private IP " + privateIp);
20402047
} else {
2048+
xml = SrxXml.STATIC_NAT_RULE_ADD.getXml();
2049+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2050+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2051+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2052+
xml = replaceXmlValue(xml, "original-ip", publicIp);
2053+
xml = replaceXmlValue(xml, "translated-ip", privateIp);
2054+
if (!sendRequestAndCheckResponse(command, xml))
2055+
{
2056+
throw new ExecutionException("Failed to add trust static NAT rule from public IP " + publicIp + " to private IP " + privateIp);
2057+
}
20412058
return true;
20422059
}
20432060

@@ -2055,6 +2072,18 @@ private boolean manageStaticNatRule(SrxCommand command, String publicIp, String
20552072
if (!sendRequestAndCheckResponse(command, xml, "name", ruleName)) {
20562073
throw new ExecutionException("Failed to delete static NAT rule from public IP " + publicIp + " to private IP " + privateIp);
20572074
} else {
2075+
if (manageStaticNatRule(SrxCommand.CHECK_PRIVATE_IF_EXISTS, publicIp, privateIp)){
2076+
xml = SrxXml.STATIC_NAT_RULE_GETONE.getXml();
2077+
xml = setDelete(xml, true);
2078+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2079+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2080+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2081+
}
2082+
2083+
if (!sendRequestAndCheckResponse(command, xml, "name", ruleName_private))
2084+
{
2085+
throw new ExecutionException("Failed to delete trust static NAT rule from public IP " + publicIp + " to private IP " + privateIp);
2086+
}
20582087
return true;
20592088
}
20602089

@@ -2184,6 +2213,7 @@ private String genDestinationNatRuleName(String publicIp, String privateIp, long
21842213

21852214
private boolean manageDestinationNatRule(SrxCommand command, String publicIp, String privateIp, long srcPort, long destPort) throws ExecutionException {
21862215
String ruleName = genDestinationNatRuleName(publicIp, privateIp, srcPort, destPort);
2216+
String ruleName_private = ruleName + "p";
21872217
String poolName = genDestinationNatPoolName(privateIp, destPort);
21882218
String xml;
21892219

@@ -2196,7 +2226,13 @@ private boolean manageDestinationNatRule(SrxCommand command, String publicIp, St
21962226
xml = replaceXmlValue(xml, "from-zone", _publicZone);
21972227
xml = replaceXmlValue(xml, "rule-name", ruleName);
21982228
return sendRequestAndCheckResponse(command, xml, "name", ruleName);
2199-
2229+
case CHECK_PRIVATE_IF_EXISTS:
2230+
xml = SrxXml.DEST_NAT_RULE_GETONE.getXml();
2231+
xml = setDelete(xml, false);
2232+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2233+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2234+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2235+
return sendRequestAndCheckResponse(command, xml, "name", ruleName_private);
22002236
case ADD:
22012237
if (manageDestinationNatRule(SrxCommand.CHECK_IF_EXISTS, publicIp, privateIp, srcPort, destPort)) {
22022238
return true;
@@ -2219,6 +2255,20 @@ private boolean manageDestinationNatRule(SrxCommand command, String publicIp, St
22192255
throw new ExecutionException("Failed to add destination NAT rule from public IP " + publicIp + ", public port " + srcPort + ", private IP " +
22202256
privateIp + ", and private port " + destPort);
22212257
} else {
2258+
2259+
xml = SrxXml.DEST_NAT_RULE_ADD.getXml();
2260+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2261+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2262+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2263+
xml = replaceXmlValue(xml, "public-address", publicIp);
2264+
xml = replaceXmlValue(xml, "src-port", String.valueOf(srcPort));
2265+
xml = replaceXmlValue(xml, "pool-name", poolName);
2266+
2267+
if (!sendRequestAndCheckResponse(command, xml))
2268+
{
2269+
s_logger.debug("Purple: loopback Failed to add " + _privateZone + " destination NAT rule from public IP " + publicIp + ", public port " + srcPort + ", private IP " +
2270+
privateIp + ", and private port " + destPort);
2271+
}
22222272
return true;
22232273
}
22242274

@@ -2237,6 +2287,21 @@ private boolean manageDestinationNatRule(SrxCommand command, String publicIp, St
22372287
throw new ExecutionException("Failed to delete destination NAT rule from public IP " + publicIp + ", public port " + srcPort + ", private IP " +
22382288
privateIp + ", and private port " + destPort);
22392289
} else {
2290+
if (manageDestinationNatRule(SrxCommand.CHECK_PRIVATE_IF_EXISTS, publicIp, privateIp, srcPort, destPort))
2291+
{
2292+
xml = SrxXml.DEST_NAT_RULE_GETONE.getXml();
2293+
xml = setDelete(xml, true);
2294+
xml = replaceXmlValue(xml, "rule-set", _privateZone);
2295+
xml = replaceXmlValue(xml, "from-zone", _privateZone);
2296+
xml = replaceXmlValue(xml, "rule-name", ruleName_private);
2297+
2298+
if (!sendRequestAndCheckResponse(command, xml))
2299+
{
2300+
s_logger.debug("Purple: Failed to delete " + _privateZone + " destination NAT rule from public IP " + publicIp + ", public port " + srcPort + ", private IP " +
2301+
privateIp + ", and private port " + destPort);
2302+
}
2303+
}
2304+
22402305
return true;
22412306
}
22422307

0 commit comments

Comments
 (0)