Commit 6f9ac66863000dab25eb22b9e418e7b6147c6850
Committed by
Andrew Shvayka
1 parent
289d3b93
Refactoring: Introduced Edge License service. Remove web dependency from dao
Showing
7 changed files
with
143 additions
and
96 deletions
... | ... | @@ -27,6 +27,7 @@ import org.springframework.http.MediaType; |
27 | 27 | import org.springframework.security.core.Authentication; |
28 | 28 | import org.springframework.security.core.context.SecurityContextHolder; |
29 | 29 | import org.springframework.web.bind.annotation.ExceptionHandler; |
30 | +import org.thingsboard.server.cluster.TbClusterService; | |
30 | 31 | import org.thingsboard.server.common.data.Customer; |
31 | 32 | import org.thingsboard.server.common.data.Dashboard; |
32 | 33 | import org.thingsboard.server.common.data.DashboardInfo; |
... | ... | @@ -122,12 +123,12 @@ import org.thingsboard.server.queue.provider.TbQueueProducerProvider; |
122 | 123 | import org.thingsboard.server.queue.util.TbCoreComponent; |
123 | 124 | import org.thingsboard.server.service.action.RuleEngineEntityActionService; |
124 | 125 | import org.thingsboard.server.service.component.ComponentDiscoveryService; |
126 | +import org.thingsboard.server.service.edge.EdgeLicenseService; | |
125 | 127 | import org.thingsboard.server.service.edge.EdgeNotificationService; |
126 | 128 | import org.thingsboard.server.service.edge.rpc.EdgeRpcService; |
127 | 129 | import org.thingsboard.server.service.lwm2m.LwM2MServerSecurityInfoRepository; |
128 | 130 | import org.thingsboard.server.service.ota.OtaPackageStateService; |
129 | 131 | import org.thingsboard.server.service.profile.TbDeviceProfileCache; |
130 | -import org.thingsboard.server.cluster.TbClusterService; | |
131 | 132 | import org.thingsboard.server.service.resource.TbResourceService; |
132 | 133 | import org.thingsboard.server.service.security.model.SecurityUser; |
133 | 134 | import org.thingsboard.server.service.security.permission.AccessControlService; |
... | ... | @@ -274,6 +275,9 @@ public abstract class BaseController { |
274 | 275 | @Autowired(required = false) |
275 | 276 | protected EdgeRpcService edgeGrpcService; |
276 | 277 | |
278 | + @Autowired(required = false) | |
279 | + protected EdgeLicenseService edgeLicenseService; | |
280 | + | |
277 | 281 | @Autowired |
278 | 282 | protected RuleEngineEntityActionService ruleEngineEntityActionService; |
279 | 283 | ... | ... |
... | ... | @@ -572,7 +572,7 @@ public class EdgeController extends BaseController { |
572 | 572 | public ResponseEntity<JsonNode> checkInstance(@RequestBody JsonNode request) throws ThingsboardException { |
573 | 573 | log.debug("Checking instance [{}]", request); |
574 | 574 | try { |
575 | - return edgeService.checkInstance(request); | |
575 | + return edgeLicenseService.checkInstance(request); | |
576 | 576 | } catch (Exception e) { |
577 | 577 | log.error("Error occurred: [{}]", e.getMessage(), e); |
578 | 578 | throw new ThingsboardException(e, ThingsboardErrorCode.SUBSCRIPTION_VIOLATION); |
... | ... | @@ -585,7 +585,7 @@ public class EdgeController extends BaseController { |
585 | 585 | @RequestParam String releaseDate) throws ThingsboardException { |
586 | 586 | log.debug("Activating instance [{}], [{}]", licenseSecret, releaseDate); |
587 | 587 | try { |
588 | - return edgeService.activateInstance(licenseSecret, releaseDate); | |
588 | + return edgeLicenseService.activateInstance(licenseSecret, releaseDate); | |
589 | 589 | } catch (Exception e) { |
590 | 590 | log.error("Error occurred: [{}]", e.getMessage(), e); |
591 | 591 | throw new ThingsboardException(e, ThingsboardErrorCode.SUBSCRIPTION_VIOLATION); | ... | ... |
application/src/main/java/org/thingsboard/server/service/edge/DefaultEdgeLicenseService.java
0 → 100644
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.service.edge; | |
17 | + | |
18 | +import com.fasterxml.jackson.databind.JsonNode; | |
19 | +import lombok.extern.slf4j.Slf4j; | |
20 | +import org.apache.http.HttpHost; | |
21 | +import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |
22 | +import org.apache.http.impl.client.CloseableHttpClient; | |
23 | +import org.apache.http.impl.client.HttpClients; | |
24 | +import org.springframework.beans.factory.annotation.Value; | |
25 | +import org.springframework.http.ResponseEntity; | |
26 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
27 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; | |
28 | +import org.springframework.stereotype.Service; | |
29 | +import org.springframework.web.client.RestTemplate; | |
30 | +import org.thingsboard.server.queue.util.TbCoreComponent; | |
31 | + | |
32 | +import javax.annotation.PostConstruct; | |
33 | +import java.net.InetSocketAddress; | |
34 | +import java.net.Proxy; | |
35 | +import java.util.HashMap; | |
36 | +import java.util.Map; | |
37 | + | |
38 | +import static org.apache.commons.lang3.StringUtils.isNotEmpty; | |
39 | + | |
40 | +@Service | |
41 | +@TbCoreComponent | |
42 | +@Slf4j | |
43 | +public class DefaultEdgeLicenseService implements EdgeLicenseService { | |
44 | + | |
45 | + private RestTemplate restTemplate; | |
46 | + | |
47 | + private static final String EDGE_LICENSE_SERVER_ENDPOINT = "https://license.thingsboard.io"; | |
48 | + | |
49 | + @Value("${edges.enabled:false}") | |
50 | + private boolean edgesEnabled; | |
51 | + | |
52 | + @PostConstruct | |
53 | + public void init() { | |
54 | + if (edgesEnabled) { | |
55 | + initRestTemplate(); | |
56 | + } | |
57 | + } | |
58 | + | |
59 | + @Override | |
60 | + public ResponseEntity<JsonNode> checkInstance(JsonNode request) { | |
61 | + return this.restTemplate.postForEntity(EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/checkInstance", request, JsonNode.class); | |
62 | + } | |
63 | + | |
64 | + @Override | |
65 | + public ResponseEntity<JsonNode> activateInstance(String edgeLicenseSecret, String releaseDate) { | |
66 | + Map<String, String> params = new HashMap<>(); | |
67 | + params.put("licenseSecret", edgeLicenseSecret); | |
68 | + params.put("releaseDate", releaseDate); | |
69 | + return this.restTemplate.postForEntity(EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/activateInstance?licenseSecret={licenseSecret}&releaseDate={releaseDate}", null, JsonNode.class, params); | |
70 | + } | |
71 | + | |
72 | + private void initRestTemplate() { | |
73 | + boolean jdkHttpClientEnabled = isNotEmpty(System.getProperty("tb.proxy.jdk")) && System.getProperty("tb.proxy.jdk").equalsIgnoreCase("true"); | |
74 | + boolean systemProxyEnabled = isNotEmpty(System.getProperty("tb.proxy.system")) && System.getProperty("tb.proxy.system").equalsIgnoreCase("true"); | |
75 | + boolean proxyEnabled = isNotEmpty(System.getProperty("tb.proxy.host")) && isNotEmpty(System.getProperty("tb.proxy.port")); | |
76 | + if (jdkHttpClientEnabled) { | |
77 | + log.warn("Going to use plain JDK Http Client!"); | |
78 | + SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); | |
79 | + if (proxyEnabled) { | |
80 | + log.warn("Going to use Proxy Server: [{}:{}]", System.getProperty("tb.proxy.host"), System.getProperty("tb.proxy.port")); | |
81 | + factory.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(System.getProperty("tb.proxy.host"), Integer.parseInt(System.getProperty("tb.proxy.port"))))); | |
82 | + } | |
83 | + | |
84 | + this.restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory()); | |
85 | + } else { | |
86 | + CloseableHttpClient httpClient; | |
87 | + HttpComponentsClientHttpRequestFactory requestFactory; | |
88 | + if (systemProxyEnabled) { | |
89 | + log.warn("Going to use System Proxy Server!"); | |
90 | + httpClient = HttpClients.createSystem(); | |
91 | + requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
92 | + requestFactory.setHttpClient(httpClient); | |
93 | + this.restTemplate = new RestTemplate(requestFactory); | |
94 | + } else if (proxyEnabled) { | |
95 | + log.warn("Going to use Proxy Server: [{}:{}]", System.getProperty("tb.proxy.host"), System.getProperty("tb.proxy.port")); | |
96 | + httpClient = HttpClients.custom().setSSLHostnameVerifier(new DefaultHostnameVerifier()).setProxy(new HttpHost(System.getProperty("tb.proxy.host"), Integer.parseInt(System.getProperty("tb.proxy.port")), "https")).build(); | |
97 | + requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
98 | + requestFactory.setHttpClient(httpClient); | |
99 | + this.restTemplate = new RestTemplate(requestFactory); | |
100 | + } else { | |
101 | + httpClient = HttpClients.custom().setSSLHostnameVerifier(new DefaultHostnameVerifier()).build(); | |
102 | + requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
103 | + requestFactory.setHttpClient(httpClient); | |
104 | + this.restTemplate = new RestTemplate(requestFactory); | |
105 | + } | |
106 | + } | |
107 | + } | |
108 | +} | |
109 | + | |
110 | + | ... | ... |
1 | +/** | |
2 | + * Copyright © 2016-2021 The Thingsboard Authors | |
3 | + * | |
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | + * you may not use this file except in compliance with the License. | |
6 | + * You may obtain a copy of the License at | |
7 | + * | |
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | + * | |
10 | + * Unless required by applicable law or agreed to in writing, software | |
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | + * See the License for the specific language governing permissions and | |
14 | + * limitations under the License. | |
15 | + */ | |
16 | +package org.thingsboard.server.service.edge; | |
17 | + | |
18 | +import com.fasterxml.jackson.databind.JsonNode; | |
19 | +import org.springframework.http.ResponseEntity; | |
20 | + | |
21 | +public interface EdgeLicenseService { | |
22 | + | |
23 | + ResponseEntity<JsonNode> checkInstance(JsonNode request); | |
24 | + | |
25 | + ResponseEntity<JsonNode> activateInstance(String licenseSecret, String releaseDate); | |
26 | +} | ... | ... |
... | ... | @@ -82,11 +82,6 @@ |
82 | 82 | <scope>provided</scope> |
83 | 83 | </dependency> |
84 | 84 | <dependency> |
85 | - <groupId>org.springframework</groupId> | |
86 | - <artifactId>spring-web</artifactId> | |
87 | - <scope>provided</scope> | |
88 | - </dependency> | |
89 | - <dependency> | |
90 | 85 | <groupId>com.datastax.oss</groupId> |
91 | 86 | <artifactId>java-driver-core</artifactId> |
92 | 87 | <scope>provided</scope> | ... | ... |
... | ... | @@ -15,9 +15,7 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.edge; |
17 | 17 | |
18 | -import com.fasterxml.jackson.databind.JsonNode; | |
19 | 18 | import com.google.common.util.concurrent.ListenableFuture; |
20 | -import org.springframework.http.ResponseEntity; | |
21 | 19 | import org.thingsboard.server.common.data.EntitySubtype; |
22 | 20 | import org.thingsboard.server.common.data.edge.Edge; |
23 | 21 | import org.thingsboard.server.common.data.edge.EdgeInfo; |
... | ... | @@ -86,9 +84,5 @@ public interface EdgeService { |
86 | 84 | |
87 | 85 | PageData<EdgeId> findRelatedEdgeIdsByEntityId(TenantId tenantId, EntityId entityId, PageLink pageLink); |
88 | 86 | |
89 | - ResponseEntity<JsonNode> checkInstance(JsonNode request); | |
90 | - | |
91 | - ResponseEntity<JsonNode> activateInstance(String licenseSecret, String releaseDate); | |
92 | - | |
93 | 87 | String findMissingToRelatedRuleChains(TenantId tenantId, EdgeId edgeId); |
94 | 88 | } | ... | ... |
... | ... | @@ -15,7 +15,6 @@ |
15 | 15 | */ |
16 | 16 | package org.thingsboard.server.dao.edge; |
17 | 17 | |
18 | -import com.fasterxml.jackson.databind.JsonNode; | |
19 | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
20 | 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
21 | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
... | ... | @@ -24,23 +23,14 @@ import com.google.common.util.concurrent.Futures; |
24 | 23 | import com.google.common.util.concurrent.ListenableFuture; |
25 | 24 | import com.google.common.util.concurrent.MoreExecutors; |
26 | 25 | import lombok.extern.slf4j.Slf4j; |
27 | -import org.apache.http.HttpHost; | |
28 | -import org.apache.http.conn.ssl.DefaultHostnameVerifier; | |
29 | -import org.apache.http.impl.client.CloseableHttpClient; | |
30 | -import org.apache.http.impl.client.HttpClients; | |
31 | 26 | import org.hibernate.exception.ConstraintViolationException; |
32 | 27 | import org.springframework.beans.factory.annotation.Autowired; |
33 | -import org.springframework.beans.factory.annotation.Value; | |
34 | 28 | import org.springframework.cache.Cache; |
35 | 29 | import org.springframework.cache.CacheManager; |
36 | 30 | import org.springframework.cache.annotation.CacheEvict; |
37 | 31 | import org.springframework.cache.annotation.Cacheable; |
38 | -import org.springframework.http.ResponseEntity; | |
39 | -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
40 | -import org.springframework.http.client.SimpleClientHttpRequestFactory; | |
41 | 32 | import org.springframework.stereotype.Service; |
42 | 33 | import org.springframework.util.StringUtils; |
43 | -import org.springframework.web.client.RestTemplate; | |
44 | 34 | import org.thingsboard.server.common.data.Customer; |
45 | 35 | import org.thingsboard.server.common.data.EntitySubtype; |
46 | 36 | import org.thingsboard.server.common.data.EntityType; |
... | ... | @@ -74,20 +64,14 @@ import org.thingsboard.server.dao.tenant.TenantDao; |
74 | 64 | import org.thingsboard.server.dao.user.UserService; |
75 | 65 | |
76 | 66 | import javax.annotation.Nullable; |
77 | -import javax.annotation.PostConstruct; | |
78 | -import java.net.InetSocketAddress; | |
79 | -import java.net.Proxy; | |
80 | 67 | import java.util.ArrayList; |
81 | 68 | import java.util.Arrays; |
82 | 69 | import java.util.Collections; |
83 | 70 | import java.util.Comparator; |
84 | -import java.util.HashMap; | |
85 | 71 | import java.util.List; |
86 | -import java.util.Map; | |
87 | 72 | import java.util.Optional; |
88 | 73 | import java.util.stream.Collectors; |
89 | 74 | |
90 | -import static org.apache.commons.lang3.StringUtils.isNotEmpty; | |
91 | 75 | import static org.thingsboard.server.common.data.CacheConstants.EDGE_CACHE; |
92 | 76 | import static org.thingsboard.server.dao.DaoUtil.toUUIDs; |
93 | 77 | import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
... | ... | @@ -108,10 +92,6 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
108 | 92 | |
109 | 93 | private static final int DEFAULT_PAGE_SIZE = 1000; |
110 | 94 | |
111 | - private RestTemplate restTemplate; | |
112 | - | |
113 | - private static final String EDGE_LICENSE_SERVER_ENDPOINT = "https://license.thingsboard.io"; | |
114 | - | |
115 | 95 | @Autowired |
116 | 96 | private EdgeDao edgeDao; |
117 | 97 | |
... | ... | @@ -133,16 +113,6 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
133 | 113 | @Autowired |
134 | 114 | private RelationService relationService; |
135 | 115 | |
136 | - @Value("${edges.enabled:false}") | |
137 | - private boolean edgesEnabled; | |
138 | - | |
139 | - @PostConstruct | |
140 | - public void init() { | |
141 | - if (edgesEnabled) { | |
142 | - initRestTemplate(); | |
143 | - } | |
144 | - } | |
145 | - | |
146 | 116 | @Override |
147 | 117 | public Edge findEdgeById(TenantId tenantId, EdgeId edgeId) { |
148 | 118 | log.trace("Executing findEdgeById [{}]", edgeId); |
... | ... | @@ -545,19 +515,6 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
545 | 515 | } |
546 | 516 | |
547 | 517 | @Override |
548 | - public ResponseEntity<JsonNode> checkInstance(JsonNode request) { | |
549 | - return this.restTemplate.postForEntity(EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/checkInstance", request, JsonNode.class); | |
550 | - } | |
551 | - | |
552 | - @Override | |
553 | - public ResponseEntity<JsonNode> activateInstance(String edgeLicenseSecret, String releaseDate) { | |
554 | - Map<String, String> params = new HashMap<>(); | |
555 | - params.put("licenseSecret", edgeLicenseSecret); | |
556 | - params.put("releaseDate", releaseDate); | |
557 | - return this.restTemplate.postForEntity(EDGE_LICENSE_SERVER_ENDPOINT + "/api/license/activateInstance?licenseSecret={licenseSecret}&releaseDate={releaseDate}", null, JsonNode.class, params); | |
558 | - } | |
559 | - | |
560 | - @Override | |
561 | 518 | public String findMissingToRelatedRuleChains(TenantId tenantId, EdgeId edgeId) { |
562 | 519 | List<RuleChain> edgeRuleChains = findEdgeRuleChains(tenantId, edgeId); |
563 | 520 | List<RuleChainId> edgeRuleChainIds = edgeRuleChains.stream().map(IdBased::getId).collect(Collectors.toList()); |
... | ... | @@ -602,43 +559,4 @@ public class EdgeServiceImpl extends AbstractEntityService implements EdgeServic |
602 | 559 | } while (pageData != null && pageData.hasNext()); |
603 | 560 | return result; |
604 | 561 | } |
605 | - | |
606 | - private void initRestTemplate() { | |
607 | - boolean jdkHttpClientEnabled = isNotEmpty(System.getProperty("tb.proxy.jdk")) && System.getProperty("tb.proxy.jdk").equalsIgnoreCase("true"); | |
608 | - boolean systemProxyEnabled = isNotEmpty(System.getProperty("tb.proxy.system")) && System.getProperty("tb.proxy.system").equalsIgnoreCase("true"); | |
609 | - boolean proxyEnabled = isNotEmpty(System.getProperty("tb.proxy.host")) && isNotEmpty(System.getProperty("tb.proxy.port")); | |
610 | - if (jdkHttpClientEnabled) { | |
611 | - log.warn("Going to use plain JDK Http Client!"); | |
612 | - SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); | |
613 | - if (proxyEnabled) { | |
614 | - log.warn("Going to use Proxy Server: [{}:{}]", System.getProperty("tb.proxy.host"), System.getProperty("tb.proxy.port")); | |
615 | - factory.setProxy(new Proxy(Proxy.Type.HTTP, InetSocketAddress.createUnresolved(System.getProperty("tb.proxy.host"), Integer.parseInt(System.getProperty("tb.proxy.port"))))); | |
616 | - } | |
617 | - | |
618 | - this.restTemplate = new RestTemplate(new SimpleClientHttpRequestFactory()); | |
619 | - } else { | |
620 | - CloseableHttpClient httpClient; | |
621 | - HttpComponentsClientHttpRequestFactory requestFactory; | |
622 | - if (systemProxyEnabled) { | |
623 | - log.warn("Going to use System Proxy Server!"); | |
624 | - httpClient = HttpClients.createSystem(); | |
625 | - requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
626 | - requestFactory.setHttpClient(httpClient); | |
627 | - this.restTemplate = new RestTemplate(requestFactory); | |
628 | - } else if (proxyEnabled) { | |
629 | - log.warn("Going to use Proxy Server: [{}:{}]", System.getProperty("tb.proxy.host"), System.getProperty("tb.proxy.port")); | |
630 | - httpClient = HttpClients.custom().setSSLHostnameVerifier(new DefaultHostnameVerifier()).setProxy(new HttpHost(System.getProperty("tb.proxy.host"), Integer.parseInt(System.getProperty("tb.proxy.port")), "https")).build(); | |
631 | - requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
632 | - requestFactory.setHttpClient(httpClient); | |
633 | - this.restTemplate = new RestTemplate(requestFactory); | |
634 | - } else { | |
635 | - httpClient = HttpClients.custom().setSSLHostnameVerifier(new DefaultHostnameVerifier()).build(); | |
636 | - requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
637 | - requestFactory.setHttpClient(httpClient); | |
638 | - this.restTemplate = new RestTemplate(requestFactory); | |
639 | - } | |
640 | - } | |
641 | - | |
642 | - } | |
643 | - | |
644 | 562 | } | ... | ... |