2323import org .apache .cloudstack .api .response .ListResponse ;
2424import org .apache .cxf .helpers .FileUtils ;
2525import org .apache .log4j .Logger ;
26+ import org .springframework .stereotype .Component ;
2627
2728import com .cloud .dc .ClusterDetailsDao ;
2829import com .cloud .dc .dao .ClusterDao ;
2930import com .cloud .host .HostVO ;
3031import com .cloud .hypervisor .Hypervisor .HypervisorType ;
3132import com .cloud .org .Cluster ;
3233import com .cloud .resource .ResourceService ;
34+ import com .cloud .ucs .database .UcsBladeDao ;
35+ import com .cloud .ucs .database .UcsBladeVO ;
3336import com .cloud .ucs .database .UcsManagerDao ;
3437import com .cloud .ucs .database .UcsManagerVO ;
3538import com .cloud .ucs .structure .ComputeBlade ;
4548import com .cloud .utils .xmlobject .XmlObjectParser ;
4649
4750@ Local (value = { UcsManager .class })
51+ @ Component
4852public class UcsManagerImpl implements UcsManager {
4953 public static final Logger s_logger = Logger .getLogger (UcsManagerImpl .class );
5054
@@ -56,6 +60,8 @@ public class UcsManagerImpl implements UcsManager {
5660 private ClusterDao clusterDao ;
5761 @ Inject
5862 private ClusterDetailsDao clusterDetailsDao ;
63+ @ Inject
64+ private UcsBladeDao bladeDao ;
5965
6066 private Map <Long , String > cookies = new HashMap <Long , String >();
6167
@@ -79,6 +85,17 @@ public String getName() {
7985 return "UcsManager" ;
8086 }
8187
88+ private void discoverBlades (UcsManagerVO ucsMgrVo ) {
89+ List <ComputeBlade > blades = listBlades (ucsMgrVo .getId ());
90+ for (ComputeBlade b : blades ) {
91+ UcsBladeVO vo = new UcsBladeVO ();
92+ vo .setDn (b .getDn ());
93+ vo .setUcsManagerId (ucsMgrVo .getId ());
94+ vo .setUuid (UUID .randomUUID ().toString ());
95+ bladeDao .persist (vo );
96+ }
97+ }
98+
8299 @ Override
83100 @ DB
84101 public AddUcsManagerResponse addUcsManager (AddUcsManagerCmd cmd ) {
@@ -99,18 +116,12 @@ public AddUcsManagerResponse addUcsManager(AddUcsManagerCmd cmd) {
99116 rsp .setName (vo .getName ());
100117 rsp .setUrl (vo .getUrl ());
101118 rsp .setZoneId (String .valueOf (vo .getZoneId ()));
119+
120+ discoverBlades (vo );
121+
102122 return rsp ;
103123 }
104124
105- private String getUcsManagerIp () {
106- SearchCriteriaService <UcsManagerVO , UcsManagerVO > serv = SearchCriteria2 .create (UcsManagerVO .class );
107- List <UcsManagerVO > vos = serv .list ();
108- if (vos .isEmpty ()) {
109- throw new CloudRuntimeException ("Cannot find any UCS manager, you must add it first" );
110- }
111- return vos .get (0 ).getUrl ();
112- }
113-
114125 private String getCookie (Long ucsMgrId ) {
115126 try {
116127 String cookie = cookies .get (ucsMgrId );
@@ -172,11 +183,7 @@ private String cloneProfile(Long ucsMgrId, String srcDn, String newProfileName)
172183 return xo .get ("lsClone.outConfig.lsServer.dn" );
173184 }
174185
175- private String buildProfileNameForHost (HostVO vo ) {
176- return String .format ("z%sp%sc%sh%s" , vo .getDataCenterId (), vo .getPodId (), vo .getClusterId (), vo .getId ());
177- }
178-
179- private boolean isBladeAssociated (Long ucsMgrId , String dn ) {
186+ private boolean isProfileAssociated (Long ucsMgrId , String dn ) {
180187 UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
181188 UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
182189 String cookie = getCookie (ucsMgrId );
@@ -188,6 +195,45 @@ private boolean isBladeAssociated(Long ucsMgrId, String dn) {
188195
189196 @ Override
190197 public void associateProfileToBlade (AssociateUcsProfileToBladeCmd cmd ) {
198+ SearchCriteriaService <UcsBladeVO , UcsBladeVO > q = SearchCriteria2 .create (UcsBladeVO .class );
199+ q .addAnd (q .getEntity ().getUcsManagerId (), Op .EQ , cmd .getUcsManagerId ());
200+ q .addAnd (q .getEntity ().getId (), Op .EQ , cmd .getBladeId ());
201+ UcsBladeVO bvo = q .find ();
202+ if (bvo == null ) {
203+ throw new IllegalArgumentException (String .format ("cannot find UCS blade[id:%s, ucs manager id:%s]" , cmd .getBladeId (), cmd .getUcsManagerId ()));
204+ }
205+
206+ if (bvo .getHostId () != null ) {
207+ throw new CloudRuntimeException (String .format ("blade[id:%s, dn:%s] has been associated with host[id:%s]" , bvo .getId (), bvo .getDn (), bvo .getHostId ()));
208+ }
209+
210+ UcsManagerVO mgrvo = ucsDao .findById (cmd .getUcsManagerId ());
211+ String cookie = getCookie (cmd .getUcsManagerId ());
212+ String pdn = cloneProfile (mgrvo .getId (), cmd .getProfileDn (), "profile-for-blade-" + bvo .getId ());
213+ String ucscmd = UcsCommands .associateProfileToBlade (cookie , pdn , bvo .getDn ());
214+ UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
215+ String res = client .call (ucscmd );
216+ int count = 0 ;
217+ int timeout = 600 ;
218+ while (count < timeout ) {
219+ if (isProfileAssociated (mgrvo .getId (), bvo .getDn ())) {
220+ break ;
221+ }
222+
223+ try {
224+ TimeUnit .SECONDS .sleep (2 );
225+ } catch (InterruptedException e ) {
226+ throw new CloudRuntimeException (e );
227+ }
228+
229+ count += 2 ;
230+ }
231+
232+ if (count >= timeout ) {
233+ throw new CloudRuntimeException (String .format ("associating profile[%s] to balde[%s] timeout after 600 seconds" , pdn , bvo .getDn ()));
234+ }
235+
236+ s_logger .debug (String .format ("successfully associated profile[%s] to blade[%s]" , pdn , bvo .getDn ()));
191237 }
192238
193239 @ Override
0 commit comments