2323import com .cloud .agent .api .CheckS2SVpnConnectionsCommand ;
2424import com .cloud .agent .api .NetworkUsageAnswer ;
2525import com .cloud .agent .api .NetworkUsageCommand ;
26+ import com .cloud .agent .api .PlugNicAnswer ;
2627import com .cloud .agent .api .PlugNicCommand ;
2728import com .cloud .agent .api .PvlanSetupCommand ;
29+ import com .cloud .agent .api .SetupGuestNetworkAnswer ;
30+ import com .cloud .agent .api .SetupGuestNetworkCommand ;
31+ import com .cloud .agent .api .UnPlugNicAnswer ;
2832import com .cloud .agent .api .UnPlugNicCommand ;
2933import com .cloud .agent .api .routing .DhcpEntryCommand ;
34+ import com .cloud .agent .api .routing .IpAssocAnswer ;
3035import com .cloud .agent .api .routing .IpAssocCommand ;
3136import com .cloud .agent .api .routing .IpAssocVpcCommand ;
3237import com .cloud .agent .api .routing .LoadBalancerConfigCommand ;
38+ import com .cloud .agent .api .routing .NetworkElementCommand ;
39+ import com .cloud .agent .api .routing .SetFirewallRulesAnswer ;
3340import com .cloud .agent .api .routing .SetFirewallRulesCommand ;
41+ import com .cloud .agent .api .routing .SetNetworkACLAnswer ;
3442import com .cloud .agent .api .routing .SetNetworkACLCommand ;
43+ import com .cloud .agent .api .routing .SetPortForwardingRulesAnswer ;
3544import com .cloud .agent .api .routing .SetPortForwardingRulesCommand ;
3645import com .cloud .agent .api .routing .SetPortForwardingRulesVpcCommand ;
46+ import com .cloud .agent .api .routing .SetSourceNatAnswer ;
3747import com .cloud .agent .api .routing .SetSourceNatCommand ;
48+ import com .cloud .agent .api .routing .SetStaticNatRulesAnswer ;
3849import com .cloud .agent .api .routing .SetStaticNatRulesCommand ;
50+ import com .cloud .agent .api .routing .SetStaticRouteAnswer ;
3951import com .cloud .agent .api .routing .SetStaticRouteCommand ;
4052import com .cloud .agent .api .routing .Site2SiteVpnCfgCommand ;
53+ import com .cloud .agent .api .to .IpAddressTO ;
54+ import com .cloud .agent .api .to .PortForwardingRuleTO ;
55+ import com .cloud .simulator .MockVMVO ;
56+ import com .cloud .simulator .dao .MockVMDao ;
4157import com .cloud .utils .component .ManagerBase ;
58+ import org .apache .log4j .Logger ;
59+
60+ import javax .inject .Inject ;
4261
4362public class MockNetworkManagerImpl extends ManagerBase implements MockNetworkManager {
63+ private static final Logger s_logger = Logger .getLogger (MockVmManagerImpl .class );
64+
65+ @ Inject
66+ MockVMDao _mockVmDao ;
4467
4568 @ Override
4669 public Answer SetStaticNatRules (SetStaticNatRulesCommand cmd ) {
@@ -53,8 +76,22 @@ public Answer SetPortForwardingRules(SetPortForwardingRulesCommand cmd) {
5376 }
5477
5578 @ Override
56- public Answer SetFirewallRules (SetFirewallRulesCommand cmd ) {
57- return new Answer (cmd );
79+ public SetFirewallRulesAnswer SetFirewallRules (SetFirewallRulesCommand cmd ) {
80+ String [] results = new String [cmd .getRules ().length ];
81+ String routerIp = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
82+ if (routerIp == null ) {
83+ return new SetFirewallRulesAnswer (cmd , false , results );
84+ }
85+
86+ String [][] rules = cmd .generateFwRules ();
87+ StringBuilder sb = new StringBuilder ();
88+ String [] fwRules = rules [0 ];
89+ if (fwRules .length > 0 ) {
90+ for (int i = 0 ; i < fwRules .length ; i ++) {
91+ sb .append (fwRules [i ]).append (',' );
92+ }
93+ }
94+ return new SetFirewallRulesAnswer (cmd , true , results );
5895 }
5996
6097
@@ -84,38 +121,107 @@ public Answer setupPVLAN(PvlanSetupCommand cmd) {
84121 }
85122
86123 @ Override
87- public Answer plugNic (PlugNicCommand cmd ) {
88- return new Answer (cmd );
124+ public PlugNicAnswer plugNic (PlugNicCommand cmd ) {
125+ String vmname = cmd .getVmName ();
126+ if (_mockVmDao .findByVmName (vmname ) != null ) {
127+ s_logger .debug ("Plugged NIC (dev=" + cmd .getNic ().getDeviceId () + ", " + cmd .getNic ().getIp () + ") into " + cmd .getVmName ());
128+ return new PlugNicAnswer (cmd , true , "success" );
129+ }
130+ s_logger .error ("Plug NIC failed for (dev=" + cmd .getNic ().getDeviceId () + ", " + cmd .getNic ().getIp () + ") into " + cmd .getVmName ());
131+ return new PlugNicAnswer (cmd , false , "failure" );
89132 }
90133
91134 @ Override
92- public Answer unplugNic (UnPlugNicCommand cmd ) {
93- return new Answer (cmd );
135+ public UnPlugNicAnswer unplugNic (UnPlugNicCommand cmd ) {
136+ String vmname = cmd .getVmName ();
137+ if (_mockVmDao .findByVmName (vmname ) != null ) {
138+ s_logger .debug ("Plugged NIC (dev=" + cmd .getNic ().getDeviceId () + ", " + cmd .getNic ().getIp () + ") into " + cmd .getVmName ());
139+ return new UnPlugNicAnswer (cmd , true , "success" );
140+ }
141+ s_logger .error ("Plug NIC failed for (dev=" + cmd .getNic ().getDeviceId () + ", " + cmd .getNic ().getIp () + ") into " + cmd .getVmName ());
142+ return new UnPlugNicAnswer (cmd , false , "failure" );
94143 }
95144
96145 @ Override
97- public Answer ipAssoc (IpAssocVpcCommand cmd ) {
98- return new Answer (cmd );
146+ public IpAssocAnswer ipAssoc (IpAssocVpcCommand cmd ) {
147+ String [] results = new String [cmd .getIpAddresses ().length ];
148+ int i = 0 ;
149+ IpAddressTO [] ips = cmd .getIpAddresses ();
150+ for (IpAddressTO ip : ips ) {
151+ results [i ++] = ip .getPublicIp () + " - success" ;
152+ }
153+ return new IpAssocAnswer (cmd , results );
99154 }
100155
101156 @ Override
102- public Answer setSourceNat (SetSourceNatCommand cmd ) {
103- return new Answer (cmd );
157+ public SetSourceNatAnswer setSourceNat (SetSourceNatCommand cmd ) {
158+ return new SetSourceNatAnswer (cmd , true , "success" );
104159 }
105160
106161 @ Override
107- public Answer setNetworkAcl (SetNetworkACLCommand cmd ) {
108- return new Answer (cmd );
162+ public SetNetworkACLAnswer setNetworkAcl (SetNetworkACLCommand cmd ) {
163+ String [] results = new String [cmd .getRules ().length ];
164+ String routerName = cmd .getAccessDetail (NetworkElementCommand .ROUTER_NAME );
165+ String routerIp = cmd .getAccessDetail (NetworkElementCommand .ROUTER_IP );
166+
167+ StringBuilder sb = new StringBuilder ();
168+ sb .append (routerIp );
169+ sb .append (routerName );
170+
171+ String [][] rules = cmd .generateFwRules ();
172+ String [] aclRules = rules [0 ];
173+
174+ for (int i = 0 ; i < aclRules .length ; i ++) {
175+ sb .append (aclRules [i ]).append (',' );
176+ }
177+ return new SetNetworkACLAnswer (cmd , true , results );
109178 }
110179
111180 @ Override
112- public Answer setVpcPortForwards (SetPortForwardingRulesVpcCommand cmd ) {
113- return new Answer (cmd );
181+ public SetPortForwardingRulesAnswer setVpcPortForwards (SetPortForwardingRulesVpcCommand cmd ) {
182+ String [] results = new String [cmd .getRules ().length ];
183+ StringBuilder sb = new StringBuilder ();
184+ for (PortForwardingRuleTO rule : cmd .getRules ()) {
185+ sb .append ("src:" );
186+ sb .append (rule .getStringSrcPortRange ());
187+ sb .append ("dst:" );
188+ sb .append (rule .getStringDstPortRange ());
189+ }
190+ return new SetPortForwardingRulesAnswer (cmd , results , true );
114191 }
115192
116193 @ Override
117- public Answer setStaticRoute (SetStaticRouteCommand cmd ) {
118- return new Answer (cmd );
194+ public SetStaticRouteAnswer setStaticRoute (SetStaticRouteCommand cmd ) {
195+ String [] results = new String [cmd .getStaticRoutes ().length ];
196+ String [][] rules = cmd .generateSRouteRules ();
197+ StringBuilder sb = new StringBuilder ();
198+ String [] srRules = rules [0 ];
199+ for (int i = 0 ; i < srRules .length ; i ++) {
200+ sb .append (srRules [i ]).append (',' );
201+ }
202+ return new SetStaticRouteAnswer (cmd , true , results );
203+ }
204+
205+ @ Override
206+ public SetupGuestNetworkAnswer setUpGuestNetwork (SetupGuestNetworkCommand cmd ) {
207+ String domrName = cmd .getAccessDetail (NetworkElementCommand .ROUTER_NAME );
208+ try {
209+ MockVMVO vms = _mockVmDao .findByVmName (domrName );
210+ if (vms == null ) {
211+ return new SetupGuestNetworkAnswer (cmd , false , "Can not find VM " + domrName );
212+ }
213+ return new SetupGuestNetworkAnswer (cmd , true , "success" );
214+ } catch (Exception e ) {
215+ String msg = "Creating guest network failed due to " + e .toString ();
216+ s_logger .warn (msg , e );
217+ return new SetupGuestNetworkAnswer (cmd , false , msg );
218+ }
219+ }
220+
221+ @ Override
222+ public SetStaticNatRulesAnswer setVPCStaticNatRules (SetStaticNatRulesCommand cmd ) {
223+ String [] results = new String [cmd .getRules ().length ];
224+ return new SetStaticNatRulesAnswer (cmd , results , true );
119225 }
120226
121227 @ Override
0 commit comments