@ -20,13 +20,21 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture ;
import com.google.common.util.concurrent.MoreExecutors ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.http.HttpHost ;
import org.apache.http.conn.ssl.DefaultHostnameVerifier ;
import org.apache.http.impl.client.CloseableHttpClient ;
import org.apache.http.impl.client.HttpClients ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.cache.Cache ;
import org.springframework.cache.CacheManager ;
import org.springframework.cache.annotation.CacheEvict ;
import org.springframework.cache.annotation.Cacheable ;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory ;
import org.springframework.http.client.SimpleClientHttpRequestFactory ;
import org.springframework.stereotype.Service ;
import org.springframework.util.StringUtils ;
import org.springframework.web.client.RestTemplate ;
import org.thingsboard.server.common.data.Customer ;
import org.thingsboard.server.common.data.EntitySubtype ;
import org.thingsboard.server.common.data.EntityType ;
@ -48,14 +56,9 @@ import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntitySearchDirection ;
import org.thingsboard.server.common.data.relation.RelationTypeGroup ;
import org.thingsboard.server.common.data.rule.RuleChain ;
import org.thingsboard.server.dao.asset.AssetService ;
import org.thingsboard.server.dao.customer.CustomerDao ;
import org.thingsboard.server.dao.dashboard.DashboardService ;
import org.thingsboard.server.dao.device.DeviceService ;
import org.thingsboard.server.dao.entity.AbstractEntityService ;
import org.thingsboard.server.dao.entityview.EntityViewService ;
import org.thingsboard.server.dao.exception.DataValidationException ;
import org.thingsboard.server.dao.model.ModelConstants ;
import org.thingsboard.server.dao.relation.RelationService ;
import org.thingsboard.server.dao.rule.RuleChainService ;
import org.thingsboard.server.dao.service.DataValidator ;
@ -65,10 +68,15 @@ import org.thingsboard.server.dao.tenant.TenantDao;
import org.thingsboard.server.dao.user.UserService ;
import javax.annotation.Nullable ;
import javax.annotation.PostConstruct ;
import java.net.InetSocketAddress ;
import java.net.Proxy ;
import java.util.ArrayList ;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Optional ;
import java.util.stream.Collectors ;
@ -89,6 +97,10 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
public static final String INCORRECT_CUSTOMER_ID = "Incorrect customerId " ;
public static final String INCORRECT_EDGE_ID = "Incorrect edgeId " ;
private RestTemplate restTemplate ;
private static final String EDGE_LICENSE_SERVER_ENDPOINT = "https://license.thingsboard.io" ;
@Autowired
private EdgeDao edgeDao ;
@ -110,6 +122,17 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
@Autowired
private RelationService relationService ;
@Value ( "${edges.rpc.enabled:false}" )
private boolean edgesRpcEnabled ;
@PostConstruct
public void init ( ) {
super . init ( ) ;
if ( edgesRpcEnabled ) {
initRestTemplate ( ) ;
}
}
@Override
public Edge findEdgeById ( TenantId tenantId , EdgeId edgeId ) {
log . trace ( "Executing findEdgeById [{}]" , edgeId ) ;
@ -374,6 +397,12 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
if ( StringUtils . isEmpty ( edge . getRoutingKey ( ) ) ) {
throw new DataValidationException ( "Edge routing key should be specified!" ) ;
}
if ( StringUtils . isEmpty ( edge . getEdgeLicenseKey ( ) ) ) {
throw new DataValidationException ( "Edge license key should be specified!" ) ;
}
if ( StringUtils . isEmpty ( edge . getCloudEndpoint ( ) ) ) {
throw new DataValidationException ( "Cloud endpoint should be specified!" ) ;
}
if ( edge . getTenantId ( ) = = null ) {
throw new DataValidationException ( "Edge should be assigned to tenant!" ) ;
} else {
@ -475,4 +504,55 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic
} , MoreExecutors . directExecutor ( ) ) ;
}
@Override
public Object checkInstance ( Object request ) {
return this . restTemplate . postForEntity ( EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/checkInstance" , request , Object . class , new Object [ 0 ] ) ;
}
@Override
public Object activateInstance ( String edgeLicenseSecret , String releaseDate ) {
Map < String , String > params = new HashMap ( ) ;
params . put ( "licenseSecret" , edgeLicenseSecret ) ;
params . put ( "releaseDate" , releaseDate ) ;
return this . restTemplate . postForEntity ( EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/activateInstance?licenseSecret={licenseSecret}&releaseDate={releaseDate}" , ( Object ) null , Object . class , params ) ;
}
private void initRestTemplate ( ) {
boolean jdkHttpClientEnabled = org . apache . commons . lang3 . StringUtils . isNotEmpty ( System . getProperty ( "tb.proxy.jdk" ) ) & & System . getProperty ( "tb.proxy.jdk" ) . equalsIgnoreCase ( "true" ) ;
boolean systemProxyEnabled = org . apache . commons . lang3 . StringUtils . isNotEmpty ( System . getProperty ( "tb.proxy.system" ) ) & & System . getProperty ( "tb.proxy.system" ) . equalsIgnoreCase ( "true" ) ;
boolean proxyEnabled = org . apache . commons . lang3 . StringUtils . isNotEmpty ( System . getProperty ( "tb.proxy.host" ) ) & & org . apache . commons . lang3 . StringUtils . isNotEmpty ( System . getProperty ( "tb.proxy.port" ) ) ;
if ( jdkHttpClientEnabled ) {
log . warn ( "Going to use plain JDK Http Client!" ) ;
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory ( ) ;
if ( proxyEnabled ) {
log . warn ( "Going to use Proxy Server: [{}:{}]" , System . getProperty ( "tb.proxy.host" ) , System . getProperty ( "tb.proxy.port" ) ) ;
factory . setProxy ( new Proxy ( Proxy . Type . HTTP , InetSocketAddress . createUnresolved ( System . getProperty ( "tb.proxy.host" ) , Integer . parseInt ( System . getProperty ( "tb.proxy.port" ) ) ) ) ) ;
}
this . restTemplate = new RestTemplate ( new SimpleClientHttpRequestFactory ( ) ) ;
} else {
CloseableHttpClient httpClient ;
HttpComponentsClientHttpRequestFactory requestFactory ;
if ( systemProxyEnabled ) {
log . warn ( "Going to use System Proxy Server!" ) ;
httpClient = HttpClients . createSystem ( ) ;
requestFactory = new HttpComponentsClientHttpRequestFactory ( ) ;
requestFactory . setHttpClient ( httpClient ) ;
this . restTemplate = new RestTemplate ( requestFactory ) ;
} else if ( proxyEnabled ) {
log . warn ( "Going to use Proxy Server: [{}:{}]" , System . getProperty ( "tb.proxy.host" ) , System . getProperty ( "tb.proxy.port" ) ) ;
httpClient = HttpClients . custom ( ) . setSSLHostnameVerifier ( new DefaultHostnameVerifier ( ) ) . setProxy ( new HttpHost ( System . getProperty ( "tb.proxy.host" ) , Integer . parseInt ( System . getProperty ( "tb.proxy.port" ) ) , "https" ) ) . build ( ) ;
requestFactory = new HttpComponentsClientHttpRequestFactory ( ) ;
requestFactory . setHttpClient ( httpClient ) ;
this . restTemplate = new RestTemplate ( requestFactory ) ;
} else {
httpClient = HttpClients . custom ( ) . setSSLHostnameVerifier ( new DefaultHostnameVerifier ( ) ) . build ( ) ;
requestFactory = new HttpComponentsClientHttpRequestFactory ( ) ;
requestFactory . setHttpClient ( httpClient ) ;
this . restTemplate = new RestTemplate ( requestFactory ) ;
}
}
}
}