Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes required by Finance Service implementation #35

Merged
merged 10 commits into from
Nov 16, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cloud.fogbow.accs.api.http;

import org.springframework.web.bind.annotation.ControllerAdvice;

import cloud.fogbow.common.http.FogbowExceptionToHttpErrorConditionTranslator;

@ControllerAdvice
public class AccsExceptionToHttpErrorConditionTranslator extends FogbowExceptionToHttpErrorConditionTranslator {
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,32 @@ public ResponseEntity<List<Record>> getResourceUsageFromOtherUser(
throw e;
}
}

@RequestMapping(value = "/{userId}/{requester}/{provider}/{initialDate}/{finalDate}",
method = RequestMethod.GET)
public ResponseEntity<List<Record>> getAllResourcesUsageFromOtherUser(
@ApiParam(value = ApiDocumentation.ResourceUsage.USER_ID)
@PathVariable String userId,
@ApiParam(value = ApiDocumentation.ResourceUsage.REQUESTER)
@PathVariable String requester,
@ApiParam(value = ApiDocumentation.ResourceUsage.PROVIDER)
@PathVariable String provider,
@ApiParam(value = ApiDocumentation.ResourceUsage.INITIAL_DATE)
@PathVariable String initialDate,
@ApiParam(value = ApiDocumentation.ResourceUsage.FINAL_DATE)
@PathVariable String finalDate,
@ApiParam(value = cloud.fogbow.common.constants.ApiDocumentation.Token.SYSTEM_USER_TOKEN)
@RequestHeader(value = SystemConstants.SYSTEM_USER_TOKEN_HEADER_KEY) String systemUserToken)
throws Exception {
try {
List<Record> records = ApplicationFacade.getInstance().getAllResourcesUserRecords(userId, requester,
provider, initialDate, finalDate, systemUserToken);
return new ResponseEntity<>(records, HttpStatus.OK);
} catch (Exception e) {
LOGGER.info(String.format(Messages.Exception.GENERIC_EXCEPTION, e.getMessage()), e);
throw e;
}
}

@ApiOperation(value = ApiDocumentation.ResourceUsage.GET_OPERATION_FROM_SYSTEM_USER)
@RequestMapping(value = "/{requester}/{resourceType}/{initialDate}/{finalDate}", method = RequestMethod.GET)
Expand Down
198 changes: 198 additions & 0 deletions src/main/java/cloud/fogbow/accs/api/http/response/AccsApiUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package cloud.fogbow.accs.api.http.response;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import cloud.fogbow.accs.constants.Messages;
import cloud.fogbow.accs.core.models.OrderStateHistory;
import cloud.fogbow.accs.core.models.orders.OrderState;
import cloud.fogbow.accs.core.models.specs.ComputeSpec;
import cloud.fogbow.accs.core.models.specs.NetworkSpec;
import cloud.fogbow.accs.core.models.specs.OrderSpec;
import cloud.fogbow.accs.core.models.specs.VolumeSpec;
import cloud.fogbow.common.exceptions.InvalidParameterException;

