CLOUDSTACK-6231: network acl item cidrs loaded from a seperate table

Conflicts:
	setup/db/db/schema-430to440.sql
This commit is contained in:
Daan Hoogland 2014-02-19 15:39:12 +01:00
parent 4efe933818
commit 6b0c34faee
9 changed files with 328 additions and 3 deletions

View File

@ -0,0 +1,39 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.cloud.network.vpc;
import java.util.List;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDao;
/**
* @author daan
*
*/
public interface NetworkACLItemCidrsDao extends GenericDao<NetworkACLItemCidrsVO, Long> {
void persist(long networkACLItemId, List<String> cidrs);
List<String> getCidrs(long networkACLItemId);
@DB
List<NetworkACLItemCidrsVO> listByNetworkACLItemId(long networkACLItemId);
}

View File

@ -0,0 +1,78 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.cloud.network.vpc;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.apache.cloudstack.api.InternalIdentity;
@Entity
@Table(name = "network_acl_item_cidrs")
public class NetworkACLItemCidrsVO implements InternalIdentity {
private static final long serialVersionUID = 7805284475485494754L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "network_acl_item_id")
private long networkACLItemId;
@Column(name = "cidr")
private String cidrList;
public NetworkACLItemCidrsVO() {
}
public NetworkACLItemCidrsVO(long networkAclItemId, String cidrList) {
this.networkACLItemId = networkAclItemId;
this.cidrList = cidrList;
}
/* (non-Javadoc)
* @see org.apache.cloudstack.api.InternalIdentity#getId()
*/
@Override
public long getId() {
return id;
}
public long getNetworkACLItemId() {
return networkACLItemId;
}
public String getCidr() {
return cidrList;
}
public String getCidrList() {
return cidrList;
}
public void setCidrList(String cidrList) {
this.cidrList = cidrList;
}
}

View File

@ -34,4 +34,6 @@ public interface NetworkACLItemDao extends GenericDao<NetworkACLItemVO, Long> {
int getMaxNumberByACL(long aclId);
NetworkACLItemVO findByAclAndNumber(long aclId, int number);
void loadCidrs(NetworkACLItemVO item);
}

View File

@ -37,6 +37,11 @@ import com.cloud.utils.net.NetUtils;
@Table(name = "network_acl_item")
public class NetworkACLItemVO implements NetworkACLItem {
/**
*
*/
private static final long serialVersionUID = 2790623532888742060L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")

View File

@ -0,0 +1,94 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.cloud.network.vpc.dao;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.network.vpc.NetworkACLItemCidrsDao;
import com.cloud.network.vpc.NetworkACLItemCidrsVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.TransactionLegacy;
/**
* @author daan
*
*/
@Component
@Local(value = NetworkACLItemCidrsDao.class)
public class NetworkACLItemCidrsDaoImpl extends GenericDaoBase<NetworkACLItemCidrsVO, Long> implements NetworkACLItemCidrsDao {
private static final Logger s_logger = Logger.getLogger(NetworkACLItemCidrsDaoImpl.class);
protected final SearchBuilder<NetworkACLItemCidrsVO> cidrsSearch;
protected NetworkACLItemCidrsDaoImpl() {
cidrsSearch = createSearchBuilder();
cidrsSearch.and("networkAclItemId", cidrsSearch.entity().getNetworkACLItemId(), SearchCriteria.Op.EQ);
cidrsSearch.done();
}
/* (non-Javadoc)
* @see com.cloud.network.dao.NetworkAclItemCidrsDao#persist(long, java.util.List)
*/
@Override
public void persist(long networkACLItemId, List<String> cidrs) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
for (String cidr : cidrs) {
NetworkACLItemCidrsVO vo = new NetworkACLItemCidrsVO(networkACLItemId, cidr);
persist(vo);
}
txn.commit();
}
/* (non-Javadoc)
* @see com.cloud.network.dao.NetworkAclItemCidrsDao#getCidrs(long)
*/
@Override
public List<String> getCidrs(long networkACLItemId) {
SearchCriteria<NetworkACLItemCidrsVO> sc = cidrsSearch.create();
sc.setParameters("firewallRuleId", networkACLItemId);
List<NetworkACLItemCidrsVO> results = search(sc, null);
List<String> cidrs = new ArrayList<String>(results.size());
for (NetworkACLItemCidrsVO result : results) {
cidrs.add(result.getCidr());
}
return cidrs;
}
@Override
public List<NetworkACLItemCidrsVO> listByNetworkACLItemId(long networkACLItemId) {
SearchCriteria<NetworkACLItemCidrsVO> sc = cidrsSearch.create();
sc.setParameters("firewallRuleId", networkACLItemId);
List<NetworkACLItemCidrsVO> results = search(sc, null);
return results;
}
}

View File

