|
| 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