Skip to content

Commit 6b0c34f

Browse files
committed
CLOUDSTACK-6231: network acl item cidrs loaded from a seperate table
Conflicts: setup/db/db/schema-430to440.sql
1 parent 4efe933 commit 6b0c34f

9 files changed

Lines changed: 328 additions & 3 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.cloud.network.vpc;
20+
21+
import java.util.List;
22+
23+
import com.cloud.utils.db.DB;
24+
import com.cloud.utils.db.GenericDao;
25+
26+
/**
27+
* @author daan
28+
*
29+
*/
30+
public interface NetworkACLItemCidrsDao extends GenericDao<NetworkACLItemCidrsVO, Long> {
31+
32+
void persist(long networkACLItemId, List<String> cidrs);
33+
34+
List<String> getCidrs(long networkACLItemId);
35+
36+
@DB
37+
List<NetworkACLItemCidrsVO> listByNetworkACLItemId(long networkACLItemId);
38+
39+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.cloud.network.vpc;
20+
21+
import javax.persistence.Column;
22+
import javax.persistence.Entity;
23+
import javax.persistence.GeneratedValue;
24+
import javax.persistence.GenerationType;
25+
import javax.persistence.Id;
26+
import javax.persistence.Table;
27+
28+
import org.apache.cloudstack.api.InternalIdentity;
29+
30+
@Entity
31+
@Table(name = "network_acl_item_cidrs")
32+
public class NetworkACLItemCidrsVO implements InternalIdentity {
33+
private static final long serialVersionUID = 7805284475485494754L;
34+
35+
@Id
36+
@GeneratedValue(strategy = GenerationType.IDENTITY)
37+
@Column(name = "id")
38+
private Long id;
39+
40+
@Column(name = "network_acl_item_id")
41+
private long networkACLItemId;
42+
43+
@Column(name = "cidr")
44+
private String cidrList;
45+
46+
public NetworkACLItemCidrsVO() {
47+
}
48+
49+
public NetworkACLItemCidrsVO(long networkAclItemId, String cidrList) {
50+
this.networkACLItemId = networkAclItemId;
51+
this.cidrList = cidrList;
52+
}
53+
54+
/* (non-Javadoc)
55+
* @see org.apache.cloudstack.api.InternalIdentity#getId()
56+
*/
57+
@Override
58+
public long getId() {
59+
return id;
60+
}
61+
62+
public long getNetworkACLItemId() {
63+
return networkACLItemId;
64+
}
65+
66+
public String getCidr() {
67+
return cidrList;
68+
}
69+
70+
public String getCidrList() {
71+
return cidrList;
72+
}
73+
74+
public void setCidrList(String cidrList) {
75+
this.cidrList = cidrList;
76+
}
77+
78+
}

engine/schema/src/com/cloud/network/vpc/NetworkACLItemDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ public interface NetworkACLItemDao extends GenericDao<NetworkACLItemVO, Long> {
3434
int getMaxNumberByACL(long aclId);
3535

3636
NetworkACLItemVO findByAclAndNumber(long aclId, int number);
37+
38+
void loadCidrs(NetworkACLItemVO item);
3739
}

engine/schema/src/com/cloud/network/vpc/NetworkACLItemVO.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
@Table(name = "network_acl_item")
3838
public class NetworkACLItemVO implements NetworkACLItem {
3939

40+
/**
41+
*
42+
*/
43+
private static final long serialVersionUID = 2790623532888742060L;
44+
4045
@Id
4146
@GeneratedValue(strategy = GenerationType.IDENTITY)
4247
@Column(name = "id")
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package com.cloud.network.vpc.dao;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import javax.ejb.Local;
25+
26+
import org.apache.log4j.Logger;
27+
import org.springframework.stereotype.Component;
28+
29+
import com.cloud.network.vpc.NetworkACLItemCidrsDao;
30+
import com.cloud.network.vpc.NetworkACLItemCidrsVO;
31+
import com.cloud.utils.db.GenericDaoBase;
32+
import com.cloud.utils.db.SearchBuilder;
33+
import com.cloud.utils.db.SearchCriteria;
34+
import com.cloud.utils.db.TransactionLegacy;
35+
36+
/**
37+
* @author daan
38+
*
39+
*/
40+
@Component
41+
@Local(value = NetworkACLItemCidrsDao.class)
42+
public class NetworkACLItemCidrsDaoImpl extends GenericDaoBase<NetworkACLItemCidrsVO, Long> implements NetworkACLItemCidrsDao {
43+
private static final Logger s_logger = Logger.getLogger(NetworkACLItemCidrsDaoImpl.class);
44+
protected final SearchBuilder<NetworkACLItemCidrsVO> cidrsSearch;
45+
46+
protected NetworkACLItemCidrsDaoImpl() {
47+
cidrsSearch = createSearchBuilder();
48+
cidrsSearch.and("networkAclItemId", cidrsSearch.entity().getNetworkACLItemId(), SearchCriteria.Op.EQ);
49+
cidrsSearch.done();
50+
}
51+
52+
/* (non-Javadoc)
53+
* @see com.cloud.network.dao.NetworkAclItemCidrsDao#persist(long, java.util.List)
54+
*/
55+
@Override
56+
public void persist(long networkACLItemId, List<String> cidrs) {
57+
TransactionLegacy txn = TransactionLegacy.currentTxn();
58+
59+
txn.start();
60+
for (String cidr : cidrs) {
61+
NetworkACLItemCidrsVO vo = new NetworkACLItemCidrsVO(networkACLItemId, cidr);
62+
persist(vo);
63+
}
64+
txn.commit();
65+
}
66+
67+
/* (non-Javadoc)
68+
* @see com.cloud.network.dao.NetworkAclItemCidrsDao#getCidrs(long)
69+
*/
70+
@Override
71+
public List<String> getCidrs(long networkACLItemId) {
72+
SearchCriteria<NetworkACLItemCidrsVO> sc = cidrsSearch.create();
73+
sc.setParameters("firewallRuleId", networkACLItemId);
74+
75+
List<NetworkACLItemCidrsVO> results = search(sc, null);
76+
List<String> cidrs = new ArrayList<String>(results.size());
77+
for (NetworkACLItemCidrsVO result : results) {
78+
cidrs.add(result.getCidr());
79+
}
80+
81+
return cidrs;
82+
}
83+
84+
@Override
85+
public List<NetworkACLItemCidrsVO> listByNetworkACLItemId(long networkACLItemId) {
86+
SearchCriteria<NetworkACLItemCidrsVO> sc = cidrsSearch.create();
87+
sc.setParameters("firewallRuleId", networkACLItemId);
88+
89+
List<NetworkACLItemCidrsVO> results = search(sc, null);
90+
91+
return results;
92+
}
93+
94+
}

engine/schema/src/com/cloud/network/vpc/dao/NetworkACLItemDaoImpl.java

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import java.util.List;
2020

2121
import javax.ejb.Local;
22+
import javax.inject.Inject;
2223

24+
import org.apache.log4j.Logger;
2325
import org.springframework.stereotype.Component;
2426

2527
import com.cloud.network.vpc.NetworkACLItem.State;
28+
import com.cloud.network.vpc.NetworkACLItemCidrsDao;
2629
import com.cloud.network.vpc.NetworkACLItemDao;
2730
import com.cloud.network.vpc.NetworkACLItemVO;
2831
import com.cloud.utils.db.DB;
@@ -31,17 +34,22 @@
3134
import com.cloud.utils.db.SearchBuilder;
3235
import com.cloud.utils.db.SearchCriteria;
3336
import com.cloud.utils.db.SearchCriteria.Op;
37+
import com.cloud.utils.db.TransactionLegacy;
3438

3539
@Component
3640
@Local(value = NetworkACLItemDao.class)
3741
@DB()
3842
public class NetworkACLItemDaoImpl extends GenericDaoBase<NetworkACLItemVO, Long> implements NetworkACLItemDao {
43+
private static final Logger s_logger = Logger.getLogger(NetworkACLItemDaoImpl.class);
3944

4045
protected final SearchBuilder<NetworkACLItemVO> AllFieldsSearch;
4146
protected final SearchBuilder<NetworkACLItemVO> NotRevokedSearch;
4247
protected final SearchBuilder<NetworkACLItemVO> ReleaseSearch;
4348
protected final GenericSearchBuilder<NetworkACLItemVO, Integer> MaxNumberSearch;
4449

50+
@Inject
51+
protected NetworkACLItemCidrsDao _networkACLItemCidrsDao;
52+
4553
protected NetworkACLItemDaoImpl() {
4654
super();
4755

@@ -75,6 +83,13 @@ protected NetworkACLItemDaoImpl() {
7583
MaxNumberSearch.done();
7684
}
7785

86+
@Override
87+
public NetworkACLItemVO findById(Long id) {
88+
NetworkACLItemVO item = super.findById(id);
89+
loadCidrs(item);
90+
return item;
91+
}
92+
7893
@Override
7994
public boolean setStateToAdd(NetworkACLItemVO rule) {
8095
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
@@ -96,7 +111,10 @@ public boolean revoke(NetworkACLItemVO rule) {
96111
public List<NetworkACLItemVO> listByACL(long aclId) {
97112
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
98113
sc.setParameters("aclId", aclId);
99-
114+
List<NetworkACLItemVO> list = listBy(sc);
115+
for(NetworkACLItemVO item :list) {
116+
loadCidrs(item);
117+
}
100118
return listBy(sc);
101119
}
102120

@@ -113,6 +131,35 @@ public NetworkACLItemVO findByAclAndNumber(long aclId, int number) {
113131
SearchCriteria<NetworkACLItemVO> sc = AllFieldsSearch.create();
114132
sc.setParameters("aclId", aclId);
115133
sc.setParameters("number", number);
116-
return findOneBy(sc);
134+
NetworkACLItemVO vo = findOneBy(sc);
135+
loadCidrs(vo);
136+
return vo;
137+
}
138+
139+
@Override
140+
@DB
141+
public NetworkACLItemVO persist(NetworkACLItemVO networkAclItem) {
142+
TransactionLegacy txn = TransactionLegacy.currentTxn();
143+
txn.start();
144+
145+
NetworkACLItemVO dbNetworkACLItem = super.persist(networkAclItem);
146+
saveCidrs(networkAclItem, networkAclItem.getSourceCidrList());
147+
loadCidrs(dbNetworkACLItem);
148+
149+
txn.commit();
150+
return dbNetworkACLItem;
151+
}
152+
153+
public void saveCidrs(NetworkACLItemVO networkACLItem, List<String> cidrList) {
154+
if (cidrList == null) {
155+
return;
156+
}
157+
_networkACLItemCidrsDao.persist(networkACLItem.getId(), cidrList);
158+
}
159+
160+
@Override
161+
public void loadCidrs(NetworkACLItemVO item) {
162+
List<String> cidrs = _networkACLItemCidrsDao.getCidrs(item.getId());
163+
item.setSourceCidrList(cidrs);
117164
}
118165
}

engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public File[] getPrepareScripts() {
6161
public void performDataMigration(Connection conn) {
6262
populateIAMGroupAccountMap(conn);
6363
secondaryIpsAccountAndDomainIdsUpdate(conn);
64+
moveCidrsToTheirOwnTable(conn);
6465
}
6566

6667
// populate iam_group_account_map table for existing accounts
@@ -244,7 +245,58 @@ private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) {
244245
}
245246

246247

248+
private void moveCidrsToTheirOwnTable(Connection conn) {
249+
PreparedStatement pstmtItem = null;
250+
PreparedStatement pstmtCidr = null;
251+
ResultSet rsItems = null;
247252

253+
String networkAclItemSql = "SELECT id, cidr FROM `cloud`.`network_acl_item`";
254+
255+
s_logger.debug("Moving network acl item cidrs to a row per cidr");
256+
try {
257+
pstmtItem = conn.prepareStatement(networkAclItemSql);
258+
rsItems = pstmtItem.executeQuery();
259+
260+
// for each network acl item
261+
while(rsItems.next()) {
262+
long itemId = rsItems.getLong(1);
263+
// get the source cidr list
264+
String cidrList = rsItems.getString(2);
265+
s_logger.debug("Moving '" + cidrList + "' to a row per cidr");
266+
// split it
267+
String[] cidrArray = cidrList.split(",");
268+
// insert a record per cidr
269+
String networkAclItemCidrSql = "INSERT INTO `cloud`.`network_acl_item_cidr` (network_acl_item_id, cidr) VALUES (?,?)";
270+
for(String cidr: cidrArray)
271+
{
272+
pstmtCidr = conn.prepareStatement(networkAclItemCidrSql);
273+
pstmtCidr.setLong(1,itemId);
274+
pstmtCidr.setString(2,cidr);
275+
pstmtCidr.executeUpdate();
276+
}
277+
pstmtCidr.close();
278+
}
279+
} catch (SQLException e) {
280+
throw new CloudRuntimeException("Exception while Moving network acl item cidrs to a row per cidr", e);
281+
} finally {
282+
283+
if (pstmtItem != null) {
284+
try {
285+
pstmtItem.close();
286+
287+
} catch (SQLException e) {
288+
}
289+
}
290+
if (pstmtCidr != null) {
291+
try {
292+
pstmtCidr.close();
293+
294+
} catch (SQLException e) {
295+
}
296+
}
297+
}
298+
s_logger.debug("Done moving network acl item cidrs to a row per cidr");
299+
}
248300

249301

250302
@Override

0 commit comments

Comments
 (0)