Skip to content

Commit dbdab98

Browse files
author
frank
committed
CLOUDSTACK-3426
UCS: Session cookie refresh must be supported. fixed resolved
1 parent 251efff commit dbdab98

2 files changed

Lines changed: 48 additions & 33 deletions

File tree

‎plugins/hypervisors/ucs/src/com/cloud/ucs/manager/UcsCommands.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
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
@@ -27,6 +27,14 @@ public static String loginCmd(String username, String password) {
2727
return cmd.dump();
2828
}
2929

30+
public static String refreshCmd(String username, String password, String cookie) {
31+
XmlObject cmd = new XmlObject("aaaRefresh");
32+
cmd.putElement("inName", username);
33+
cmd.putElement("inPassword", password);
34+
cmd.putElement("inCookie", cookie);
35+
return cmd.dump();
36+
}
37+
3038
public static String listComputeBlades(String cookie) {
3139
XmlObject cmd = new XmlObject("configResolveClass");
3240
cmd.putElement("classId", "computeBlade");

‎plugins/hypervisors/ucs/src/com/cloud/ucs/manager/UcsManagerImpl.java‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
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
@@ -17,8 +17,6 @@
1717
//
1818
package com.cloud.ucs.manager;
1919

20-
import java.io.File;
21-
import java.io.StringReader;
2220
import java.util.ArrayList;
2321
import java.util.HashMap;
2422
import java.util.List;
@@ -29,55 +27,47 @@
2927
import javax.ejb.Local;
3028
import javax.inject.Inject;
3129
import javax.naming.ConfigurationException;
32-
import javax.xml.bind.JAXBContext;
33-
import javax.xml.bind.Unmarshaller;
30+
31+
import org.apache.log4j.Logger;
3432

3533
import org.apache.cloudstack.api.AddUcsManagerCmd;
36-
import org.apache.cloudstack.api.ApiConstants;
3734
import org.apache.cloudstack.api.AssociateUcsProfileToBladeCmd;
38-
import org.apache.cloudstack.api.BaseCmd;
3935
import org.apache.cloudstack.api.ListUcsBladeCmd;
4036
import org.apache.cloudstack.api.ListUcsManagerCmd;
4137
import 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;
4538
import org.apache.cloudstack.api.response.ListResponse;
4639
import org.apache.cloudstack.api.response.UcsBladeResponse;
4740
import org.apache.cloudstack.api.response.UcsManagerResponse;
4841
import 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

5343
import com.cloud.dc.ClusterDetailsDao;
5444
import com.cloud.dc.DataCenterVO;
5545
import com.cloud.dc.dao.ClusterDao;
5646
import com.cloud.dc.dao.DataCenterDao;
5747
import com.cloud.host.HostVO;
5848
import com.cloud.host.dao.HostDao;
59-
import com.cloud.hypervisor.Hypervisor.HypervisorType;
60-
import com.cloud.org.Cluster;
6149
import com.cloud.resource.ResourceService;
6250
import com.cloud.ucs.database.UcsBladeDao;
6351
import com.cloud.ucs.database.UcsBladeVO;
6452
import com.cloud.ucs.database.UcsManagerDao;
6553
import com.cloud.ucs.database.UcsManagerVO;
6654
import com.cloud.ucs.structure.ComputeBlade;
55+
import com.cloud.ucs.structure.UcsCookie;
6756
import com.cloud.ucs.structure.UcsProfile;
6857
import com.cloud.utils.db.DB;
6958
import com.cloud.utils.db.SearchCriteria.Op;
7059
import com.cloud.utils.db.SearchCriteria2;
7160
import com.cloud.utils.db.SearchCriteriaService;
7261
import com.cloud.utils.db.Transaction;
7362
import com.cloud.utils.exception.CloudRuntimeException;
74-
import com.cloud.utils.script.Script;
7563
import com.cloud.utils.xmlobject.XmlObject;
7664
import com.cloud.utils.xmlobject.XmlObjectParser;
7765

7866
@Local(value = { UcsManager.class })
7967
public 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

Comments
 (0)