@ -19,10 +19,13 @@ package com.cloud.network.vpc.dao;
import java.util.List;
import javax.ejb.Local;
import javax.inject.Inject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.cloud.network.vpc.NetworkACLItem.State;
import com.cloud.network.vpc.NetworkACLItemCidrsDao;
import com.cloud.network.vpc.NetworkACLItemDao;
import com.cloud.network.vpc.NetworkACLItemVO;
import com.cloud.utils.db.DB;
@ -31,17 +34,22 @@ import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.TransactionLegacy;
@Component
@Local(value = NetworkACLItemDao.class)
@DB()
public class NetworkACLItemDaoImpl extends GenericDaoBase<NetworkACLItemVO, Long> implements NetworkACLItemDao {
private static final Logger s_logger = Logger.getLogger(NetworkACLItemDaoImpl.class);
protected final SearchBuilder<NetworkACLItemVO> AllFieldsSearch;
protected final SearchBuilder<NetworkACLItemVO> NotRevokedSearch;
protected final SearchBuilder<NetworkACLItemVO> ReleaseSearch;
protected final GenericSearchBuilder<NetworkACLItemVO, Integer> MaxNumberSearch;
@Inject
protected NetworkACLItemCidrsDao _networkACLItemCidrsDao;
protected NetworkACLItemDaoImpl() {
super();
@ -75,6 +83,13 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase<NetworkACLItemVO, Long
MaxNumberSearch.done();
}
@Override
public NetworkACLItemVO findById(Long id) {
NetworkACLItemVO item = super.findById(id);
loadCidrs(item);
return item;
}
@Override
public boolean setStateToAdd(NetworkACLItemVO rule) {
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
@ -96,7 +111,10 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase<NetworkACLItemVO, Long
public List<NetworkACLItemVO> listByACL(long aclId) {
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
sc.setParameters("aclId", aclId);
List<NetworkACLItemVO> list = listBy(sc);
for(NetworkACLItemVO item :list) {
loadCidrs(item);
}
return listBy(sc);
}
@ -113,6 +131,35 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase<NetworkACLItemVO, Long
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
sc.setParameters("aclId", aclId);
sc.setParameters("number", number);
return findOneBy(sc);
NetworkACLItemVO vo = findOneBy(sc);
loadCidrs(vo);
return vo;
}
@Override
@DB
public NetworkACLItemVO persist(NetworkACLItemVO networkAclItem) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
NetworkACLItemVO dbNetworkACLItem = super.persist(networkAclItem);
saveCidrs(networkAclItem, networkAclItem.getSourceCidrList());
loadCidrs(dbNetworkACLItem);
txn.commit();
return dbNetworkACLItem;
}
public void saveCidrs(NetworkACLItemVO networkACLItem, List<String> cidrList) {
if (cidrList == null) {
return;
}
_networkACLItemCidrsDao.persist(networkACLItem.getId(), cidrList);
}
@Override
public void loadCidrs(NetworkACLItemVO item) {
List<String> cidrs = _networkACLItemCidrsDao.getCidrs(item.getId());
item.setSourceCidrList(cidrs);
}
}

View File

@ -61,6 +61,7 @@ public class Upgrade430to440 implements DbUpgrade {
public void performDataMigration(Connection conn) {
populateIAMGroupAccountMap(conn);
secondaryIpsAccountAndDomainIdsUpdate(conn);
moveCidrsToTheirOwnTable(conn);
}
// populate iam_group_account_map table for existing accounts
@ -244,7 +245,58 @@ public class Upgrade430to440 implements DbUpgrade {
}
private void moveCidrsToTheirOwnTable(Connection conn) {
PreparedStatement pstmtItem = null;
PreparedStatement pstmtCidr = null;
ResultSet rsItems = null;
String networkAclItemSql = "SELECT id, cidr FROM `cloud`.`network_acl_item`";
s_logger.debug("Moving network acl item cidrs to a row per cidr");
try {
pstmtItem = conn.prepareStatement(networkAclItemSql);
rsItems = pstmtItem.executeQuery();
// for each network acl item
while(rsItems.next()) {
long itemId = rsItems.getLong(1);
// get the source cidr list
String cidrList = rsItems.getString(2);
s_logger.debug("Moving '" + cidrList + "' to a row per cidr");
// split it
String[] cidrArray = cidrList.split(",");
// insert a record per cidr
String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidr` (network_acl_item_id, cidr) VALUES (?,?)";
for(String cidr: cidrArray)
{
pstmtCidr = conn.prepareStatement(networkAclItemCidrSql);
pstmtCidr.setLong(1,itemId);
pstmtCidr.setString(2,cidr);
pstmtCidr.executeUpdate();
}
pstmtCidr.close();
}
} catch (SQLException e) {
throw new CloudRuntimeException("Exception while Moving network acl item cidrs to a row per cidr", e);
} finally {
if (pstmtItem != null) {
try {
pstmtItem.close();
} catch (SQLException e) {
}
}
if (pstmtCidr != null) {
try {
pstmtCidr.close();
} catch (SQLException e) {
}
}
}
s_logger.debug("Done moving network acl item cidrs to a row per cidr");
}
@Override

View File

@ -19,4 +19,4 @@
-- Schema cleanup from 4.3.0 to 4.4.0;
--;
ALTER TABLE `cloud`.`network_acl_item` DROP COLUMN `cidr`;

View File

@ -1654,3 +1654,11 @@ CREATE TABLE `cloud`.`op_vpc_distributed_router_sequence_no` (
PRIMARY KEY (`id`),
UNIQUE `u_op_vpc_distributed_router_sequence_no_vpc_id`(`vpc_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `cloud`.`network_acl_item_cidr` (
`id` bigint unsigned UNIQUE NOT NULL auto_increment,
`network_acl_item_id` bigint unsigned NOT NULL COMMENT 'Network ACL Item id',
`cidr` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `fk_network_acl_item_id` FOREIGN KEY `fk_network_acl_item_id`(`network_acl_item_id`) REFERENCES `network_acl_item`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;