public class AccsApiUtils {
private static final String RESOURCE_TYPE_KEY = "resourceType";
private static final String COMPUTE_RESOURCE = "compute";
private static final String VOLUME_RESOURCE = "volume";
private static final String NETWORK_RESOURCE = "network";

public AccsApiUtils() {
}

public List<Record> getRecordsFromString(String recordsString) throws InvalidParameterException {
ArrayList<Record> recordList = new ArrayList<Record>();
Gson gson = new Gson();

ArrayList<LinkedTreeMap<String, Object>> rawRecordsList =
gson.fromJson(recordsString, ArrayList.class);

for (LinkedTreeMap<String, Object> rawRecord : rawRecordsList) {
Record record;
String recordType = (String) rawRecord.get(RESOURCE_TYPE_KEY);

switch(recordType) {
case COMPUTE_RESOURCE: record = getComputeRecord(rawRecord); break;
case VOLUME_RESOURCE: record = getVolumeRecord(rawRecord); break;
case NETWORK_RESOURCE: record = getNetworkRecord(rawRecord); break;
default: throw new InvalidParameterException(
String.format(Messages.Exception.INVALID_RECORD_TYPE, recordType));
}

recordList.add(record);
}

return recordList;
}

private ComputeRecord getComputeRecord(LinkedTreeMap<String, Object> rawRecord) throws InvalidParameterException {
Gson gson = new Gson();
String jsonRepr = gson.toJson((LinkedTreeMap<String, Object>) rawRecord);
ComputeRecord computeRecord = gson.fromJson(jsonRepr, ComputeRecord.class);
computeRecord.validate();
return computeRecord;
}

private VolumeRecord getVolumeRecord(LinkedTreeMap<String, Object> rawRecord) throws InvalidParameterException {
Gson gson = new Gson();
String jsonRepr = gson.toJson((LinkedTreeMap<String, Object>) rawRecord);
VolumeRecord volumeRecord = gson.fromJson(jsonRepr, VolumeRecord.class);
volumeRecord.validate();
return volumeRecord;
}

private Record getNetworkRecord(LinkedTreeMap<String, Object> rawRecord) throws InvalidParameterException {
Gson gson = new Gson();
String jsonRepr = gson.toJson((LinkedTreeMap<String, Object>) rawRecord);
NetworkRecord networkRecord = gson.fromJson(jsonRepr, NetworkRecord.class);
networkRecord.validate();
return networkRecord;
}

public class ComputeRecord extends Record {
private ComputeSpec spec;

public ComputeRecord(Long id, String orderId, String resourceType, ComputeSpec spec, String requester, Timestamp startTime,
Timestamp startDate, Timestamp endTime, Timestamp endDate, long duration, OrderState state,
OrderStateHistory stateHistory) throws InvalidParameterException {
super(id, orderId, resourceType, requester, startTime, startDate, endTime, endDate,
duration, state, stateHistory);
this.spec = spec;
}

@Override
public ComputeSpec getSpec() {
return spec;
}

@Override
public void setSpec(OrderSpec orderSpec) {
this.spec = (ComputeSpec) orderSpec;
}

public void validate() throws InvalidParameterException {
checkRecordPropertyIsNotNull("resourceType", getResourceType());
checkRecordPropertyIsNotNull("spec", getSpec());
checkRecordPropertyIsNotNull("startTime", getStartTime());
checkRecordPropertyIsNotNull("startDate", getStartDate());
checkRecordPropertyIsNotNull("stateHistory", getStateHistory());
}
}

public class VolumeRecord extends Record {
private VolumeSpec spec;

public VolumeRecord(Long id, String orderId, String resourceType, VolumeSpec spec, String requester,
Timestamp startTime, Timestamp startDate, Timestamp endTime, Timestamp endDate, long duration,
OrderState state, OrderStateHistory stateHistory) {
super(id, orderId, resourceType, requester, startTime, startDate, endTime, endDate, duration,
state, stateHistory);
this.spec = spec;
}

@Override
public VolumeSpec getSpec() {
return spec;
}

@Override
public void setSpec(OrderSpec orderSpec) {
this.spec = (VolumeSpec) orderSpec;
}

public void validate() throws InvalidParameterException {
checkRecordPropertyIsNotNull("resourceType", getResourceType());
checkRecordPropertyIsNotNull("spec", getSpec());
checkRecordPropertyIsNotNull("startTime", getStartTime());
checkRecordPropertyIsNotNull("startDate", getStartDate());
checkRecordPropertyIsNotNull("stateHistory", getStateHistory());
}
}

public class NetworkRecord extends Record {
private NetworkSpec spec;

public NetworkRecord(Long id, String orderId, String resourceType, NetworkSpec spec, String requester,
Timestamp startTime, Timestamp startDate, Timestamp endTime, Timestamp endDate, long duration,
OrderState state, OrderStateHistory stateHistory) {
super(id, orderId, resourceType, requester, startTime, startDate, endTime, endDate,
duration, state, stateHistory);
this.spec = spec;
}

@Override
public NetworkSpec getSpec() {
return spec;
}

@Override
public void setSpec(OrderSpec orderSpec) {
this.spec = (NetworkSpec) orderSpec;
}

public void validate() throws InvalidParameterException {
checkRecordPropertyIsNotNull("resourceType", getResourceType());
checkRecordPropertyIsNotNull("spec", getSpec());
checkRecordPropertyIsNotNull("startTime", getStartTime());
checkRecordPropertyIsNotNull("startDate", getStartDate());
checkRecordPropertyIsNotNull("stateHistory", getStateHistory());
}
}

private static void checkRecordPropertyIsNotNull(String propertyName, Object o)
throws InvalidParameterException {
if (o == null) {
throw new InvalidParameterException(
String.format(Messages.Exception.INVALID_RECORD_PROPERTY, propertyName));
}
}

public Record mountResponseRecord(cloud.fogbow.accs.core.models.Record dbRecord) throws InvalidParameterException {
OrderSpec spec = dbRecord.getSpec();

if (spec instanceof ComputeSpec) {
return new ComputeRecord(dbRecord.getId(), dbRecord.getOrderId(), dbRecord.getResourceType(), (ComputeSpec) dbRecord.getSpec(),
dbRecord.getRequestingMember(), dbRecord.getStartTime(), dbRecord.getStartDate(),
dbRecord.getEndTime(), dbRecord.getEndDate(), dbRecord.getDuration(), dbRecord.getState(),
dbRecord.getStateHistory());
} else if (spec instanceof VolumeSpec) {
return new VolumeRecord(dbRecord.getId(), dbRecord.getOrderId(), dbRecord.getResourceType(), (VolumeSpec) dbRecord.getSpec(),
dbRecord.getRequestingMember(), dbRecord.getStartTime(), dbRecord.getStartDate(),
dbRecord.getEndTime(), dbRecord.getEndDate(), dbRecord.getDuration(), dbRecord.getState(),
dbRecord.getStateHistory());
} else if (spec instanceof NetworkSpec) {
return new NetworkRecord(dbRecord.getId(), dbRecord.getOrderId(), dbRecord.getResourceType(), (NetworkSpec) dbRecord.getSpec(),
dbRecord.getRequestingMember(), dbRecord.getStartTime(), dbRecord.getStartDate(),
dbRecord.getEndTime(), dbRecord.getEndDate(), dbRecord.getDuration(), dbRecord.getState(),
dbRecord.getStateHistory());
} else {
// TODO error
return null;
}
}
}
38 changes: 23 additions & 15 deletions src/main/java/cloud/fogbow/accs/api/http/response/Record.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cloud.fogbow.accs.api.http.response;

