2020import com .cloud .baremetal .database .BaremetalRctDao ;
2121import com .cloud .baremetal .database .BaremetalRctVO ;
2222import com .cloud .baremetal .networkservice .BaremetalRctResponse ;
23+ import com .cloud .baremetal .networkservice .BaremetalSwitchBackend ;
24+ import com .cloud .baremetal .networkservice .BaremetalVlanStruct ;
25+ import com .cloud .deploy .DeployDestination ;
26+ import com .cloud .host .HostVO ;
27+ import com .cloud .host .dao .HostDao ;
28+ import com .cloud .network .Network ;
29+ import com .cloud .network .Networks ;
2330import com .cloud .user .Account ;
2431import com .cloud .user .AccountManager ;
2532import com .cloud .user .AccountVO ;
2936import com .cloud .utils .component .ManagerBase ;
3037import com .cloud .utils .db .QueryBuilder ;
3138import com .cloud .utils .db .SearchCriteria ;
39+ import com .cloud .utils .exception .CloudRuntimeException ;
40+ import com .cloud .vm .VirtualMachineProfile ;
3241import com .google .gson .Gson ;
3342import org .apache .cloudstack .api .AddBaremetalRctCmd ;
3443import org .apache .cloudstack .api .command .admin .user .RegisterCmd ;
3847import java .net .MalformedURLException ;
3948import java .net .URL ;
4049import java .util .ArrayList ;
50+ import java .util .HashMap ;
4151import java .util .List ;
52+ import java .util .Map ;
4253import java .util .UUID ;
4354
4455/**
@@ -50,12 +61,21 @@ public class BaremetalVlanManagerImpl extends ManagerBase implements BaremetalVl
5061 @ Inject
5162 private BaremetalRctDao rctDao ;
5263 @ Inject
64+ private HostDao hostDao ;
65+ @ Inject
5366 private AccountDao acntDao ;
5467 @ Inject
5568 private UserDao userDao ;
5669 @ Inject
5770 private AccountManager acntMgr ;
5871
72+ private Map <String , BaremetalSwitchBackend > backends = new HashMap <>();
73+
74+ private class RackPair {
75+ BaremetalRct .Rack rack ;
76+ BaremetalRct .HostEntry host ;
77+ }
78+
5979 @ Override
6080 public BaremetalRctResponse addRct (AddBaremetalRctCmd cmd ) {
6181 try {
@@ -87,11 +107,95 @@ public BaremetalRctResponse addRct(AddBaremetalRctCmd cmd) {
87107 }
88108 }
89109
110+ @ Override
111+ public void prepareVlan (Network nw , DeployDestination destHost ) {
112+ List <BaremetalRctVO > vos = rctDao .listAll ();
113+ if (vos .isEmpty ()) {
114+ throw new CloudRuntimeException ("no rack configuration found, please call addBaremetalRct to add one" );
115+ }
116+
117+ BaremetalRctVO vo = vos .get (0 );
118+ BaremetalRct rct = gson .fromJson (vo .getRct (), BaremetalRct .class );
119+
120+ RackPair rp = findRack (rct , destHost .getHost ().getPrivateMacAddress ());
121+ if (rp == null ) {
122+ throw new CloudRuntimeException (String .format ("cannot find any rack contains host[mac:%s], please double check your rack configuration file, update it and call addBaremetalRct again" , destHost .getHost ().getPrivateMacAddress ()));
123+ }
124+
125+ int vlan = Integer .parseInt (Networks .BroadcastDomainType .getValue (nw .getBroadcastUri ()));
126+ BaremetalSwitchBackend backend = getSwitchBackend (rp .rack .getL2Switch ().getType ());
127+ BaremetalVlanStruct struct = new BaremetalVlanStruct ();
128+ struct .setHostMac (rp .host .getMac ());
129+ struct .setPort (rp .host .getPort ());
130+ struct .setSwitchIp (rp .rack .getL2Switch ().getIp ());
131+ struct .setSwitchPassword (rp .rack .getL2Switch ().getPassword ());
132+ struct .setSwitchType (rp .rack .getL2Switch ().getType ());
133+ struct .setSwitchUsername (rp .rack .getL2Switch ().getUsername ());
134+ struct .setVlan (vlan );
135+ backend .prepareVlan (struct );
136+ }
137+
138+ @ Override
139+ public void releaseVlan (Network nw , VirtualMachineProfile vm ) {
140+ List <BaremetalRctVO > vos = rctDao .listAll ();
141+ if (vos .isEmpty ()) {
142+ throw new CloudRuntimeException ("no rack configuration found, please call addBaremetalRct to add one" );
143+ }
144+
145+ BaremetalRctVO vo = vos .get (0 );
146+ BaremetalRct rct = gson .fromJson (vo .getRct (), BaremetalRct .class );
147+ HostVO host = hostDao .findById (vm .getVirtualMachine ().getHostId ());
148+ RackPair rp = findRack (rct , host .getPrivateMacAddress ());
149+ assert rp != null : String .format ("where is my rack???" );
150+
151+ int vlan = Integer .parseInt (Networks .BroadcastDomainType .getValue (nw .getBroadcastUri ()));
152+ BaremetalVlanStruct struct = new BaremetalVlanStruct ();
153+ struct .setHostMac (rp .host .getMac ());
154+ struct .setPort (rp .host .getPort ());
155+ struct .setSwitchIp (rp .rack .getL2Switch ().getIp ());
156+ struct .setSwitchPassword (rp .rack .getL2Switch ().getPassword ());
157+ struct .setSwitchType (rp .rack .getL2Switch ().getType ());
158+ struct .setSwitchUsername (rp .rack .getL2Switch ().getUsername ());
159+ struct .setVlan (vlan );
160+ BaremetalSwitchBackend backend = getSwitchBackend (rp .rack .getL2Switch ().getType ());
161+ backend .removePortFromVlan (struct );
162+ }
163+
164+ private BaremetalSwitchBackend getSwitchBackend (String type ) {
165+ BaremetalSwitchBackend backend = backends .get (type );
166+ if (backend == null ) {
167+ throw new CloudRuntimeException (String .format ("cannot find switch backend[type:%s]" , type ));
168+ }
169+ return backend ;
170+ }
171+
172+ private RackPair findRack (BaremetalRct rct , String mac ) {
173+ for (BaremetalRct .Rack rack : rct .getRacks ()) {
174+ for (BaremetalRct .HostEntry host : rack .getHosts ()) {
175+ if (mac .equals (host .getMac ())) {
176+ RackPair p = new RackPair ();
177+ p .host = host ;
178+ p .rack = rack ;
179+ return p ;
180+ }
181+ }
182+ }
183+ return null ;
184+ }
185+
90186 @ Override
91187 public String getName () {
92188 return "Baremetal Vlan Manager" ;
93189 }
94190
191+
192+ @ Override
193+ public List <Class <?>> getCommands () {
194+ List <Class <?>> cmds = new ArrayList <Class <?>>();
195+ cmds .add (AddBaremetalRctCmd .class );
196+ return cmds ;
197+ }
198+
95199 @ Override
96200 public boolean start () {
97201 QueryBuilder <AccountVO > acntq = QueryBuilder .create (AccountVO .class );
@@ -126,12 +230,4 @@ public boolean start() {
126230 userDao .update (user .getId (), user );
127231 return true ;
128232 }
129-
130-
131- @ Override
132- public List <Class <?>> getCommands () {
133- List <Class <?>> cmds = new ArrayList <Class <?>>();
134- cmds .add (AddBaremetalRctCmd .class );
135- return cmds ;
136- }
137233}
0 commit comments