Skip to content

Commit 5297b52

Browse files
Improve the guest OS hypervisor mappings addition on upgrade. (apache#5911)
- This removes the hardcoded guest os ids 305 - 329 in upgrade path 4.14.0 to 4.15.0, and 330 - 349 in upgrade path 4.15.0.0 to 4.15.1.0.
1 parent a3bb84b commit 5297b52

File tree

17 files changed

+703
-346
lines changed

17 files changed

+703
-346
lines changed

api/src/main/java/com/cloud/storage/GuestOS.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ public interface GuestOS extends InternalIdentity, Identity {
3333

3434
Date getRemoved();
3535

36-
boolean isUserDefined();
36+
boolean getIsUserDefined();
3737
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.storage;
18+
19+
import org.apache.commons.lang3.StringUtils;
20+
21+
public class GuestOSHypervisorMapping {
22+
23+
private String hypervisorType;
24+
private String hypervisorVersion;
25+
private String guestOsName;
26+
27+
public GuestOSHypervisorMapping(String hypervisorType, String hypervisorVersion, String guestOsName) {
28+
this.hypervisorType = hypervisorType;
29+
this.hypervisorVersion = hypervisorVersion;
30+
this.guestOsName = guestOsName;
31+
}
32+
33+
public String getHypervisorType() {
34+
return hypervisorType;
35+
}
36+
37+
public String getHypervisorVersion() {
38+
return hypervisorVersion;
39+
}
40+
41+
public String getGuestOsName() {
42+
return guestOsName;
43+
}
44+
45+
public boolean isValid() {
46+
if (StringUtils.isBlank(hypervisorType) || StringUtils.isBlank(hypervisorVersion) || StringUtils.isBlank(guestOsName)) {
47+
return false;
48+
}
49+
50+
return true;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "Hypervisor(Version): " + hypervisorType + "(" + hypervisorVersion + "), Guest OS: " + guestOsName;
56+
}
57+
}

engine/schema/src/main/java/com/cloud/storage/GuestOSVO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public Date getRemoved() {
113113
}
114114

115115
@Override
116-
public boolean isUserDefined() {
116+
public boolean getIsUserDefined() {
117117
return isUserDefined;
118118
}
119119

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ public interface GuestOSDao extends GenericDao<GuestOSVO, Long> {
2323

2424
GuestOSVO listByDisplayName(String displayName);
2525

26+
GuestOSVO findByCategoryIdAndDisplayNameOrderByCreatedDesc(long categoryId, String displayName);
2627
}

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSDaoImpl.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
package com.cloud.storage.dao;
1818

1919

20+
import java.util.List;
21+
22+
import org.apache.commons.collections.CollectionUtils;
2023
import org.springframework.stereotype.Component;
2124

2225
import com.cloud.storage.GuestOSVO;
26+
import com.cloud.utils.db.Filter;
2327
import com.cloud.utils.db.GenericDaoBase;
2428
import com.cloud.utils.db.SearchBuilder;
2529
import com.cloud.utils.db.SearchCriteria;
@@ -29,9 +33,11 @@ public class GuestOSDaoImpl extends GenericDaoBase<GuestOSVO, Long> implements G
2933

3034
protected final SearchBuilder<GuestOSVO> Search;
3135

32-
protected GuestOSDaoImpl() {
36+
public GuestOSDaoImpl() {
3337
Search = createSearchBuilder();
38+
Search.and("category_id", Search.entity().getCategoryId(), SearchCriteria.Op.EQ);
3439
Search.and("display_name", Search.entity().getDisplayName(), SearchCriteria.Op.EQ);
40+
Search.and("is_user_defined", Search.entity().getIsUserDefined(), SearchCriteria.Op.EQ);
3541
Search.done();
3642
}
3743

@@ -42,4 +48,18 @@ public GuestOSVO listByDisplayName(String displayName) {
4248
return findOneBy(sc);
4349
}
4450

51+
@Override
52+
public GuestOSVO findByCategoryIdAndDisplayNameOrderByCreatedDesc(long categoryId, String displayName) {
53+
SearchCriteria<GuestOSVO> sc = Search.create();
54+
sc.setParameters("category_id", categoryId);
55+
sc.setParameters("display_name", displayName);
56+
sc.setParameters("is_user_defined", false);
57+
58+
Filter orderByFilter = new Filter(GuestOSVO.class, "created", false, null, 1L);
59+
List<GuestOSVO> guestOSes = listBy(sc, orderByFilter);
60+
if (CollectionUtils.isNotEmpty(guestOSes)) {
61+
return guestOSes.get(0);
62+
}
63+
return null;
64+
}
4565
}

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSHypervisorDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public interface GuestOSHypervisorDao extends GenericDao<GuestOSHypervisorVO, Lo
3434

3535
GuestOSHypervisorVO findByOsNameAndHypervisor(String guestOsName, String hypervisorType, String hypervisorVersion);
3636

37+
GuestOSHypervisorVO findByOsNameAndHypervisorOrderByCreatedDesc(String guestOsName, String hypervisorType, String hypervisorVersion);
38+
3739
List<GuestOSHypervisorVO> listByOsNameAndHypervisorMinimumVersion(String guestOsName, String hypervisorType,
3840
String minHypervisorVersion);
3941

engine/schema/src/main/java/com/cloud/storage/dao/GuestOSHypervisorDaoImpl.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class GuestOSHypervisorDaoImpl extends GenericDaoBase<GuestOSHypervisorVO
4040
protected final SearchBuilder<GuestOSHypervisorVO> guestOsNameSearch;
4141
protected final SearchBuilder<GuestOSHypervisorVO> availableHypervisorVersionSearch;
4242

43-
protected GuestOSHypervisorDaoImpl() {
43+
public GuestOSHypervisorDaoImpl() {
4444
guestOsSearch = createSearchBuilder();
4545
guestOsSearch.and("guest_os_id", guestOsSearch.entity().getGuestOsId(), SearchCriteria.Op.EQ);
4646
guestOsSearch.done();
@@ -62,6 +62,7 @@ protected GuestOSHypervisorDaoImpl() {
6262
guestOsNameSearch.and("guest_os_name", guestOsNameSearch.entity().getGuestOsName(), SearchCriteria.Op.EQ);
6363
guestOsNameSearch.and("hypervisor_type", guestOsNameSearch.entity().getHypervisorType(), SearchCriteria.Op.EQ);
6464
guestOsNameSearch.and("hypervisor_version", guestOsNameSearch.entity().getHypervisorVersion(), SearchCriteria.Op.EQ);
65+
guestOsNameSearch.and("is_user_defined", guestOsNameSearch.entity().getIsUserDefined(), SearchCriteria.Op.EQ);
6566
guestOsNameSearch.done();
6667

6768
availableHypervisorVersionSearch = createSearchBuilder();
@@ -133,6 +134,22 @@ public GuestOSHypervisorVO findByOsNameAndHypervisor(String guestOsName, String
133134
return CollectionUtils.isNotEmpty(results) ? results.get(0) : null;
134135
}
135136

137+
@Override
138+
public GuestOSHypervisorVO findByOsNameAndHypervisorOrderByCreatedDesc(String guestOsName, String hypervisorType, String hypervisorVersion) {
139+
SearchCriteria<GuestOSHypervisorVO> sc = guestOsNameSearch.create();
140+
sc.setParameters("guest_os_name", guestOsName);
141+
sc.setParameters("hypervisor_type", hypervisorType);
142+
sc.setParameters("hypervisor_version", hypervisorVersion);
143+
sc.setParameters("is_user_defined", false);
144+
145+
Filter orderByFilter = new Filter(GuestOSHypervisorVO.class, "created", false, null, 1L);
146+
List<GuestOSHypervisorVO> GuestOSHypervisorVOs = listBy(sc, orderByFilter);
147+
if (CollectionUtils.isNotEmpty(GuestOSHypervisorVOs)) {
148+
return GuestOSHypervisorVOs.get(0);
149+
}
150+
return null;
151+
}
152+
136153
@Override
137154
public List<GuestOSHypervisorVO> listByOsNameAndHypervisorMinimumVersion(String guestOsName, String hypervisorType,
138155
String minHypervisorVersion) {
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.upgrade;
18+
19+
import org.apache.commons.collections.CollectionUtils;
20+
import org.apache.log4j.Logger;
21+
22+
import java.sql.Connection;
23+
import java.sql.PreparedStatement;
24+
import java.sql.SQLException;
25+
import java.util.List;
26+
27+
import javax.inject.Inject;
28+
29+
import com.cloud.storage.GuestOSHypervisorMapping;
30+
import com.cloud.storage.GuestOSHypervisorVO;
31+
import com.cloud.storage.GuestOSVO;
32+
import com.cloud.storage.dao.GuestOSDao;
33+
import com.cloud.storage.dao.GuestOSDaoImpl;
34+
import com.cloud.storage.dao.GuestOSHypervisorDao;
35+
import com.cloud.storage.dao.GuestOSHypervisorDaoImpl;
36+
37+
public class GuestOsMapper {
38+
39+
final static Logger LOG = Logger.getLogger(GuestOsMapper.class);
40+
41+
@Inject
42+
GuestOSHypervisorDao guestOSHypervisorDao;
43+
@Inject
44+
GuestOSDao guestOSDao;
45+
46+
private static final String updateGuestOsHypervisorSql =
47+
"UPDATE `cloud`.`guest_os_hypervisor` SET guest_os_id = ? WHERE guest_os_id = ? AND hypervisor_type = ? AND hypervisor_version = ? AND guest_os_name = ? AND is_user_defined = 0 AND removed IS NULL";
48+
49+
public GuestOsMapper() {
50+
guestOSHypervisorDao = new GuestOSHypervisorDaoImpl();
51+
guestOSDao = new GuestOSDaoImpl();
52+
}
53+
54+
private long getGuestOsId(long categoryId, String displayName) {
55+
GuestOSVO guestOS = guestOSDao.findByCategoryIdAndDisplayNameOrderByCreatedDesc(categoryId, displayName);
56+
if (guestOS != null) {
57+
guestOS.getId();
58+
}
59+
60+
LOG.warn("Unable to find the guest OS details with category id: " + categoryId + " and display name: " + displayName);
61+
return 0;
62+
}
63+
64+
private long getGuestOsIdFromHypervisorMapping(GuestOSHypervisorMapping mapping) {
65+
GuestOSHypervisorVO guestOSHypervisorVO = guestOSHypervisorDao.findByOsNameAndHypervisorOrderByCreatedDesc(mapping.getGuestOsName(), mapping.getHypervisorType(), mapping.getHypervisorVersion());
66+
if (guestOSHypervisorVO != null) {
67+
guestOSHypervisorVO.getGuestOsId();
68+
}
69+
70+
LOG.debug("Unable to find the guest OS hypervisor mapping details for " + mapping.toString());
71+
return 0;
72+
}
73+
74+
public void addGuestOsAndHypervisorMappings(long categoryId, String displayName, List<GuestOSHypervisorMapping> mappings) {
75+
if (!addGuestOs(categoryId, displayName)) {
76+
LOG.warn("Couldn't add the guest OS with category id: " + categoryId + " and display name: " + displayName);
77+
return;
78+
}
79+
80+
if (CollectionUtils.isEmpty(mappings)) {
81+
return;
82+
}
83+
84+
long guestOsId = getGuestOsId(categoryId, displayName);
85+
if (guestOsId == 0) {
86+
LOG.debug("No guest OS found with category id: " + categoryId + " and display name: " + displayName);
87+
return;
88+
}
89+
90+
for (final GuestOSHypervisorMapping mapping : mappings) {
91+
addGuestOsHypervisorMapping(mapping, guestOsId);
92+
}
93+
}
94+
95+
private boolean addGuestOs(long categoryId, String displayName) {
96+
LOG.debug("Adding guest OS with category id: " + categoryId + " and display name: " + displayName);
97+
GuestOSVO guestOS = new GuestOSVO();
98+
guestOS.setCategoryId(categoryId);
99+
guestOS.setDisplayName(displayName);
100+
guestOS = guestOSDao.persist(guestOS);
101+
return (guestOS != null);
102+
}
103+
104+
public void addGuestOsHypervisorMapping(GuestOSHypervisorMapping mapping, long guestOsId) {
105+
if(!isValidGuestOSHypervisorMapping(mapping)) {
106+
return;
107+
}
108+
109+
LOG.debug("Adding guest OS hypervisor mapping - " + mapping.toString());
110+
GuestOSHypervisorVO guestOsMapping = new GuestOSHypervisorVO();
111+
guestOsMapping.setHypervisorType(mapping.getHypervisorType());
112+
guestOsMapping.setHypervisorVersion(mapping.getHypervisorVersion());
113+
guestOsMapping.setGuestOsName(mapping.getGuestOsName());
114+
guestOsMapping.setGuestOsId(guestOsId);
115+
guestOSHypervisorDao.persist(guestOsMapping);
116+
}
117+
118+
public void updateGuestOsName(long categoryId, String oldDisplayName, String newDisplayName) {
119+
GuestOSVO guestOS = guestOSDao.findByCategoryIdAndDisplayNameOrderByCreatedDesc(categoryId, oldDisplayName);
120+
if (guestOS == null) {
121+
LOG.debug("Unable to update guest OS name, as there is no guest OS with category id: " + categoryId + " and display name: " + oldDisplayName);
122+
return;
123+
}
124+
125+
guestOS.setDisplayName(newDisplayName);
126+
guestOSDao.update(guestOS.getId(), guestOS);
127+
}
128+
129+
public void updateGuestOsNameFromMapping(String newDisplayName, GuestOSHypervisorMapping mapping) {
130+
if(!isValidGuestOSHypervisorMapping(mapping)) {
131+
return;
132+
}
133+
134+
GuestOSHypervisorVO guestOSHypervisorVO = guestOSHypervisorDao.findByOsNameAndHypervisorOrderByCreatedDesc(mapping.getGuestOsName(), mapping.getHypervisorType(), mapping.getHypervisorVersion());
135+
if (guestOSHypervisorVO == null) {
136+
LOG.debug("Unable to update guest OS name, as there is no guest os hypervisor mapping");
137+
return;
138+
}
139+
140+
long guestOsId = guestOSHypervisorVO.getGuestOsId();
141+
GuestOSVO guestOS = guestOSDao.findById(guestOsId);
142+
if (guestOS != null) {
143+
guestOS.setDisplayName(newDisplayName);
144+
guestOSDao.update(guestOS.getId(), guestOS);
145+
}
146+
}
147+
148+
public void updateGuestOsIdInHypervisorMapping(Connection conn, long categoryId, String displayName, GuestOSHypervisorMapping mapping) {
149+
if(!isValidGuestOSHypervisorMapping(mapping)) {
150+
return;
151+
}
152+
153+
long oldGuestOsId = getGuestOsIdFromHypervisorMapping(mapping);
154+
if (oldGuestOsId == 0) {
155+
LOG.debug("Unable to update guest OS in hypervisor mapping, as there is no guest os hypervisor mapping - " + mapping.toString());
156+
return;
157+
}
158+
159+
long newGuestOsId = getGuestOsId(categoryId, displayName);
160+
if (newGuestOsId == 0) {
161+
LOG.debug("Unable to update guest OS id in hypervisor mapping, as there is no guest OS with category id: " + categoryId + " and display name: " + displayName);
162+
return;
163+
}
164+
165+
updateGuestOsIdInMapping(conn, oldGuestOsId, newGuestOsId, mapping);
166+
}
167+
168+
private void updateGuestOsIdInMapping(Connection conn, long oldGuestOsId, long newGuestOsId, GuestOSHypervisorMapping mapping) {
169+
LOG.debug("Updating guest os id: " + oldGuestOsId + " to id: " + newGuestOsId + " in hypervisor mapping - " + mapping.toString());
170+
try {
171+
PreparedStatement pstmt = conn.prepareStatement(updateGuestOsHypervisorSql);
172+
pstmt.setLong(1, newGuestOsId);
173+
pstmt.setLong(2, oldGuestOsId);
174+
pstmt.setString(3, mapping.getHypervisorType());
175+
pstmt.setString(4, mapping.getHypervisorVersion());
176+
pstmt.setString(5, mapping.getGuestOsName());
177+
pstmt.executeUpdate();
178+
} catch (SQLException e) {
179+
LOG.error("Failed to update guest OS id in hypervisor mapping due to: " + e.getMessage(), e);
180+
}
181+
}
182+
183+
private boolean isValidGuestOSHypervisorMapping(GuestOSHypervisorMapping mapping) {
184+
if (mapping != null && mapping.isValid()) {
185+
return true;
186+
}
187+
188+
LOG.warn("Invalid Guest OS hypervisor mapping");
189+
return false;
190+
}
191+
}

0 commit comments

Comments
 (0)