55// to you under the Apache License, Version 2.0 (the
66// "License"); you may not use this file except in compliance
77// with the License. You may obtain a copy of the License at
8- //
8+ //
99// http://www.apache.org/licenses/LICENSE-2.0
10- //
10+ //
1111// Unless required by applicable law or agreed to in writing,
1212// software distributed under the License is distributed on an
1313// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1717//
1818package com .cloud .ucs .manager ;
1919
20- import java .io .File ;
21- import java .io .StringReader ;
2220import java .util .ArrayList ;
2321import java .util .HashMap ;
2422import java .util .List ;
2927import javax .ejb .Local ;
3028import javax .inject .Inject ;
3129import javax .naming .ConfigurationException ;
32- import javax . xml . bind . JAXBContext ;
33- import javax . xml . bind . Unmarshaller ;
30+
31+ import org . apache . log4j . Logger ;
3432
3533import org .apache .cloudstack .api .AddUcsManagerCmd ;
36- import org .apache .cloudstack .api .ApiConstants ;
3734import org .apache .cloudstack .api .AssociateUcsProfileToBladeCmd ;
38- import org .apache .cloudstack .api .BaseCmd ;
3935import org .apache .cloudstack .api .ListUcsBladeCmd ;
4036import org .apache .cloudstack .api .ListUcsManagerCmd ;
4137import org .apache .cloudstack .api .ListUcsProfileCmd ;
42- import org .apache .cloudstack .api .ResponseGenerator ;
43- import org .apache .cloudstack .api .ServerApiException ;
44- import org .apache .cloudstack .api .response .ClusterResponse ;
4538import org .apache .cloudstack .api .response .ListResponse ;
4639import org .apache .cloudstack .api .response .UcsBladeResponse ;
4740import org .apache .cloudstack .api .response .UcsManagerResponse ;
4841import org .apache .cloudstack .api .response .UcsProfileResponse ;
49- import org .apache .cxf .helpers .FileUtils ;
50- import org .apache .log4j .Logger ;
51- import org .springframework .stereotype .Component ;
5242
5343import com .cloud .dc .ClusterDetailsDao ;
5444import com .cloud .dc .DataCenterVO ;
5545import com .cloud .dc .dao .ClusterDao ;
5646import com .cloud .dc .dao .DataCenterDao ;
5747import com .cloud .host .HostVO ;
5848import com .cloud .host .dao .HostDao ;
59- import com .cloud .hypervisor .Hypervisor .HypervisorType ;
60- import com .cloud .org .Cluster ;
6149import com .cloud .resource .ResourceService ;
6250import com .cloud .ucs .database .UcsBladeDao ;
6351import com .cloud .ucs .database .UcsBladeVO ;
6452import com .cloud .ucs .database .UcsManagerDao ;
6553import com .cloud .ucs .database .UcsManagerVO ;
6654import com .cloud .ucs .structure .ComputeBlade ;
55+ import com .cloud .ucs .structure .UcsCookie ;
6756import com .cloud .ucs .structure .UcsProfile ;
6857import com .cloud .utils .db .DB ;
6958import com .cloud .utils .db .SearchCriteria .Op ;
7059import com .cloud .utils .db .SearchCriteria2 ;
7160import com .cloud .utils .db .SearchCriteriaService ;
7261import com .cloud .utils .db .Transaction ;
7362import com .cloud .utils .exception .CloudRuntimeException ;
74- import com .cloud .utils .script .Script ;
7563import com .cloud .utils .xmlobject .XmlObject ;
7664import com .cloud .utils .xmlobject .XmlObjectParser ;
7765
7866@ Local (value = { UcsManager .class })
7967public class UcsManagerImpl implements UcsManager {
8068 public static final Logger s_logger = Logger .getLogger (UcsManagerImpl .class );
69+ public static final Long COOKIE_TTL = TimeUnit .MILLISECONDS .convert (100L , TimeUnit .MINUTES );
70+ public static final Long COOKIE_REFRESH_TTL = TimeUnit .MILLISECONDS .convert (10L , TimeUnit .MINUTES );
8171
8272 @ Inject
8373 private UcsManagerDao ucsDao ;
@@ -94,7 +84,7 @@ public class UcsManagerImpl implements UcsManager {
9484 @ Inject
9585 private DataCenterDao dcDao ;
9686
97- private Map <Long , String > cookies = new HashMap <Long , String >();
87+ private final Map <Long , UcsCookie > cookies = new HashMap <Long , UcsCookie >();
9888 private String name ;
9989 private int runLevel ;
10090 private Map <String , Object > params ;
@@ -166,23 +156,38 @@ public UcsManagerResponse addUcsManager(AddUcsManagerCmd cmd) {
166156
167157 private String getCookie (Long ucsMgrId ) {
168158 try {
169- String cookie = cookies .get (ucsMgrId );
170- if (cookie == null ) {
171- UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
172- UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
173- String login = UcsCommands .loginCmd (mgrvo .getUsername (), mgrvo .getPassword ());
174- String ret = client .call (login );
159+ UcsCookie ucsCookie = cookies .get (ucsMgrId );
160+ long currentTime = System .currentTimeMillis ();
161+ UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
162+ UcsHttpClient client = new UcsHttpClient (mgrvo .getUrl ());
163+ String cmd = null ;
164+ if (ucsCookie == null ) {
165+ cmd = UcsCommands .loginCmd (mgrvo .getUsername (), mgrvo .getPassword ());
166+ }
167+ else {
168+ String cookie = ucsCookie .getCookie ();
169+ long cookieStartTime = ucsCookie .getStartTime ();
170+ if (currentTime - cookieStartTime > COOKIE_TTL ) {
171+ cmd = UcsCommands .loginCmd (mgrvo .getUsername (), mgrvo .getPassword ());
172+ }
173+ else if (currentTime - cookieStartTime > COOKIE_REFRESH_TTL ) {
174+ cmd = UcsCommands .refreshCmd (mgrvo .getUsername (), mgrvo .getPassword (), cookie );
175+ }
176+ }
177+ if (!(cmd == null )) {
178+ String ret = client .call (cmd );
175179 XmlObject xo = XmlObjectParser .parseFromString (ret );
176- cookie = xo .get ("outCookie" );
177- cookies .put (ucsMgrId , cookie );
180+ String cookie = xo .get ("outCookie" );
181+ ucsCookie = new UcsCookie (cookie , currentTime );
182+ cookies .put (ucsMgrId , ucsCookie );
183+ //cookiesTime.put(cookie, currentTime); //This is currentTime on purpose, and not latest time.
178184 }
179-
180- return cookie ;
185+ return ucsCookie .getCookie ();
181186 } catch (Exception e ) {
182187 throw new CloudRuntimeException ("Cannot get cookie" , e );
183188 }
184189 }
185-
190+
186191 private List <ComputeBlade > listBlades (Long ucsMgrId ) {
187192 String cookie = getCookie (ucsMgrId );
188193 UcsManagerVO mgrvo = ucsDao .findById (ucsMgrId );
@@ -234,6 +239,7 @@ private boolean isProfileAssociated(Long ucsMgrId, String dn) {
234239 String cmd = UcsCommands .configResolveDn (cookie , dn );
235240 String res = client .call (cmd );
236241 XmlObject xo = XmlObjectParser .parseFromString (res );
242+ s_logger .debug (String .format ("association response is %s" , res ));
237243 return xo .get ("outConfig.computeBlade.association" ).equals ("associated" );
238244 }
239245
@@ -335,6 +341,7 @@ private UcsBladeResponse bladeVOToResponse(UcsBladeVO vo) {
335341 return rsp ;
336342 }
337343
344+ @ Override
338345 public ListResponse <UcsBladeResponse > listUcsBlades (ListUcsBladeCmd cmd ) {
339346 SearchCriteriaService <UcsBladeVO , UcsBladeVO > serv = SearchCriteria2 .create (UcsBladeVO .class );
340347 serv .addAnd (serv .getEntity ().getUcsManagerId (), Op .EQ , cmd .getUcsManagerId ());
@@ -364,7 +371,7 @@ public void setConfigParams(Map<String, Object> params) {
364371
365372 @ Override
366373 public Map <String , Object > getConfigParams () {
367- return this . params ;
374+ return params ;
368375 }
369376
370377 @ Override
@@ -374,7 +381,7 @@ public int getRunLevel() {
374381
375382 @ Override
376383 public void setRunLevel (int level ) {
377- this . runLevel = level ;
384+ runLevel = level ;
378385 }
379386
380387 @ Override
0 commit comments