From 6b0c34faeecad0e1c19181835130e7b91d2783ee Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Wed, 19 Feb 2014 15:39:12 +0100 Subject: [PATCH] CLOUDSTACK-6231: network acl item cidrs loaded from a seperate table Conflicts: setup/db/db/schema-430to440.sql --- .../network/vpc/NetworkACLItemCidrsDao.java | 39 ++++++++ .../network/vpc/NetworkACLItemCidrsVO.java | 78 +++++++++++++++ .../cloud/network/vpc/NetworkACLItemDao.java | 2 + .../cloud/network/vpc/NetworkACLItemVO.java | 5 + .../vpc/dao/NetworkACLItemCidrsDaoImpl.java | 94 +++++++++++++++++++ .../vpc/dao/NetworkACLItemDaoImpl.java | 51 +++++++++- .../cloud/upgrade/dao/Upgrade430to440.java | 52 ++++++++++ setup/db/db/schema-430to440-cleanup.sql | 2 +- setup/db/db/schema-430to440.sql | 8 ++ 9 files changed, 328 insertions(+), 3 deletions(-) create mode 100644 engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsDao.java create mode 100644 engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsVO.java create mode 100644 engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemCidrsDaoImpl.java diff --git a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsDao.java b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsDao.java new file mode 100644 index 00000000000..5e49368de7e --- /dev/null +++ b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsDao.java @@ -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 { + + void persist(long networkACLItemId, List cidrs); + + List getCidrs(long networkACLItemId); + + @DB + List listByNetworkACLItemId(long networkACLItemId); + +} diff --git a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsVO.java b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsVO.java new file mode 100644 index 00000000000..c366f947961 --- /dev/null +++ b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemCidrsVO.java @@ -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; + } + +} diff --git a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemDao.java b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemDao.java index e27848522ff..9ab6365df18 100644 --- a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemDao.java +++ b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemDao.java @@ -34,4 +34,6 @@ public interface NetworkACLItemDao extends GenericDao { int getMaxNumberByACL(long aclId); NetworkACLItemVO findByAclAndNumber(long aclId, int number); + + void loadCidrs(NetworkACLItemVO item); } diff --git a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemVO.java b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemVO.java index 8031b001b8a..0d2897dbf9b 100644 --- a/engine/schema/src/com/cloud/network/vpc/NetworkACLItemVO.java +++ b/engine/schema/src/com/cloud/network/vpc/NetworkACLItemVO.java @@ -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") diff --git a/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemCidrsDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemCidrsDaoImpl.java new file mode 100644 index 00000000000..23c1fa782e7 --- /dev/null +++ b/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemCidrsDaoImpl.java @@ -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 implements NetworkACLItemCidrsDao { + private static final Logger s_logger = Logger.getLogger(NetworkACLItemCidrsDaoImpl.class); + protected final SearchBuilder 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 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 getCidrs(long networkACLItemId) { + SearchCriteria sc = cidrsSearch.create(); + sc.setParameters("firewallRuleId", networkACLItemId); + + List results = search(sc, null); + List cidrs = new ArrayList(results.size()); + for (NetworkACLItemCidrsVO result : results) { + cidrs.add(result.getCidr()); + } + + return cidrs; + } + + @Override + public List listByNetworkACLItemId(long networkACLItemId) { + SearchCriteria sc = cidrsSearch.create(); + sc.setParameters("firewallRuleId", networkACLItemId); + + List results = search(sc, null); + + return results; + } + +} diff --git a/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemDaoImpl.java b/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemDaoImpl.java index 31ff6f259ac..c9402473476 100644 --- a/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemDaoImpl.java +++ b/engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemDaoImpl.java @@ -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 implements NetworkACLItemDao { + private static final Logger s_logger = Logger.getLogger(NetworkACLItemDaoImpl.class); protected final SearchBuilder AllFieldsSearch; protected final SearchBuilder NotRevokedSearch; protected final SearchBuilder ReleaseSearch; protected final GenericSearchBuilder MaxNumberSearch; + @Inject + protected NetworkACLItemCidrsDao _networkACLItemCidrsDao; + protected NetworkACLItemDaoImpl() { super(); @@ -75,6 +83,13 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase sc = AllFieldsSearch.create(); @@ -96,7 +111,10 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase listByACL(long aclId) { SearchCriteria sc = AllFieldsSearch.create(); sc.setParameters("aclId", aclId); - + List list = listBy(sc); + for(NetworkACLItemVO item :list) { + loadCidrs(item); + } return listBy(sc); } @@ -113,6 +131,35 @@ public class NetworkACLItemDaoImpl extends GenericDaoBase 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 cidrList) { + if (cidrList == null) { + return; + } + _networkACLItemCidrsDao.persist(networkACLItem.getId(), cidrList); + } + + @Override + public void loadCidrs(NetworkACLItemVO item) { + List cidrs = _networkACLItemCidrsDao.getCidrs(item.getId()); + item.setSourceCidrList(cidrs); } } diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java index 922a892be04..7406c1ec03a 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java @@ -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 diff --git a/setup/db/db/schema-430to440-cleanup.sql b/setup/db/db/schema-430to440-cleanup.sql index 30df9782f61..8b1eec406c1 100644 --- a/setup/db/db/schema-430to440-cleanup.sql +++ b/setup/db/db/schema-430to440-cleanup.sql @@ -19,4 +19,4 @@ -- Schema cleanup from 4.3.0 to 4.4.0; --; - +ALTER TABLE `cloud`.`network_acl_item` DROP COLUMN `cidr`; diff --git a/setup/db/db/schema-430to440.sql b/setup/db/db/schema-430to440.sql index bea311a2042..711919e6b46 100644 --- a/setup/db/db/schema-430to440.sql +++ b/setup/db/db/schema-430to440.sql @@ -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;