Skip to content

Commit cde887d

Browse files
Sina Kashipazhaweizhouapache
authored andcommitted
Refactored the code, regarding the code change suggetions by GutoVeronezi.
1 parent 4ebef65 commit cde887d

File tree

17 files changed

+234
-322
lines changed

17 files changed

+234
-322
lines changed

api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.cloud.utils.Pair;
3333

3434
import org.apache.cloudstack.network.lb.LoadBalancerConfig;
35+
import org.springframework.util.CollectionUtils;
3536

3637
public class LoadBalancerTO {
3738
String uuid;
@@ -185,7 +186,7 @@ public LoadBalancerConfigTO[] getLbConfigs() {
185186
}
186187

187188
public void setLbConfigs(List<? extends LoadBalancerConfig> lbConfigs) {
188-
if (lbConfigs == null || lbConfigs.size() == 0) {
189+
if (CollectionUtils.isEmpty(lbConfigs)) {
189190
this.lbConfigs = new LoadBalancerConfigTO[0];
190191
return;
191192
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/AssignCertToLoadBalancerCmd.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// under the License.
1818
package org.apache.cloudstack.api.command.user.loadbalancer;
1919

20+
import org.apache.commons.lang3.BooleanUtils;
2021
import org.apache.log4j.Logger;
2122

2223
import org.apache.cloudstack.api.APICommand;
@@ -106,7 +107,7 @@ public Long getLbRuleId() {
106107
}
107108

108109
public boolean isForced() {
109-
return (forced != null) ? forced : false;
110+
return BooleanUtils.toBoolean(forced);
110111
}
111112

112113
@Override
@@ -127,9 +128,6 @@ public String getSyncObjType() {
127128
@Override
128129
public Long getSyncObjId() {
129130
LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getLbRuleId());
130-
if (lb == null) {
131-
return null;
132-
}
133-
return lb.getNetworkId();
131+
return (lb != null )? lb.getNetworkId(): null;
134132
}
135133
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerConfigCmd.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.cloudstack.api.APICommand;
2121
import org.apache.cloudstack.api.ApiConstants;
2222
import org.apache.cloudstack.api.ApiErrorCode;
23-
import org.apache.cloudstack.api.BaseAsyncCmd;
2423
import org.apache.cloudstack.api.BaseAsyncCreateCmd;
2524
import org.apache.cloudstack.api.Parameter;
2625
import org.apache.cloudstack.api.ServerApiException;
@@ -30,6 +29,7 @@
3029
import org.apache.cloudstack.api.response.VpcResponse;
3130
import org.apache.cloudstack.network.lb.LoadBalancerConfig;
3231
import org.apache.cloudstack.network.lb.LoadBalancerConfig.Scope;
32+
import org.apache.commons.lang3.BooleanUtils;
3333
import org.apache.log4j.Logger;
3434

3535
import com.cloud.event.EventTypes;
@@ -123,7 +123,7 @@ public String getValue() {
123123
}
124124

125125
public boolean isForced() {
126-
return (forced != null) ? forced : false;
126+
return BooleanUtils.toBoolean(forced);
127127
}
128128

129129
/////////////////////////////////////////////////////
@@ -138,9 +138,8 @@ public String getCommandName() {
138138
@Override
139139
public void execute() {
140140

141-
LoadBalancerConfig config = null;
142141
try {
143-
config = _entityMgr.findById(LoadBalancerConfig.class, getEntityId());
142+
LoadBalancerConfig config = _entityMgr.findById(LoadBalancerConfig.class, getEntityId());
144143
LoadBalancerConfigResponse lbConfigResponse = new LoadBalancerConfigResponse();
145144
if (config != null) {
146145
lbConfigResponse = _responseGenerator.createLoadBalancerConfigResponse(config);
@@ -165,41 +164,22 @@ public void create() {
165164

166165
@Override
167166
public String getSyncObjType() {
168-
if (networkId != null) {
169-
return BaseAsyncCmd.networkSyncObject;
170-
} else if (vpcId != null) {
171-
return BaseAsyncCmd.vpcSyncObject;
172-
}
173-
return null;
167+
return LoadBalancerHelper.getSyncObjType(networkId, vpcId);
174168
}
175169

176170
@Override
177171
public Long getSyncObjId() {
178-
if (networkId != null) {
179-
return getNetworkId();
180-
} else if (vpcId != null) {
181-
return getVpcId();
182-
}
183-
return null;
172+
return LoadBalancerHelper.getSyncObjId(networkId, vpcId);
184173
}
185174

186175
@Override
187176
public long getEntityOwnerId() {
188177
if (Scope.Network.name().equalsIgnoreCase(scope) && networkId != null) {
189-
Network network = _entityMgr.findById(Network.class, networkId);
190-
if (network != null) {
191-
return network.getAccountId();
192-
}
178+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, Network.class, networkId);
193179
} else if (Scope.Vpc.name().equalsIgnoreCase(scope) && vpcId != null) {
194-
Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
195-
if (vpc != null) {
196-
return vpc.getAccountId();
197-
}
180+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, Vpc.class, vpcId);
198181
} else if (Scope.LoadBalancerRule.name().equalsIgnoreCase(scope) && loadBalancerId != null) {
199-
LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, loadBalancerId);
200-
if (lb != null) {
201-
return lb.getAccountId();
202-
}
182+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, LoadBalancer.class, loadBalancerId);
203183
}
204184
throw new InvalidParameterValueException("Unable to find the entity owner");
205185
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.apache.cloudstack.api.response.NetworkResponse;
3434
import org.apache.cloudstack.api.response.ZoneResponse;
3535
import org.apache.cloudstack.context.CallContext;
36+
import org.apache.commons.lang3.StringUtils;
3637
import org.apache.log4j.Logger;
3738

3839
import com.cloud.dc.DataCenter;
@@ -255,7 +256,7 @@ public List<String> getSourceCidrList() {
255256
}
256257

257258
public String getLbProtocol() {
258-
return lbProtocol != null ? lbProtocol.toLowerCase().trim() : null;
259+
return StringUtils.trim(StringUtils.lowerCase(lbProtocol));
259260
}
260261

261262
/////////////////////////////////////////////////////

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/DeleteLoadBalancerConfigCmd.java

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -84,57 +84,19 @@ public void execute() {
8484

8585
@Override
8686
public String getSyncObjType() {
87-
LoadBalancerConfig config = _entityMgr.findById(LoadBalancerConfig.class, getId());
88-
if (config == null) {
89-
throw new InvalidParameterValueException("Unable to find load balancer config: " + id);
90-
}
91-
if (config.getNetworkId() != null) {
92-
return BaseAsyncCmd.networkSyncObject;
93-
} else if (config.getVpcId() != null) {
94-
return BaseAsyncCmd.vpcSyncObject;
95-
}
96-
return null;
87+
return LoadBalancerHelper.getSyncObjType(_entityMgr, getId());
9788
}
9889

9990
@Override
10091
public Long getSyncObjId() {
101-
LoadBalancerConfig config = _entityMgr.findById(LoadBalancerConfig.class, getId());
102-
if (config == null) {
103-
throw new InvalidParameterValueException("Unable to find load balancer config: " + id);
104-
}
105-
if (config.getNetworkId() != null) {
106-
return config.getNetworkId();
107-
} else if (config.getVpcId() != null) {
108-
return config.getVpcId();
109-
}
110-
return null;
92+
return LoadBalancerHelper.getSyncObjId(_entityMgr, getId());
11193
}
11294

11395
@Override
11496
public long getEntityOwnerId() {
115-
LoadBalancerConfig config = _entityMgr.findById(LoadBalancerConfig.class, getId());
116-
if (config != null) {
117-
if (config.getNetworkId() != null) {
118-
Network network = _entityMgr.findById(Network.class, config.getNetworkId());
119-
if (network != null) {
120-
return network.getAccountId();
121-
}
122-
} else if (config.getVpcId() != null) {
123-
Vpc vpc = _entityMgr.findById(Vpc.class, config.getVpcId());
124-
if (vpc != null) {
125-
return vpc.getAccountId();
126-
}
127-
} else if (config.getLoadBalancerId() != null) {
128-
FirewallRule rule = _entityMgr.findById(FirewallRule.class, config.getLoadBalancerId());
129-
if (rule != null) {
130-
return rule.getAccountId();
131-
}
132-
}
133-
}
134-
throw new InvalidParameterValueException("Unable to find the entity owner");
97+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, getId());
13598
}
13699

137-
138100
@Override
139101
public String getEventType() {
140102
return EventTypes.EVENT_LOAD_BALANCER_CONFIG_DELETE;

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/ListLoadBalancerConfigsCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.cloudstack.api.response.NetworkResponse;
3131
import org.apache.cloudstack.api.response.VpcResponse;
3232
import org.apache.cloudstack.network.lb.LoadBalancerConfig;
33+
import org.apache.commons.lang3.BooleanUtils;
3334
import org.apache.log4j.Logger;
3435

3536
@APICommand(name = "listLoadBalancerConfigs", description = "Lists load balancer configs.",
@@ -115,7 +116,7 @@ public String getName() {
115116
}
116117

117118
public boolean listAll() {
118-
return listAll == null ? false : listAll;
119+
return BooleanUtils.toBoolean(listAll);
119120
}
120121

121122
// ///////////////////////////////////////////////////
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.apache.cloudstack.api.command.user.loadbalancer;
2+
3+
import com.cloud.exception.InvalidParameterValueException;
4+
import com.cloud.network.Network;
5+
import com.cloud.network.rules.FirewallRule;
6+
import com.cloud.network.vpc.Vpc;
7+
import com.cloud.user.OwnedBy;
8+
import com.cloud.utils.db.EntityManager;
9+
import org.apache.cloudstack.api.BaseAsyncCmd;
10+
import org.apache.cloudstack.network.lb.LoadBalancerConfig;
11+
12+
13+
public interface LoadBalancerHelper {
14+
15+
static LoadBalancerConfig findConfigById(EntityManager entityMgr, Long id){
16+
LoadBalancerConfig config = entityMgr.findById(LoadBalancerConfig.class, id);
17+
if (config == null) {
18+
throw new InvalidParameterValueException("Unable to find load balancer config: " + id);
19+
}
20+
return config;
21+
}
22+
23+
static String getSyncObjType(EntityManager entityMgr, Long id){
24+
LoadBalancerConfig config = findConfigById(entityMgr, id);
25+
return getSyncObjType(config.getNetworkId(), config.getVpcId());
26+
}
27+
28+
static String getSyncObjType(Long networkId, Long vpcId){
29+
if (networkId != null) {
30+
return BaseAsyncCmd.networkSyncObject;
31+
} else if (vpcId != null) {
32+
return BaseAsyncCmd.vpcSyncObject;
33+
}
34+
return null;
35+
}
36+
37+
static Long getSyncObjId(EntityManager entityMgr, Long id){
38+
LoadBalancerConfig config = findConfigById(entityMgr, id);
39+
return getSyncObjId(config.getNetworkId(), config.getVpcId());
40+
}
41+
42+
// Cause vpcId might be null, this method might return null
43+
static Long getSyncObjId(Long networkId, Long vpcId){
44+
if (networkId != null)
45+
return networkId;
46+
return vpcId;
47+
}
48+
49+
static <T extends OwnedBy> long getEntityOwnerId(EntityManager entityMgr , Class<T> entityType, Long id){
50+
T t = entityMgr.findById(entityType, id);
51+
if (t != null) {
52+
return t.getAccountId();
53+
}
54+
throw new InvalidParameterValueException("Unable to find the entity owner");
55+
}
56+
57+
static long getEntityOwnerId(EntityManager entityMgr, Long id) {
58+
LoadBalancerConfig config = findConfigById(entityMgr, id);
59+
if (config.getNetworkId() != null) {
60+
return LoadBalancerHelper.getEntityOwnerId(entityMgr, Network.class, config.getNetworkId());
61+
} else if (config.getVpcId() != null) {
62+
return LoadBalancerHelper.getEntityOwnerId(entityMgr, Vpc.class, config.getVpcId());
63+
} else if (config.getLoadBalancerId() != null) {
64+
return LoadBalancerHelper.getEntityOwnerId(entityMgr, FirewallRule.class, config.getLoadBalancerId());
65+
}
66+
throw new InvalidParameterValueException("Unable to find the entity owner");
67+
}
68+
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/RemoveCertFromLoadBalancerCmd.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ public String getSyncObjType() {
104104
@Override
105105
public Long getSyncObjId() {
106106
LoadBalancer lb = _entityMgr.findById(LoadBalancer.class, getLbRuleId());
107-
if (lb == null) {
108-
return null;
109-
}
110-
return lb.getNetworkId();
107+
return (lb != null )? lb.getNetworkId(): null;
111108
}
112109
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/ReplaceLoadBalancerConfigsCmd.java

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
import org.apache.cloudstack.api.response.SuccessResponse;
3131
import org.apache.cloudstack.api.response.VpcResponse;
3232
import org.apache.cloudstack.network.lb.LoadBalancerConfig;
33+
import org.apache.commons.collections.MapUtils;
3334
import org.apache.log4j.Logger;
3435

3536
import com.cloud.event.EventTypes;
36-
import com.cloud.exception.InvalidParameterValueException;
3737
import com.cloud.network.Network;
3838
import com.cloud.network.vpc.Vpc;
3939

@@ -101,7 +101,7 @@ public Long getLoadBalancerId() {
101101
}
102102

103103
public Map<String, String> getConfigList() {
104-
if (configList == null || configList.isEmpty()) {
104+
if (MapUtils.isEmpty(configList)) {
105105
return null;
106106
}
107107

@@ -127,38 +127,20 @@ public void execute() {
127127

128128
@Override
129129
public String getSyncObjType() {
130-
if (networkId != null) {
131-
return BaseAsyncCmd.networkSyncObject;
132-
} else if (vpcId != null) {
133-
return BaseAsyncCmd.vpcSyncObject;
134-
}
135-
return null;
130+
return LoadBalancerHelper.getSyncObjType(networkId, vpcId);
136131
}
137132

138133
@Override
139134
public Long getSyncObjId() {
140-
if (networkId != null) {
141-
return networkId;
142-
} else if (vpcId != null) {
143-
return vpcId;
144-
}
145-
return null;
135+
return LoadBalancerHelper.getSyncObjId(networkId, vpcId);
146136
}
147137

148138
@Override
149139
public long getEntityOwnerId() {
150140
if (networkId != null) {
151-
Network network = _entityMgr.findById(Network.class, networkId);
152-
if (network != null) {
153-
return network.getAccountId();
154-
}
155-
} else if (vpcId != null) {
156-
Vpc vpc = _entityMgr.findById(Vpc.class, vpcId);
157-
if (vpc != null) {
158-
return vpc.getAccountId();
159-
}
141+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, Network.class, networkId);
160142
}
161-
throw new InvalidParameterValueException("Unable to find the entity owner");
143+
return LoadBalancerHelper.getEntityOwnerId(_entityMgr, Vpc.class, vpcId);
162144
}
163145

164146
@Override

0 commit comments

Comments
 (0)