1919
2020import org .apache .cloudstack .network .topology .NetworkTopologyVisitor ;
2121
22+ import com .cloud .agent .api .routing .DhcpEntryCommand ;
23+ import com .cloud .agent .api .routing .NetworkElementCommand ;
24+ import com .cloud .agent .manager .Commands ;
25+ import com .cloud .dc .DataCenterVO ;
2226import com .cloud .deploy .DeployDestination ;
2327import com .cloud .exception .ResourceUnavailableException ;
2428import com .cloud .network .Network ;
29+ import com .cloud .network .Network .Provider ;
30+ import com .cloud .network .Network .Service ;
2531import com .cloud .network .router .VirtualRouter ;
32+ import com .cloud .offering .NetworkOffering ;
33+ import com .cloud .uservm .UserVm ;
34+ import com .cloud .utils .net .NetUtils ;
35+ import com .cloud .vm .Nic ;
2636import com .cloud .vm .NicProfile ;
37+ import com .cloud .vm .NicVO ;
38+ import com .cloud .vm .UserVmVO ;
2739import com .cloud .vm .VirtualMachineProfile ;
2840
2941public class DhcpEntryRules extends RuleApplier {
3042
31- private final NicProfile nic ;
32- private final VirtualMachineProfile profile ;
33- private final DeployDestination destination ;
43+ private final NicProfile _nic ;
44+ private final VirtualMachineProfile _profile ;
45+ private final DeployDestination _destination ;
46+
47+ private NicVO _nicVo ;
48+ private UserVmVO _userVM ;
3449
3550 public DhcpEntryRules (final Network network , final NicProfile nic , final VirtualMachineProfile profile , final DeployDestination destination ) {
3651 super (network );
3752
38- this . nic = nic ;
39- this . profile = profile ;
40- this . destination = destination ;
53+ _nic = nic ;
54+ _profile = profile ;
55+ _destination = destination ;
4156 }
4257
4358 @ Override
4459 public boolean accept (final NetworkTopologyVisitor visitor , final VirtualRouter router ) throws ResourceUnavailableException {
45- this ._router = router ;
60+ _router = router ;
61+
62+ _userVM = _userVmDao .findById (_profile .getId ());
63+ _userVmDao .loadDetails (_userVM );
64+ _nicVo = _nicDao .findById (_nic .getId ());
4665
4766 return visitor .visit (this );
4867 }
4968
5069 public NicProfile getNic () {
51- return nic ;
70+ return _nic ;
71+ }
72+
73+ public NicVO getNicVo () {
74+ return _nicVo ;
5275 }
5376
54- public VirtualMachineProfile getProfile () {
55- return profile ;
77+ public UserVmVO getUserVM () {
78+ return _userVM ;
5679 }
5780
5881 public DeployDestination getDestination () {
59- return destination ;
82+ return _destination ;
83+ }
84+
85+ public void createDhcpEntryCommand (final VirtualRouter router , final UserVm vm , final NicVO nic , final Commands cmds ) {
86+ final DhcpEntryCommand dhcpCommand = new DhcpEntryCommand (nic .getMacAddress (), nic .getIp4Address (), vm .getHostName (), nic .getIp6Address (),
87+ _networkModel .getExecuteInSeqNtwkElmtCmd ());
88+ final DataCenterVO dcVo = _dcDao .findById (router .getDataCenterId ());
89+ final Nic defaultNic = findGatewayIp (vm .getId ());
90+ String gatewayIp = defaultNic .getGateway ();
91+ if (gatewayIp != null && !gatewayIp .equals (nic .getGateway ())) {
92+ gatewayIp = "0.0.0.0" ;
93+ }
94+ dhcpCommand .setDefaultRouter (gatewayIp );
95+ dhcpCommand .setIp6Gateway (nic .getIp6Gateway ());
96+ String ipaddress = null ;
97+ final NicVO domrDefaultNic = findDefaultDnsIp (vm .getId ());
98+ if (domrDefaultNic != null ) {
99+ ipaddress = domrDefaultNic .getIp4Address ();
100+ }
101+ dhcpCommand .setDefaultDns (ipaddress );
102+ dhcpCommand .setDuid (NetUtils .getDuidLL (nic .getMacAddress ()));
103+ dhcpCommand .setDefault (nic .isDefaultNic ());
104+
105+ dhcpCommand .setAccessDetail (NetworkElementCommand .ROUTER_IP , _routerControlHelper .getRouterControlIp (router .getId ()));
106+ dhcpCommand .setAccessDetail (NetworkElementCommand .ROUTER_NAME , router .getInstanceName ());
107+ dhcpCommand .setAccessDetail (NetworkElementCommand .ROUTER_GUEST_IP , _routerControlHelper .getRouterIpInNetwork (nic .getNetworkId (), router .getId ()));
108+ dhcpCommand .setAccessDetail (NetworkElementCommand .ZONE_NETWORK_TYPE , dcVo .getNetworkType ().toString ());
109+
110+ cmds .addCommand ("dhcp" , dhcpCommand );
60111 }
112+
113+ private NicVO findGatewayIp (final long userVmId ) {
114+ final NicVO defaultNic = _nicDao .findDefaultNicForVM (userVmId );
115+ return defaultNic ;
116+ }
117+
118+ private NicVO findDefaultDnsIp (final long userVmId ) {
119+ final NicVO defaultNic = _nicDao .findDefaultNicForVM (userVmId );
120+
121+ // check if DNS provider is the domR
122+ if (!_networkModel .isProviderSupportServiceInNetwork (defaultNic .getNetworkId (), Service .Dns , Provider .VirtualRouter )) {
123+ return null ;
124+ }
125+
126+ final NetworkOffering offering = _networkOfferingDao .findById (_networkDao .findById (defaultNic .getNetworkId ()).getNetworkOfferingId ());
127+ if (offering .getRedundantRouter ()) {
128+ return findGatewayIp (userVmId );
129+ }
130+
131+ return null ;
132+ }
133+
61134}
0 commit comments