@@ -77,6 +77,7 @@ class NitroError {
7777 static final int NS_RESOURCE_NOT_EXISTS =258 ;
7878 static final int NS_NO_SERIVCE = 344 ;
7979 static final int NS_OPERATION_NOT_PERMITTED = 257 ;
80+ static final int NS_INTERFACE_ALREADY_BOUND_TO_VLAN = 2080 ;
8081}
8182
8283public class NetscalerResource implements ServerResource {
@@ -853,36 +854,54 @@ private synchronized Answer execute(ExternalNetworkResourceUsageCommand cmd, int
853854
854855 private void addGuestVlanAndSubnet (long vlanTag , String vlanSelfIp , String vlanNetmask , boolean guestVlan ) throws ExecutionException {
855856 try {
857+ // add vlan object for guest VLAN
856858 if (!nsVlanExists (vlanTag )) {
857- // add new vlan
858- vlan vlanObj = new vlan ();
859- vlanObj .set_id (vlanTag );
860- apiCallResult = vlan .add (_netscalerService , vlanObj );
861- if (apiCallResult .errorcode != 0 ) {
862- throw new ExecutionException ("Failed to add new vlan with tag:" + vlanTag + "due to" + apiCallResult .message );
859+ try {
860+ vlan vlanObj = new vlan ();
861+ vlanObj .set_id (vlanTag );
862+ apiCallResult = vlan .add (_netscalerService , vlanObj );
863+ if (apiCallResult .errorcode != 0 ) {
864+ throw new ExecutionException ("Failed to add new vlan with tag:" + vlanTag + "on the NetScaler device due to " + apiCallResult .message );
865+ }
866+ } catch (nitro_exception e ) {
867+ throw new ExecutionException ("Failed to add new vlan with tag:" + vlanTag + "on the NetScaler device due to " + e .getMessage ());
863868 }
864-
865- // add self-ip and subnet to the Netscaler
866- nsip selfIp = new nsip ();
867- selfIp .set_ipaddress (vlanSelfIp );
868- selfIp .set_netmask (vlanNetmask );
869- selfIp .set_type ("SNIP" );
870- apiCallResult = nsip .add (_netscalerService , selfIp );
871- if (apiCallResult .errorcode != 0 ) {
872- throw new ExecutionException ("Failed to add new self-ip due to " + apiCallResult .message );
869+ }
870+
871+ // add subnet IP object for this guest network
872+ if (!nsSnipExists (vlanSelfIp )) {
873+ try {
874+ nsip selfIp = new nsip ();
875+ selfIp .set_ipaddress (vlanSelfIp );
876+ selfIp .set_netmask (vlanNetmask );
877+ selfIp .set_type ("SNIP" );
878+ apiCallResult = nsip .add (_netscalerService , selfIp );
879+ if (apiCallResult .errorcode != 0 ) {
880+ throw new ExecutionException ("Failed to add SNIP object for the guest network on the Netscaler device due to " + apiCallResult .message );
881+ }
882+ } catch (nitro_exception e ) {
883+ throw new ExecutionException ("Failed to add SNIP object for the guest network on the Netscaler device due to " + e .getMessage ());
873884 }
885+ }
874886
875- //bind the vlan to guest subnet
876- vlan_nsip_binding ipVlanBinding = new vlan_nsip_binding ();
877- ipVlanBinding .set_id (vlanTag );
878- ipVlanBinding .set_ipaddress (vlanSelfIp );
879- ipVlanBinding .set_netmask (vlanNetmask );
880- apiCallResult = vlan_nsip_binding .add (_netscalerService , ipVlanBinding );
881- if (apiCallResult .errorcode != 0 ) {
882- throw new ExecutionException ("Failed to bind vlan with tag:" + vlanTag + " to the subnet due to" + apiCallResult .message );
887+ // bind the vlan object to subnet IP object
888+ if (!nsVlanNsipBindingExists (vlanTag , vlanSelfIp )) {
889+ try {
890+ vlan_nsip_binding ipVlanBinding = new vlan_nsip_binding ();
891+ ipVlanBinding .set_id (vlanTag );
892+ ipVlanBinding .set_ipaddress (vlanSelfIp );
893+ ipVlanBinding .set_netmask (vlanNetmask );
894+ apiCallResult = vlan_nsip_binding .add (_netscalerService , ipVlanBinding );
895+ if (apiCallResult .errorcode != 0 ) {
896+ throw new ExecutionException ("Failed to bind VLAN with tag:" + vlanTag + " to the subnet due to " + apiCallResult .message );
897+ }
898+ } catch (nitro_exception e ) {
899+ throw new ExecutionException ("Failed to bind VLAN with tage:" + vlanTag + " to the subnet due to " + e .getMessage ());
883900 }
901+ }
884902
885- // bind vlan to the private interface
903+ // bind vlan object to the private interface
904+ try {
886905 vlan_interface_binding vlanBinding = new vlan_interface_binding ();
887906 if (guestVlan ) {
888907 vlanBinding .set_ifnum (_privateInterface );
@@ -891,25 +910,20 @@ private void addGuestVlanAndSubnet(long vlanTag, String vlanSelfIp, String vlanN
891910 }
892911 vlanBinding .set_tagged (true );
893912 vlanBinding .set_id (vlanTag );
894- try {
895- apiCallResult = vlan_interface_binding .add (_netscalerService , vlanBinding );
896- } catch (nitro_exception e ) {
897- // FIXME: Vlan binding (subsequent unbind) to an interfaces will fail on the VPX created on Xen server and on
898- // NetScaler SDX appliance till the VPX fix to handle VLAN's is released. Relaxing this restriction NetScaler until then
899- if (!(_deviceName .equalsIgnoreCase ("NetscalerVPXLoadBalancer" ) && e .getErrorCode () == NitroError .NS_OPERATION_NOT_PERMITTED )) {
900- throw new ExecutionException ("Failed to bind vlan to the interface while implementing guest network on the Netscaler device due to " + e .getMessage ());
901- }
902- }
903-
913+ apiCallResult = vlan_interface_binding .add (_netscalerService , vlanBinding );
904914 if (apiCallResult .errorcode != 0 ) {
905915 String vlanInterface = guestVlan ? _privateInterface : _publicInterface ;
906916 throw new ExecutionException ("Failed to bind vlan with tag:" + vlanTag + " with the interface " + vlanInterface + " due to " + apiCallResult .message );
907917 }
918+ } catch (nitro_exception e ) {
919+ if (!(e .getErrorCode () == NitroError .NS_INTERFACE_ALREADY_BOUND_TO_VLAN )) {
920+ throw new ExecutionException ("Failed to bind VLAN " + vlanTag + " with interface on the Netscaler device due to " + e .getMessage ());
921+ }
908922 }
909923 } catch (nitro_exception e ) {
910924 throw new ExecutionException ("Failed to implement guest network on the Netscaler device due to " + e .getMessage ());
911925 } catch (Exception e ) {
912- throw new ExecutionException ("Failed to implement guest network on the Netscaler device " + e .getMessage ());
926+ throw new ExecutionException ("Failed to implement guest network on the Netscaler device due to " + e .getMessage ());
913927 }
914928 }
915929
@@ -982,17 +996,39 @@ private void deleteGuestVlan(long vlanTag, String vlanSelfIp, String vlanNetmask
982996 }
983997 }
984998
985- private boolean nsVlanExists (long vlanTag ) {
999+ private boolean nsVlanExists (long vlanTag ) throws ExecutionException {
9861000 try {
9871001 if (vlan .get (_netscalerService , new Long (vlanTag )) != null ) {
9881002 return true ;
1003+ } else {
1004+ return false ;
1005+ }
1006+ } catch (nitro_exception e ) {
1007+ if (e .getErrorCode () == NitroError .NS_RESOURCE_NOT_EXISTS ) {
1008+ return false ;
1009+ } else {
1010+ throw new ExecutionException ("Failed to verify VLAN exists on the NetScaler device due to " + e .getMessage ());
9891011 }
990- return false ;
9911012 } catch (Exception e ) {
992- return false ;
1013+ throw new ExecutionException ( "Failed to verify VLAN exists on the NetScaler device due to " + e . getMessage ()) ;
9931014 }
9941015 }
995-
1016+
1017+ private boolean nsSnipExists (String subnetIP ) throws ExecutionException {
1018+ try {
1019+ nsip snip = nsip .get (_netscalerService , subnetIP );
1020+ return (snip != null );
1021+ } catch (nitro_exception e ) {
1022+ if (e .getErrorCode () == NitroError .NS_RESOURCE_NOT_EXISTS ) {
1023+ return false ;
1024+ } else {
1025+ throw new ExecutionException ("Failed to verify if SNIP exists on the NetScaler device due to " + e .getMessage ());
1026+ }
1027+ } catch (Exception e ) {
1028+ throw new ExecutionException ("Failed to verify if SNIP exists on the NetScaler device due to " + e .getMessage ());
1029+ }
1030+ }
1031+
9961032 private boolean nsServerExists (String serverName ) throws ExecutionException {
9971033 try {
9981034 if (com .citrix .netscaler .nitro .resource .config .basic .server .get (_netscalerService , serverName ) != null ) {
@@ -1004,10 +1040,29 @@ private boolean nsServerExists(String serverName) throws ExecutionException {
10041040 if (e .getErrorCode () == NitroError .NS_RESOURCE_NOT_EXISTS ) {
10051041 return false ;
10061042 } else {
1007- throw new ExecutionException (e .getMessage ());
1043+ throw new ExecutionException ("Failed to verify Server " + serverName + " exists on the NetScaler device due to " + e .getMessage ());
10081044 }
10091045 } catch (Exception e ) {
1010- throw new ExecutionException (e .getMessage ());
1046+ throw new ExecutionException ("Failed to verify Server " + serverName + " exists on the NetScaler device due to " + e .getMessage ());
1047+ }
1048+ }
1049+
1050+ private boolean nsVlanNsipBindingExists (long vlanTag , String vlanSelfIp ) throws ExecutionException {
1051+ try {
1052+ vlan_nsip_binding [] vlanNsipBindings = vlan_nsip_binding .get (_netscalerService , vlanTag );
1053+ if (vlanNsipBindings != null && vlanNsipBindings [0 ] != null && vlanNsipBindings [0 ].get_ipaddress ().equalsIgnoreCase (vlanSelfIp )) {
1054+ return true ;
1055+ } else {
1056+ return false ;
1057+ }
1058+ } catch (nitro_exception e ) {
1059+ if (e .getErrorCode () == NitroError .NS_RESOURCE_NOT_EXISTS ) {
1060+ return false ;
1061+ } else {
1062+ throw new ExecutionException ("Failed to verify Vlan " + vlanTag + " to SNIP " + vlanSelfIp + " binding exists due to " + e .getMessage ());
1063+ }
1064+ } catch (Exception e ) {
1065+ throw new ExecutionException ("Failed to verify Vlan " + vlanTag + " to SNIP " + vlanSelfIp + " binding exists due to " + e .getMessage ());
10111066 }
10121067 }
10131068
0 commit comments