com.sitewhere.grpc.client.tenant.TenantManagementApiChannel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sitewhere-grpc-client Show documentation
Show all versions of sitewhere-grpc-client Show documentation
SiteWhere gRPC Client Components
/*
* Copyright (c) SiteWhere, LLC. All rights reserved. http://www.sitewhere.com
*
* The software in this package is published under the terms of the CPAL v1.0
* license, a copy of which has been included with this distribution in the
* LICENSE.txt file.
*/
package com.sitewhere.grpc.client.tenant;
import com.sitewhere.grpc.client.ApiChannel;
import com.sitewhere.grpc.client.GrpcUtils;
import com.sitewhere.grpc.client.spi.client.ITenantManagementApiChannel;
import com.sitewhere.grpc.service.GCreateTenantRequest;
import com.sitewhere.grpc.service.GCreateTenantResponse;
import com.sitewhere.grpc.service.GDeleteTenantRequest;
import com.sitewhere.grpc.service.GDeleteTenantResponse;
import com.sitewhere.grpc.service.GGetTenantRequest;
import com.sitewhere.grpc.service.GGetTenantResponse;
import com.sitewhere.grpc.service.GListTenantsRequest;
import com.sitewhere.grpc.service.GListTenantsResponse;
import com.sitewhere.grpc.service.GUpdateTenantRequest;
import com.sitewhere.grpc.service.GUpdateTenantResponse;
import com.sitewhere.grpc.service.TenantManagementGrpc;
import com.sitewhere.grpc.tenant.TenantModelConverter;
import com.sitewhere.spi.SiteWhereException;
import com.sitewhere.spi.microservice.IFunctionIdentifier;
import com.sitewhere.spi.microservice.MicroserviceIdentifier;
import com.sitewhere.spi.microservice.grpc.GrpcServiceIdentifier;
import com.sitewhere.spi.microservice.grpc.IGrpcServiceIdentifier;
import com.sitewhere.spi.microservice.grpc.IGrpcSettings;
import com.sitewhere.spi.microservice.instance.IInstanceSettings;
import com.sitewhere.spi.search.ISearchResults;
import com.sitewhere.spi.search.tenant.ITenantSearchCriteria;
import com.sitewhere.spi.tenant.ITenant;
import com.sitewhere.spi.tenant.request.ITenantCreateRequest;
/**
* Supports SiteWhere tenant management APIs on top of a
* {@link TenantManagementGrpcChannel}.
*/
public class TenantManagementApiChannel extends ApiChannel
implements ITenantManagementApiChannel {
public TenantManagementApiChannel(IInstanceSettings settings) {
super(settings, MicroserviceIdentifier.InstanceManagement, GrpcServiceIdentifier.TenantManagement,
IGrpcSettings.DEFAULT_API_PORT);
}
/*
* @see
* com.sitewhere.grpc.client.spi.IApiChannel#createGrpcChannel(com.sitewhere.spi
* .microservice.instance.IInstanceSettings,
* com.sitewhere.spi.microservice.IFunctionIdentifier,
* com.sitewhere.spi.microservice.grpc.IGrpcServiceIdentifier, int)
*/
@Override
public TenantManagementGrpcChannel createGrpcChannel(IInstanceSettings settings, IFunctionIdentifier identifier,
IGrpcServiceIdentifier grpcServiceIdentifier, int port) {
return new TenantManagementGrpcChannel(settings, identifier, grpcServiceIdentifier, port);
}
/*
* (non-Javadoc)
*
* @see
* com.sitewhere.spi.tenant.ITenantManagement#createTenant(com.sitewhere.spi
* .tenant.request.ITenantCreateRequest)
*/
@Override
public ITenant createTenant(ITenantCreateRequest request) throws SiteWhereException {
try {
GrpcUtils.handleClientMethodEntry(this, TenantManagementGrpc.getCreateTenantMethod());
GCreateTenantRequest.Builder grequest = GCreateTenantRequest.newBuilder();
grequest.setRequest(TenantModelConverter.asGrpcTenantCreateRequest(request));
GCreateTenantResponse gresponse = getGrpcChannel().getBlockingStub().createTenant(grequest.build());
ITenant response = TenantModelConverter.asApiTenant(gresponse.getTenant());
GrpcUtils.logClientMethodResponse(TenantManagementGrpc.getCreateTenantMethod(), response);
return response;
} catch (Throwable t) {
throw GrpcUtils.handleClientMethodException(TenantManagementGrpc.getCreateTenantMethod(), t);
}
}
/*
* @see
* com.sitewhere.spi.microservice.tenant.ITenantManagement#updateTenant(java.
* lang.String, com.sitewhere.spi.tenant.request.ITenantCreateRequest)
*/
@Override
public ITenant updateTenant(String token, ITenantCreateRequest request) throws SiteWhereException {
try {
GrpcUtils.handleClientMethodEntry(this, TenantManagementGrpc.getUpdateTenantMethod());
GUpdateTenantRequest.Builder grequest = GUpdateTenantRequest.newBuilder();
grequest.setToken(token);
grequest.setRequest(TenantModelConverter.asGrpcTenantCreateRequest(request));
GUpdateTenantResponse gresponse = getGrpcChannel().getBlockingStub().updateTenant(grequest.build());
ITenant response = TenantModelConverter.asApiTenant(gresponse.getTenant());
GrpcUtils.logClientMethodResponse(TenantManagementGrpc.getUpdateTenantMethod(), response);
return response;
} catch (Throwable t) {
throw GrpcUtils.handleClientMethodException(TenantManagementGrpc.getUpdateTenantMethod(), t);
}
}
/*
* @see
* com.sitewhere.spi.microservice.tenant.ITenantManagement#getTenant(java.lang.
* String)
*/
@Override
public ITenant getTenant(String token) throws SiteWhereException {
try {
GrpcUtils.handleClientMethodEntry(this, TenantManagementGrpc.getGetTenantMethod());
GGetTenantRequest.Builder grequest = GGetTenantRequest.newBuilder();
grequest.setToken(token);
GGetTenantResponse gresponse = getGrpcChannel().getBlockingStub().getTenant(grequest.build());
ITenant response = (gresponse.hasTenant()) ? TenantModelConverter.asApiTenant(gresponse.getTenant()) : null;
GrpcUtils.logClientMethodResponse(TenantManagementGrpc.getGetTenantMethod(), response);
return response;
} catch (Throwable t) {
throw GrpcUtils.handleClientMethodException(TenantManagementGrpc.getGetTenantMethod(), t);
}
}
/*
* (non-Javadoc)
*
* @see
* com.sitewhere.spi.tenant.ITenantManagement#listTenants(com.sitewhere.spi.
* search.user.ITenantSearchCriteria)
*/
@Override
public ISearchResults listTenants(ITenantSearchCriteria criteria) throws SiteWhereException {
try {
GrpcUtils.handleClientMethodEntry(this, TenantManagementGrpc.getListTenantsMethod());
GListTenantsRequest.Builder grequest = GListTenantsRequest.newBuilder();
grequest.setCriteria(TenantModelConverter.asGrpcTenantSearchCriteria(criteria));
GListTenantsResponse gresponse = getGrpcChannel().getBlockingStub().listTenants(grequest.build());
ISearchResults results = TenantModelConverter.asApiTenantSearchResults(gresponse.getResults());
GrpcUtils.logClientMethodResponse(TenantManagementGrpc.getListTenantsMethod(), results);
return results;
} catch (Throwable t) {
throw GrpcUtils.handleClientMethodException(TenantManagementGrpc.getListTenantsMethod(), t);
}
}
/*
* @see
* com.sitewhere.spi.microservice.tenant.ITenantManagement#deleteTenant(java.
* lang.String)
*/
@Override
public ITenant deleteTenant(String token) throws SiteWhereException {
try {
GrpcUtils.handleClientMethodEntry(this, TenantManagementGrpc.getDeleteTenantMethod());
GDeleteTenantRequest.Builder grequest = GDeleteTenantRequest.newBuilder();
grequest.setToken(token);
GDeleteTenantResponse gresponse = getGrpcChannel().getBlockingStub().deleteTenant(grequest.build());
ITenant response = TenantModelConverter.asApiTenant(gresponse.getTenant());
GrpcUtils.logClientMethodResponse(TenantManagementGrpc.getDeleteTenantMethod(), response);
return response;
} catch (Throwable t) {
throw GrpcUtils.handleClientMethodException(TenantManagementGrpc.getDeleteTenantMethod(), t);
}
}
}