Skip to content

Commit

Permalink
Add Controllers
Browse files Browse the repository at this point in the history
- Add Json RPC methods
- Add REST endpoint
  • Loading branch information
ghareeb-falazi committed Apr 26, 2023
1 parent 7535b03 commit 663c079
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Institute for the Architecture of Application System - University of Stuttgart
* Copyright (c) 2019-2023 Institute for the Architecture of Application System - University of Stuttgart
* Author: Ghareeb Falazi
*
* This program and the accompanying materials are made available under the
Expand All @@ -12,12 +12,15 @@
package blockchains.iaas.uni.stuttgart.de.jsonrpc;

import java.util.List;
import java.util.UUID;

import blockchains.iaas.uni.stuttgart.de.api.exceptions.InvalidScipParameterException;
import blockchains.iaas.uni.stuttgart.de.management.BlockchainManager;
import blockchains.iaas.uni.stuttgart.de.api.model.Parameter;
import blockchains.iaas.uni.stuttgart.de.api.model.QueryResult;
import blockchains.iaas.uni.stuttgart.de.api.model.TimeFrame;
import blockchains.iaas.uni.stuttgart.de.management.tccsci.DistributedTransactionManager;
import blockchains.iaas.uni.stuttgart.de.management.tccsci.DistributedTransactionRepository;
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcMethod;
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcOptional;
import com.github.arteam.simplejsonrpc.core.annotation.JsonRpcParam;
Expand All @@ -34,6 +37,7 @@ public class BalService {
private final String blockchainType;
private final String blockchainId;
private final String smartContractPath;
private static final String DTX_ID_FIELD_NAME = "dtx_id";

@JsonRpcMethod
public String Invoke(
Expand All @@ -47,9 +51,15 @@ public String Invoke(
@JsonRpcParam("signature") String signature
) {
log.info("Invoke method is executed!");
BlockchainManager manager = new BlockchainManager();
manager.invokeSmartContractFunction(blockchainId, smartContractPath, functionIdentifier, inputs, outputs,
requiredConfidence, callbackUrl, timeoutMillis, correlationId, signature);
if (inputs.stream().anyMatch(p -> p.getName().equals(DTX_ID_FIELD_NAME))) {
DistributedTransactionManager distributedTransactionManager = new DistributedTransactionManager();
distributedTransactionManager.invokeSc(blockchainId, smartContractPath, functionIdentifier, inputs, outputs,
requiredConfidence, callbackUrl, timeoutMillis, correlationId, signature);
} else {
BlockchainManager manager = new BlockchainManager();
manager.invokeSmartContractFunction(blockchainId, smartContractPath, functionIdentifier, inputs, outputs,
requiredConfidence, callbackUrl, timeoutMillis, correlationId, signature);
}

return "OK";
}
Expand Down Expand Up @@ -122,4 +132,32 @@ public QueryResult Query(

throw new InvalidScipParameterException();
}

@JsonRpcMethod
public String Start_Dtx() {
log.info("Start_Dtx method is executed!");
DistributedTransactionManager manager = new DistributedTransactionManager();

return manager.startDtx().toString();
}

@JsonRpcMethod
public String Commit_Dtx(@JsonRpcParam(DTX_ID_FIELD_NAME) String dtxId) {
log.info("Commit_Dtx method is executed!");
UUID uuid = UUID.fromString(dtxId);
DistributedTransactionManager manager = new DistributedTransactionManager();
manager.commitDtx(uuid);

return "OK";
}

@JsonRpcMethod
public String Abort_Dtx(@JsonRpcParam(DTX_ID_FIELD_NAME) String dtxId) {
log.info("Abort_Dtx method is executed!");
UUID uuid = UUID.fromString(dtxId);
DistributedTransactionManager manager = new DistributedTransactionManager();
manager.abortDtx(uuid);

return "OK";
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/********************************************************************************
* Copyright (c) 2023 Institute for the Architecture of Application System -
* University of Stuttgart
* Author: Ghareeb Falazi
*
* This program and the accompanying materials are made available under the
* terms the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package blockchains.iaas.uni.stuttgart.de.management.tccsci;

import blockchains.iaas.uni.stuttgart.de.adaptation.AdapterManager;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/********************************************************************************
* Copyright (c) 2023 Institute for the Architecture of Application System -
* University of Stuttgart
* Author: Ghareeb Falazi
*
* This program and the accompanying materials are made available under the
* terms the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package blockchains.iaas.uni.stuttgart.de.management.tccsci;

import blockchains.iaas.uni.stuttgart.de.management.model.DistributedTransaction;
Expand Down Expand Up @@ -38,6 +50,10 @@ public DistributedTransaction getById(UUID txId) {
return distributedTransactions.stream().filter(tx->tx.getId().equals(txId)).findFirst().orElse(null);
}

public List<DistributedTransaction> getAll() {
return this.distributedTransactions;
}

public Collection<DistributedTransaction> getByState(DistributedTransactionState state) {
return distributedTransactions.stream().filter(tx->tx.getState().equals(state)).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/********************************************************************************
* Copyright (c) 2023 Institute for the Architecture of Application System -
* University of Stuttgart
*
* Author: Ghareeb Falazi
*
* This program and the accompanying materials are made available under the
* terms the Apache Software License 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

package blockchains.iaas.uni.stuttgart.de.restapi.Controllers;

import blockchains.iaas.uni.stuttgart.de.management.model.DistributedTransaction;
import blockchains.iaas.uni.stuttgart.de.management.tccsci.DistributedTransactionRepository;
import blockchains.iaas.uni.stuttgart.de.restapi.model.response.LinkCollectionResponse;
import blockchains.iaas.uni.stuttgart.de.restapi.util.UriUtil;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.List;
import java.util.UUID;

@Path("distributed-transactions")
public class DistributedTransactionsController {

@Context
protected UriInfo uriInfo;
@GET
public Response get() {
List<DistributedTransaction> all = DistributedTransactionRepository.getInstance().getAll();
final LinkCollectionResponse response = new LinkCollectionResponse();

for (final DistributedTransaction dtx : all) {
response.add(UriUtil.generateSubResourceLink(uriInfo, dtx.getId().toString(), false, "self"));
}

return Response.ok(response).build();
}

@GET
@Path("/{dtxId}")
public Response getSubscriptionDetails(@PathParam("dtxId") final String dtxId) {
UUID uuid = UUID.fromString(dtxId);
DistributedTransaction dtx = DistributedTransactionRepository.getInstance().getById(uuid);
if (dtx != null) {
return Response
.status(Response.Status.OK)
.entity(dtx)
.type(MediaType.APPLICATION_JSON)
.build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}

}

0 comments on commit 663c079

Please sign in to comment.