Skip to content

Commit 7f91964

Browse files
refactoring: Remove duplicate code (by Frank/Nuage) (apache#3538)
Refactor: Cleanup duplicate code Make use of Java 8 default implementation in interfaces, to remove code duplication between XxxCmd and XxxCmdAsAdmin. Refactor checkFormat by pre-calculating the supported extensions. Also make use of this in ImageStoreUtil. Makes it easier to add new file and compression formats.
1 parent 8170ec5 commit 7f91964

File tree

117 files changed

+696
-1608
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+696
-1608
lines changed

api/src/main/java/com/cloud/template/VirtualMachineTemplate.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import com.cloud.hypervisor.Hypervisor.HypervisorType;
2727
import com.cloud.storage.Storage.ImageFormat;
2828
import com.cloud.storage.Storage.TemplateType;
29-
import com.cloud.storage.Volume.Event;
30-
import com.cloud.storage.Volume.State;
3129
import com.cloud.utils.fsm.StateMachine2;
3230
import com.cloud.utils.fsm.StateObject;
3331

api/src/main/java/org/apache/cloudstack/api/BaseListTaggedResourcesCmd.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,14 @@
1616
// under the License.
1717
package org.apache.cloudstack.api;
1818

19-
import java.util.Collection;
20-
import java.util.HashMap;
21-
import java.util.Iterator;
2219
import java.util.Map;
2320

24-
import com.cloud.exception.InvalidParameterValueException;
25-
2621
public abstract class BaseListTaggedResourcesCmd extends BaseListProjectAndAccountResourcesCmd implements IBaseListTaggedResourcesCmd {
2722
@Parameter(name = ApiConstants.TAGS, type = CommandType.MAP, description = "List resources by tags (key/value pairs)")
2823
private Map tags;
2924

3025
@Override
3126
public Map<String, String> getTags() {
32-
Map<String, String> tagsMap = null;
33-
if (tags != null && !tags.isEmpty()) {
34-
tagsMap = new HashMap<String, String>();
35-
Collection<?> servicesCollection = tags.values();
36-
Iterator<?> iter = servicesCollection.iterator();
37-
while (iter.hasNext()) {
38-
HashMap<String, String> services = (HashMap<String, String>)iter.next();
39-
String key = services.get("key");
40-
String value = services.get("value");
41-
if (value == null) {
42-
throw new InvalidParameterValueException("No value is passed in for key " + key);
43-
}
44-
tagsMap.put(key, value);
45-
}
46-
}
47-
return tagsMap;
27+
return TaggedResources.parseKeyValueMap(tags, false);
4828
}
4929
}

api/src/main/java/org/apache/cloudstack/api/BaseListTemplateOrIsoPermissionsCmd.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
import org.apache.log4j.Logger;
2222

2323
import org.apache.cloudstack.api.ResponseObject.ResponseView;
24+
import org.apache.cloudstack.api.command.ResponseViewProvider;
2425
import org.apache.cloudstack.api.response.TemplatePermissionsResponse;
2526

2627
import com.cloud.template.VirtualMachineTemplate;
2728
import com.cloud.user.Account;
2829

29-
public abstract class BaseListTemplateOrIsoPermissionsCmd extends BaseCmd {
30+
public abstract class BaseListTemplateOrIsoPermissionsCmd extends BaseCmd implements ResponseViewProvider {
3031
public Logger logger = getLogger();
3132
protected static final String s_name = "listtemplatepermissionsresponse";
3233

33-
3434
/////////////////////////////////////////////////////
3535
//////////////// API parameters /////////////////////
3636
/////////////////////////////////////////////////////
@@ -76,6 +76,10 @@ public String getMediaType() {
7676
return "templateOrIso";
7777
}
7878

79+
@Override
80+
public void execute() {
81+
executeWithView(getResponseView());
82+
}
7983

8084
protected void executeWithView(ResponseView view) {
8185
List<String> accountNames = _templateService.listTemplatePermissions(this);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 org.apache.cloudstack.api;
18+
19+
import static java.util.stream.Collectors.groupingBy;
20+
import static java.util.stream.Collectors.mapping;
21+
import static java.util.stream.Collectors.toList;
22+
import static java.util.stream.Collectors.toMap;
23+
24+
import java.util.List;
25+
import java.util.Map;
26+
import java.util.function.Function;
27+
28+
import javax.annotation.Nullable;
29+
30+
import org.apache.commons.collections.MapUtils;
31+
32+
import com.cloud.exception.InvalidParameterValueException;
33+
34+
public abstract class TaggedResources {
35+
@Nullable
36+
public static Map<String, String> parseKeyValueMap(Map map, boolean allowNullValues) {
37+
Map<String, String> result = null;
38+
if (MapUtils.isNotEmpty(map)) {
39+
Map<Integer, Map<String, String>> typedMap = map;
40+
result = typedMap.values()
41+
.stream()
42+
.collect(toMap(
43+
t -> t.get("key"),
44+
t -> getValue(t, allowNullValues)
45+
));
46+
}
47+
return result;
48+
}
49+
50+
@Nullable
51+
public static Map<String, List<String>> groupBy(Map map, String keyField, String valueField) {
52+
Map<String, List<String>> result = null;
53+
if (MapUtils.isNotEmpty(map)) {
54+
final Function<Map<String, String>, String> key = entry -> entry.get(keyField);
55+
final Function<Map<String, String>, String> value = entry -> entry.get(valueField);
56+
Map<Integer, Map<String, String>> typedMap = (Map<Integer, Map<String, String>>) map;
57+
result = typedMap.values()
58+
.stream()
59+
.collect(groupingBy(key, mapping(value, toList())));
60+
}
61+
62+
return result;
63+
}
64+
65+
private static String getValue(Map<String, String> tagEntry, boolean allowNullValues) {
66+
String value = tagEntry.get("value");
67+
if (value == null && !allowNullValues) {
68+
throw new InvalidParameterValueException("No value is passed in for key " + tagEntry.get("key"));
69+
}
70+
return value;
71+
}
72+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
20+
package org.apache.cloudstack.api.command;
21+
22+
import org.apache.cloudstack.api.ResponseObject;
23+
24+
public interface ResponseViewProvider {
25+
ResponseObject.ResponseView getResponseView();
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
20+
package org.apache.cloudstack.api.command.admin;
21+
22+
import org.apache.cloudstack.api.ResponseObject.ResponseView;
23+
import org.apache.cloudstack.api.command.user.UserCmd;
24+
25+
public interface AdminCmd extends UserCmd {
26+
default ResponseView getResponseView() {
27+
return ResponseView.Full;
28+
}
29+
}

api/src/main/java/org/apache/cloudstack/api/command/admin/address/AssociateIPAddrCmdByAdmin.java

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,13 @@
1919
import org.apache.log4j.Logger;
2020

2121
import org.apache.cloudstack.api.APICommand;
22-
import org.apache.cloudstack.api.ApiErrorCode;
2322
import org.apache.cloudstack.api.ResponseObject.ResponseView;
24-
import org.apache.cloudstack.api.ServerApiException;
23+
import org.apache.cloudstack.api.command.admin.AdminCmd;
2524
import org.apache.cloudstack.api.command.user.address.AssociateIPAddrCmd;
2625
import org.apache.cloudstack.api.response.IPAddressResponse;
27-
import org.apache.cloudstack.context.CallContext;
28-
29-
import com.cloud.exception.ConcurrentOperationException;
30-
import com.cloud.exception.InsufficientCapacityException;
31-
import com.cloud.exception.ResourceAllocationException;
32-
import com.cloud.exception.ResourceUnavailableException;
33-
import com.cloud.network.IpAddress;
3426

3527
@APICommand(name = "associateIpAddress", description = "Acquires and associates a public IP to an account.", responseObject = IPAddressResponse.class, responseView = ResponseView.Full,
3628
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
37-
public class AssociateIPAddrCmdByAdmin extends AssociateIPAddrCmd {
29+
public class AssociateIPAddrCmdByAdmin extends AssociateIPAddrCmd implements AdminCmd {
3830
public static final Logger s_logger = Logger.getLogger(AssociateIPAddrCmdByAdmin.class.getName());
39-
40-
41-
@Override
42-
public void execute() throws ResourceUnavailableException, ResourceAllocationException,
43-
ConcurrentOperationException, InsufficientCapacityException {
44-
CallContext.current().setEventDetails("Ip Id: " + getEntityId());
45-
46-
IpAddress result = null;
47-
48-
if (getVpcId() != null) {
49-
result = _vpcService.associateIPToVpc(getEntityId(), getVpcId());
50-
} else if (getNetworkId() != null) {
51-
result = _networkService.associateIPToNetwork(getEntityId(), getNetworkId());
52-
}
53-
54-
if (result != null) {
55-
IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(ResponseView.Full, result);
56-
ipResponse.setResponseName(getCommandName());
57-
setResponseObject(ipResponse);
58-
} else {
59-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to assign ip address");
60-
}
61-
}
62-
63-
64-
65-
6631
}

api/src/main/java/org/apache/cloudstack/api/command/admin/address/ListPublicIpAddressesCmdByAdmin.java

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,14 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.admin.address;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
21-
22-
import org.apache.log4j.Logger;
23-
2419
import org.apache.cloudstack.api.APICommand;
2520
import org.apache.cloudstack.api.ResponseObject.ResponseView;
21+
import org.apache.cloudstack.api.command.admin.AdminCmd;
2622
import org.apache.cloudstack.api.command.user.address.ListPublicIpAddressesCmd;
2723
import org.apache.cloudstack.api.response.IPAddressResponse;
28-
import org.apache.cloudstack.api.response.ListResponse;
2924

3025
import com.cloud.network.IpAddress;
31-
import com.cloud.utils.Pair;
3226

3327
@APICommand(name = "listPublicIpAddresses", description = "Lists all public ip addresses", responseObject = IPAddressResponse.class, responseView = ResponseView.Full,
3428
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, entityType = {IpAddress.class})
35-
public class ListPublicIpAddressesCmdByAdmin extends ListPublicIpAddressesCmd {
36-
public static final Logger s_logger = Logger.getLogger(ListPublicIpAddressesCmdByAdmin.class.getName());
37-
38-
@Override
39-
public void execute(){
40-
Pair<List<? extends IpAddress>, Integer> result = _mgr.searchForIPAddresses(this);
41-
ListResponse<IPAddressResponse> response = new ListResponse<IPAddressResponse>();
42-
List<IPAddressResponse> ipAddrResponses = new ArrayList<IPAddressResponse>();
43-
for (IpAddress ipAddress : result.first()) {
44-
IPAddressResponse ipResponse = _responseGenerator.createIPAddressResponse(ResponseView.Full, ipAddress);
45-
ipResponse.setObjectName("publicipaddress");
46-
ipAddrResponses.add(ipResponse);
47-
}
48-
49-
response.setResponses(ipAddrResponses, result.second());
50-
response.setResponseName(getCommandName());
51-
setResponseObject(response);
52-
}
53-
54-
}
29+
public class ListPublicIpAddressesCmdByAdmin extends ListPublicIpAddressesCmd implements AdminCmd {}

api/src/main/java/org/apache/cloudstack/api/command/admin/affinitygroup/UpdateVMAffinityGroupCmdByAdmin.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,14 @@
1616
// under the License.
1717
package org.apache.cloudstack.api.command.admin.affinitygroup;
1818

19-
import java.util.ArrayList;
20-
import java.util.EnumSet;
21-
2219
import org.apache.log4j.Logger;
2320

2421
import org.apache.cloudstack.api.APICommand;
25-
import org.apache.cloudstack.api.ApiConstants.VMDetails;
26-
import org.apache.cloudstack.api.ApiErrorCode;
2722
import org.apache.cloudstack.api.ResponseObject.ResponseView;
28-
import org.apache.cloudstack.api.ServerApiException;
23+
import org.apache.cloudstack.api.command.admin.AdminCmd;
2924
import org.apache.cloudstack.api.command.user.affinitygroup.UpdateVMAffinityGroupCmd;
3025
import org.apache.cloudstack.api.response.UserVmResponse;
31-
import org.apache.cloudstack.context.CallContext;
3226

33-
import com.cloud.exception.InsufficientCapacityException;
34-
import com.cloud.exception.ResourceUnavailableException;
35-
import com.cloud.uservm.UserVm;
3627
import com.cloud.vm.VirtualMachine;
3728

3829

@@ -41,27 +32,6 @@
4132
entityType = {VirtualMachine.class},
4233
requestHasSensitiveInfo = false,
4334
responseHasSensitiveInfo = true)
44-
public class UpdateVMAffinityGroupCmdByAdmin extends UpdateVMAffinityGroupCmd {
35+
public class UpdateVMAffinityGroupCmdByAdmin extends UpdateVMAffinityGroupCmd implements AdminCmd {
4536
public static final Logger s_logger = Logger.getLogger(UpdateVMAffinityGroupCmdByAdmin.class.getName());
46-
47-
48-
@Override
49-
public void execute() throws ResourceUnavailableException,
50-
InsufficientCapacityException, ServerApiException {
51-
CallContext.current().setEventDetails("Vm Id: "+getId());
52-
UserVm result = _affinityGroupService.updateVMAffinityGroups(getId(), getAffinityGroupIdList());
53-
ArrayList<VMDetails> dc = new ArrayList<VMDetails>();
54-
dc.add(VMDetails.valueOf("affgrp"));
55-
EnumSet<VMDetails> details = EnumSet.copyOf(dc);
56-
57-
if (result != null){
58-
UserVmResponse response = _responseGenerator.createUserVmResponse(ResponseView.Full, "virtualmachine", details, result).get(0);
59-
response.setResponseName(getCommandName());
60-
setResponseObject(response);
61-
} else {
62-
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update vm's affinity groups");
63-
}
64-
}
65-
66-
6737
}

api/src/main/java/org/apache/cloudstack/api/command/admin/domain/ListDomainsCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.cloudstack.api.BaseListCmd;
2929
import org.apache.cloudstack.api.Parameter;
3030
import org.apache.cloudstack.api.ResponseObject.ResponseView;
31+
import org.apache.cloudstack.api.command.user.UserCmd;
3132
import org.apache.cloudstack.api.response.DomainResponse;
3233
import org.apache.cloudstack.api.response.ListResponse;
3334

@@ -36,7 +37,7 @@
3637

3738
@APICommand(name = "listDomains", description = "Lists domains and provides detailed information for listed domains", responseObject = DomainResponse.class, responseView = ResponseView.Restricted, entityType = {Domain.class},
3839
requestHasSensitiveInfo = false, responseHasSensitiveInfo = false)
39-
public class ListDomainsCmd extends BaseListCmd {
40+
public class ListDomainsCmd extends BaseListCmd implements UserCmd {
4041
public static final Logger s_logger = Logger.getLogger(ListDomainsCmd.class.getName());
4142

4243
private static final String s_name = "listdomainsresponse";

0 commit comments

Comments
 (0)