Skip to content

Commit fc0bf21

Browse files
author
Kishan Kavala
committed
CLOUDSTACK-786, CLOUDSTACK-1014: Moved usage APIs to cloud-api. Removed ManagementServerExt. Usage API related implementation is added to UsageServiceImpl
1 parent 2234e04 commit fc0bf21

35 files changed

Lines changed: 608 additions & 471 deletions

api/src/com/cloud/dao/EntityManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ public interface EntityManager {
3838
*/
3939
public <T, K extends Serializable> T findById(Class<T> entityType, K id);
4040

41+
/**
42+
* Finds an entity by its id including removed.
43+
* @param <T> class of the entity you're trying to find.
44+
* @param <K> class of the id that the entity uses.
45+
* @param entityType Type of the entity.
46+
* @param id id value
47+
* @return T if found; null if not.
48+
*/
49+
public <T, K extends Serializable> T findByIdIncludingRemoved(Class<T> entityType, K id);
50+
4151
/**
4252
* Finds a unique entity by uuid string
4353
* @param <T> entity class
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 com.cloud.network;
18+
19+
import java.util.List;
20+
21+
import com.cloud.host.Host;
22+
23+
import org.apache.cloudstack.api.command.admin.usage.AddTrafficMonitorCmd;
24+
import org.apache.cloudstack.api.command.admin.usage.DeleteTrafficMonitorCmd;
25+
import org.apache.cloudstack.api.command.admin.usage.ListTrafficMonitorsCmd;
26+
import org.apache.cloudstack.api.response.TrafficMonitorResponse;
27+
import com.cloud.utils.component.Manager;
28+
29+
public interface NetworkUsageService extends Manager {
30+
31+
Host addTrafficMonitor(AddTrafficMonitorCmd cmd);
32+
33+
boolean deleteTrafficMonitor(DeleteTrafficMonitorCmd cmd);
34+
35+
List<? extends Host> listTrafficMonitors(ListTrafficMonitorsCmd cmd);
36+
37+
}

api/src/org/apache/cloudstack/api/BaseCmd.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import org.apache.cloudstack.query.QueryService;
3131
import org.apache.cloudstack.region.RegionService;
32+
import org.apache.cloudstack.usage.UsageService;
3233
import org.apache.log4j.Logger;
3334

3435
import com.cloud.configuration.ConfigurationService;
@@ -43,6 +44,7 @@
4344
import com.cloud.exception.ResourceAllocationException;
4445
import com.cloud.exception.ResourceUnavailableException;
4546
import com.cloud.network.NetworkService;
47+
import com.cloud.network.NetworkUsageService;
4648
import com.cloud.network.StorageNetworkService;
4749
import com.cloud.network.VpcVirtualNetworkApplianceService;
4850
import com.cloud.network.as.AutoScaleService;
@@ -124,6 +126,8 @@ public enum CommandType {
124126
@Inject public Site2SiteVpnService _s2sVpnService;
125127

126128
@Inject public QueryService _queryService;
129+
@Inject public UsageService _usageService;
130+
@Inject public NetworkUsageService _networkUsageService;
127131

128132
public abstract void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException;
129133

api/src/org/apache/cloudstack/api/ResponseGenerator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@
8383
import org.apache.cloudstack.api.response.SystemVmResponse;
8484
import org.apache.cloudstack.api.response.TemplatePermissionsResponse;
8585
import org.apache.cloudstack.api.response.TemplateResponse;
86+
import org.apache.cloudstack.api.response.TrafficMonitorResponse;
8687
import org.apache.cloudstack.api.response.TrafficTypeResponse;
88+
import org.apache.cloudstack.api.response.UsageRecordResponse;
8789
import org.apache.cloudstack.api.response.UserResponse;
8890
import org.apache.cloudstack.api.response.UserVmResponse;
8991
import org.apache.cloudstack.api.response.VirtualRouterProviderResponse;
@@ -94,6 +96,7 @@
9496
import org.apache.cloudstack.api.response.VpnUsersResponse;
9597
import org.apache.cloudstack.api.response.ZoneResponse;
9698
import org.apache.cloudstack.region.Region;
99+
import org.apache.cloudstack.usage.Usage;
97100

98101
import com.cloud.async.AsyncJob;
99102
import com.cloud.capacity.Capacity;
@@ -375,4 +378,8 @@ public interface ResponseGenerator {
375378
GuestOSResponse createGuestOSResponse(GuestOS os);
376379

377380
SnapshotScheduleResponse createSnapshotScheduleResponse(SnapshotSchedule sched);
381+
382+
UsageRecordResponse createUsageResponse(Usage usageRecord);
383+
384+
TrafficMonitorResponse createTrafficMonitorResponse(Host trafficMonitor);
378385
}

server/src/com/cloud/api/commands/AddTrafficMonitorCmd.java renamed to api/src/org/apache/cloudstack/api/command/admin/usage/AddTrafficMonitorCmd.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package com.cloud.api.commands;
17+
package org.apache.cloudstack.api.command.admin.usage;
1818

1919
import javax.inject.Inject;
2020

@@ -30,15 +30,13 @@
3030

3131
import com.cloud.exception.InvalidParameterValueException;
3232
import com.cloud.host.Host;
33-
import com.cloud.network.NetworkUsageManager;
3433
import com.cloud.user.Account;
3534
import com.cloud.utils.exception.CloudRuntimeException;
3635

3736
@APICommand(name = "addTrafficMonitor", description="Adds Traffic Monitor Host for Direct Network Usage", responseObject = TrafficMonitorResponse.class)
3837
public class AddTrafficMonitorCmd extends BaseCmd {
3938
public static final Logger s_logger = Logger.getLogger(AddTrafficMonitorCmd.class.getName());
4039
private static final String s_name = "addtrafficmonitorresponse";
41-
@Inject NetworkUsageManager networkUsageMgr;
4240

4341
/////////////////////////////////////////////////////
4442
//////////////// API parameters /////////////////////
@@ -94,8 +92,8 @@ public long getEntityOwnerId() {
9492
@Override
9593
public void execute(){
9694
try {
97-
Host trafficMonitor = networkUsageMgr.addTrafficMonitor(this);
98-
TrafficMonitorResponse response = networkUsageMgr.getApiResponse(trafficMonitor);
95+
Host trafficMonitor = _networkUsageService.addTrafficMonitor(this);
96+
TrafficMonitorResponse response = _responseGenerator.createTrafficMonitorResponse(trafficMonitor);
9997
response.setObjectName("trafficmonitor");
10098
response.setResponseName(getCommandName());
10199
this.setResponseObject(response);

server/src/com/cloud/api/commands/DeleteTrafficMonitorCmd.java renamed to api/src/org/apache/cloudstack/api/command/admin/usage/DeleteTrafficMonitorCmd.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package com.cloud.api.commands;
17+
package org.apache.cloudstack.api.command.admin.usage;
1818

1919
import javax.inject.Inject;
2020

@@ -29,14 +29,12 @@
2929
import org.apache.log4j.Logger;
3030

3131
import com.cloud.exception.InvalidParameterValueException;
32-
import com.cloud.network.NetworkUsageManager;
3332
import com.cloud.user.Account;
3433

3534
@APICommand(name = "deleteTrafficMonitor", description="Deletes an traffic monitor host.", responseObject = SuccessResponse.class)
3635
public class DeleteTrafficMonitorCmd extends BaseCmd {
3736
public static final Logger s_logger = Logger.getLogger(DeleteTrafficMonitorCmd.class.getName());
3837
private static final String s_name = "deletetrafficmonitorresponse";
39-
@Inject NetworkUsageManager _networkUsageMgr;
4038

4139
/////////////////////////////////////////////////////
4240
//////////////// API parameters /////////////////////
@@ -71,7 +69,7 @@ public long getEntityOwnerId() {
7169
@Override
7270
public void execute(){
7371
try {
74-
boolean result = _networkUsageMgr.deleteTrafficMonitor(this);
72+
boolean result = _networkUsageService.deleteTrafficMonitor(this);
7573
if (result) {
7674
SuccessResponse response = new SuccessResponse(getCommandName());
7775
response.setResponseName(getCommandName());

server/src/com/cloud/api/commands/GenerateUsageRecordsCmd.java renamed to api/src/org/apache/cloudstack/api/command/admin/usage/GenerateUsageRecordsCmd.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package com.cloud.api.commands;
17+
package org.apache.cloudstack.api.command.admin.usage;
1818

1919
import java.util.Date;
2020

@@ -24,7 +24,6 @@
2424

2525
import org.apache.cloudstack.api.APICommand;
2626
import org.apache.cloudstack.api.response.SuccessResponse;
27-
import com.cloud.server.ManagementServerExt;
2827
import com.cloud.user.Account;
2928

3029
@APICommand(name = "generateUsageRecords", description="Generates usage records. This will generate records only if there any records to be generated, i.e if the scheduled usage job was not run or failed", responseObject=SuccessResponse.class)
@@ -79,8 +78,7 @@ public long getEntityOwnerId() {
7978

8079
@Override
8180
public void execute(){
82-
ManagementServerExt _mgrExt = (ManagementServerExt)_mgr;
83-
boolean result = _mgrExt.generateUsageRecords(this);
81+
boolean result = _usageService.generateUsageRecords(this);
8482
if (result) {
8583
SuccessResponse response = new SuccessResponse(getCommandName());
8684
this.setResponseObject(response);
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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.command.admin.usage;
18+
19+
import java.util.ArrayList;
20+
import java.util.Date;
21+
import java.util.List;
22+
import java.util.TimeZone;
23+
24+
import org.apache.cloudstack.api.APICommand;
25+
import org.apache.cloudstack.api.ApiConstants;
26+
import org.apache.cloudstack.api.BaseListCmd;
27+
import org.apache.cloudstack.api.Parameter;
28+
import org.apache.cloudstack.api.response.AccountResponse;
29+
import org.apache.cloudstack.api.response.DomainResponse;
30+
import org.apache.cloudstack.api.response.ListResponse;
31+
import org.apache.cloudstack.api.response.ProjectResponse;
32+
import org.apache.cloudstack.api.response.UsageRecordResponse;
33+
import org.apache.cloudstack.usage.Usage;
34+
import org.apache.log4j.Logger;
35+
36+
@APICommand(name = "listUsageRecords", description="Lists usage records for accounts", responseObject=UsageRecordResponse.class)
37+
public class GetUsageRecordsCmd extends BaseListCmd {
38+
public static final Logger s_logger = Logger.getLogger(GetUsageRecordsCmd.class.getName());
39+
40+
private static final String s_name = "listusagerecordsresponse";
41+
42+
/////////////////////////////////////////////////////
43+
//////////////// API parameters /////////////////////
44+
/////////////////////////////////////////////////////
45+
46+
@Parameter(name=ApiConstants.ACCOUNT, type=CommandType.STRING, description="List usage records for the specified user.")
47+
private String accountName;
48+
49+
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.UUID, entityType = DomainResponse.class,
50+
description="List usage records for the specified domain.")
51+
private Long domainId;
52+
53+
@Parameter(name=ApiConstants.END_DATE, type=CommandType.DATE, required=true, description="End date range for usage record query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-03.")
54+
private Date endDate;
55+
56+
@Parameter(name=ApiConstants.START_DATE, type=CommandType.DATE, required=true, description="Start date range for usage record query. Use yyyy-MM-dd as the date format, e.g. startDate=2009-06-01.")
57+
private Date startDate;
58+
59+
@Parameter(name=ApiConstants.ACCOUNT_ID, type=CommandType.UUID, entityType = AccountResponse.class,
60+
description="List usage records for the specified account")
61+
private Long accountId;
62+
63+
@Parameter(name=ApiConstants.PROJECT_ID, type=CommandType.UUID, entityType = ProjectResponse.class,
64+
description="List usage records for specified project")
65+
private Long projectId;
66+
67+
@Parameter(name=ApiConstants.TYPE, type=CommandType.LONG, description="List usage records for the specified usage type")
68+
private Long usageType;
69+
70+
/////////////////////////////////////////////////////
71+
/////////////////// Accessors ///////////////////////
72+
/////////////////////////////////////////////////////
73+
74+
public String getAccountName() {
75+
return accountName;
76+
}
77+
78+
public Long getDomainId() {
79+
return domainId;
80+
}
81+
82+
public Date getEndDate() {
83+
return endDate;
84+
}
85+
86+
public Date getStartDate() {
87+
return startDate;
88+
}
89+
90+
public Long getAccountId() {
91+
return accountId;
92+
}
93+
94+
public Long getUsageType() {
95+
return usageType;
96+
}
97+
98+
public Long getProjectId() {
99+
return projectId;
100+
}
101+
102+
/////////////////////////////////////////////////////
103+
/////////////// API Implementation///////////////////
104+
/////////////////////////////////////////////////////
105+
106+
@Override
107+
public String getCommandName() {
108+
return s_name;
109+
}
110+
111+
@Override
112+
public void execute(){
113+
List<? extends Usage> usageRecords = _usageService.getUsageRecords(this);
114+
ListResponse<UsageRecordResponse> response = new ListResponse<UsageRecordResponse>();
115+
List<UsageRecordResponse> usageResponses = new ArrayList<UsageRecordResponse>();
116+
for(Usage usageRecord: usageRecords){
117+
UsageRecordResponse usageResponse = _responseGenerator.createUsageResponse(usageRecord);
118+
usageResponse.setObjectName("usagerecord");
119+
usageResponses.add(usageResponse);
120+
}
121+
122+
response.setResponses(usageResponses);
123+
response.setResponseName(getCommandName());
124+
this.setResponseObject(response);
125+
}
126+
}

server/src/com/cloud/api/commands/ListTrafficMonitorsCmd.java renamed to api/src/org/apache/cloudstack/api/command/admin/usage/ListTrafficMonitorsCmd.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package com.cloud.api.commands;
17+
package org.apache.cloudstack.api.command.admin.usage;
1818

1919
import java.util.ArrayList;
2020
import java.util.List;
@@ -32,15 +32,13 @@
3232
import org.apache.log4j.Logger;
3333

3434
import com.cloud.host.Host;
35-
import com.cloud.network.NetworkUsageManager;
3635

3736

3837
@APICommand(name = "listTrafficMonitors", description="List traffic monitor Hosts.", responseObject = TrafficMonitorResponse.class)
3938
public class ListTrafficMonitorsCmd extends BaseListCmd {
4039
public static final Logger s_logger = Logger.getLogger(ListServiceOfferingsCmd.class.getName());
4140
private static final String s_name = "listtrafficmonitorsresponse";
4241

43-
@Inject NetworkUsageManager networkUsageMgr;
4442
/////////////////////////////////////////////////////
4543
//////////////// API parameters /////////////////////
4644
/////////////////////////////////////////////////////
@@ -68,12 +66,12 @@ public String getCommandName() {
6866

6967
@Override
7068
public void execute(){
71-
List<? extends Host> trafficMonitors = networkUsageMgr.listTrafficMonitors(this);
69+
List<? extends Host> trafficMonitors = _networkUsageService.listTrafficMonitors(this);
7270

7371
ListResponse<TrafficMonitorResponse> listResponse = new ListResponse<TrafficMonitorResponse>();
7472
List<TrafficMonitorResponse> responses = new ArrayList<TrafficMonitorResponse>();
7573
for (Host trafficMonitor : trafficMonitors) {
76-
TrafficMonitorResponse response = networkUsageMgr.getApiResponse(trafficMonitor);
74+
TrafficMonitorResponse response = _responseGenerator.createTrafficMonitorResponse(trafficMonitor);
7775
response.setObjectName("trafficmonitor");
7876
response.setResponseName(getCommandName());
7977
responses.add(response);

server/src/com/cloud/api/commands/ListUsageTypesCmd.java renamed to api/src/org/apache/cloudstack/api/command/admin/usage/ListUsageTypesCmd.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// KIND, either express or implied. See the License for the
1515
// specific language governing permissions and limitations
1616
// under the License.
17-
package com.cloud.api.commands;
17+
package org.apache.cloudstack.api.command.admin.usage;
1818

1919
import java.util.List;
2020

@@ -23,7 +23,6 @@
2323

2424
import org.apache.cloudstack.api.BaseCmd;
2525
import org.apache.cloudstack.api.response.ListResponse;
26-
import com.cloud.server.ManagementServerExt;
2726
import org.apache.cloudstack.api.response.UsageTypeResponse;
2827
import com.cloud.user.Account;
2928

@@ -43,8 +42,7 @@ public long getEntityOwnerId() {
4342

4443
@Override
4544
public void execute() {
46-
ManagementServerExt _mgrExt = (ManagementServerExt)_mgr;
47-
List<UsageTypeResponse> result = _mgrExt.listUsageTypes();
45+
List<UsageTypeResponse> result = _usageService.listUsageTypes();
4846
ListResponse<UsageTypeResponse> response = new ListResponse<UsageTypeResponse>();
4947
response.setResponses(result);
5048
response.setResponseName(getCommandName());

0 commit comments

Comments
 (0)