import cloud.fogbow.accs.core.models.OrderStateHistory;
import cloud.fogbow.accs.core.models.orders.OrderState;
import cloud.fogbow.accs.core.models.specs.OrderSpec;
import cloud.fogbow.accs.constants.ApiDocumentation;
Expand All @@ -10,7 +11,7 @@
import java.util.Objects;

@ApiModel
public class Record {
public abstract class Record {

@ApiModelProperty(position = 0, example = ApiDocumentation.Record.RECORD_ID)
private Long id;
Expand All @@ -21,8 +22,8 @@ public class Record {
@ApiModelProperty(position = 2, example = ApiDocumentation.Record.RESOURCE_TYPE)
private String resourceType;

@ApiModelProperty
private OrderSpec spec;
// TODO update documentation
// The spec field was moved to the Record subclasses

@ApiModelProperty(position = 4, example = ApiDocumentation.Record.REQUESTER)
private String requester;
Expand All @@ -44,6 +45,9 @@ public class Record {

@ApiModelProperty(position = 10, example = ApiDocumentation.Record.STATE)
private OrderState state;

// TODO update documentation
private OrderStateHistory stateHistory;

public Long getId() {
return id;
Expand All @@ -69,13 +73,8 @@ public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}

public OrderSpec getSpec() {
return spec;
}

public void setSpec(OrderSpec spec) {
this.spec = spec;
}
public abstract OrderSpec getSpec();
public abstract void setSpec(OrderSpec spec);

public void setEndTime(Timestamp endTime) {
this.endTime = endTime;
Expand Down Expand Up @@ -132,6 +131,14 @@ public void setEndDate(Timestamp endDate) {
public Timestamp getEndDate() {
return endDate;
}

public OrderStateHistory getStateHistory() {
return stateHistory;
}

public void setStateHistory(OrderStateHistory stateHistory) {
this.stateHistory = stateHistory;
}

@Override
public boolean equals(Object o) {
Expand All @@ -142,29 +149,30 @@ public boolean equals(Object o) {
id.equals(record.id) &&
Objects.equals(orderId, record.orderId) &&
Objects.equals(resourceType, record.resourceType) &&
Objects.equals(spec, record.spec) &&
Objects.equals(requester, record.requester) &&
Objects.equals(startTime, record.startTime);
}

@Override
public int hashCode() {
return Objects.hash(id, orderId, resourceType, spec, requester, startTime, duration);
return Objects.hash(id, orderId, resourceType, requester, startTime, duration);
}

public Record(Long id, String orderId, String resourceType, OrderSpec spec, String requester, Timestamp startTime,
Timestamp startDate, Timestamp endTime, Timestamp endDate, long duration, OrderState state) {
public Record(Long id, String orderId, String resourceType,
String requester, Timestamp startTime,
Timestamp startDate, Timestamp endTime, Timestamp endDate, long duration, OrderState state,
OrderStateHistory orderStateHistory) {
this.id = id;
this.orderId = orderId;
this.resourceType = resourceType;
this.spec = spec;
this.requester = requester;
this.startTime = startTime;
this.startDate = startDate;
this.endDate = endDate;
this.endTime = endTime;
this.duration = duration;
this.state = state;
this.stateHistory = orderStateHistory;
}

public Record() {}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/cloud/fogbow/accs/constants/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static class Exception {
public static final String BILLING_PREDICTIONS = "Accounting predictions are not allowed.";
public static final String START_TIME_GREATER_THAN_END_TIME = "Begin time must be smaller than end time.";
public static final String GENERIC_EXCEPTION = "Operation returned error: %s.";
public static final String INVALID_RECORD_TYPE = "Invalid record type: %s.";
public static final String INVALID_RECORD_PROPERTY = "Invalid record property: %s.";
}

public static class Info {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ public class SystemConstants {
public static final String WHITE_LIST_FILE = "white-list.conf";
public static final String SERVICE_BASE_ENDPOINT = "accs/";
public static final String API_VERSION_NUMBER = "1.0.0";
public static final String SIMPLE_DATE_FORMAT = "yyyy-MM-dd";
public static final String COMPLETE_DATE_FORMAT = "yyyy-MM-dd_HH:mm:ss";
}
Loading