Search in sources :

Example 11 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testAssignUnassignEdgeToCustomer.

@Test
public void testAssignUnassignEdgeToCustomer() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    Customer customer = new Customer();
    customer.setTitle("My customer");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    Edge assignedEdge = doPost("/api/customer/" + savedCustomer.getId().getId().toString() + "/edge/" + savedEdge.getId().getId().toString(), Edge.class);
    Assert.assertEquals(savedCustomer.getId(), assignedEdge.getCustomerId());
    Edge foundEdge = doGet("/api/edge/" + savedEdge.getId().getId().toString(), Edge.class);
    Assert.assertEquals(savedCustomer.getId(), foundEdge.getCustomerId());
    Edge unassignedEdge = doDelete("/api/customer/edge/" + savedEdge.getId().getId().toString(), Edge.class);
    Assert.assertEquals(ModelConstants.NULL_UUID, unassignedEdge.getCustomerId().getId());
    foundEdge = doGet("/api/edge/" + savedEdge.getId().getId().toString(), Edge.class);
    Assert.assertEquals(ModelConstants.NULL_UUID, foundEdge.getCustomerId().getId());
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 12 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseDashboardControllerTest method testAssignDashboardToEdge.

@Test
public void testAssignDashboardToEdge() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    Dashboard dashboard = new Dashboard();
    dashboard.setTitle("My dashboard");
    Dashboard savedDashboard = doPost("/api/dashboard", dashboard, Dashboard.class);
    doPost("/api/edge/" + savedEdge.getId().getId().toString() + "/dashboard/" + savedDashboard.getId().getId().toString(), Dashboard.class);
    PageData<Dashboard> pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId().toString() + "/dashboards?", new TypeReference<PageData<Dashboard>>() {
    }, new PageLink(100));
    Assert.assertEquals(1, pageData.getData().size());
    doDelete("/api/edge/" + savedEdge.getId().getId().toString() + "/dashboard/" + savedDashboard.getId().getId().toString(), Dashboard.class);
    pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId().toString() + "/dashboards?", new TypeReference<PageData<Dashboard>>() {
    }, new PageLink(100));
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) PageLink(org.thingsboard.server.common.data.page.PageLink) Dashboard(org.thingsboard.server.common.data.Dashboard) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 13 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseRuleChainService method deleteRuleChainById.

@Override
@Transactional
public void deleteRuleChainById(TenantId tenantId, RuleChainId ruleChainId) {
    Validator.validateId(ruleChainId, "Incorrect rule chain id for delete request.");
    RuleChain ruleChain = ruleChainDao.findById(tenantId, ruleChainId.getId());
    if (ruleChain != null) {
        if (ruleChain.isRoot()) {
            throw new DataValidationException("Deletion of Root Tenant Rule Chain is prohibited!");
        }
        if (RuleChainType.EDGE.equals(ruleChain.getType())) {
            PageLink pageLink = new PageLink(DEFAULT_PAGE_SIZE);
            PageData<Edge> pageData;
            do {
                pageData = edgeService.findEdgesByTenantIdAndEntityId(tenantId, ruleChainId, pageLink);
                if (pageData != null && pageData.getData() != null && !pageData.getData().isEmpty()) {
                    for (Edge edge : pageData.getData()) {
                        if (edge.getRootRuleChainId() != null && edge.getRootRuleChainId().equals(ruleChainId)) {
                            throw new DataValidationException("Can't delete rule chain that is root for edge [" + edge.getName() + "]. Please assign another root rule chain first to the edge!");
                        }
                    }
                    if (pageData.hasNext()) {
                        pageLink = pageLink.nextPageLink();
                    }
                }
            } while (pageData != null && pageData.hasNext());
        }
    }
    checkRuleNodesAndDelete(tenantId, ruleChainId);
}
Also used : DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) PageLink(org.thingsboard.server.common.data.page.PageLink) Edge(org.thingsboard.server.common.data.edge.Edge) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseRuleChainService method assignRuleChainToEdge.

@Override
public RuleChain assignRuleChainToEdge(TenantId tenantId, RuleChainId ruleChainId, EdgeId edgeId) {
    RuleChain ruleChain = findRuleChainById(tenantId, ruleChainId);
    Edge edge = edgeService.findEdgeById(tenantId, edgeId);
    if (edge == null) {
        throw new DataValidationException("Can't assign ruleChain to non-existent edge!");
    }
    if (!edge.getTenantId().equals(ruleChain.getTenantId())) {
        throw new DataValidationException("Can't assign ruleChain to edge from different tenant!");
    }
    if (!RuleChainType.EDGE.equals(ruleChain.getType())) {
        throw new DataValidationException("Can't assign non EDGE ruleChain to edge!");
    }
    try {
        createRelation(tenantId, new EntityRelation(edgeId, ruleChainId, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE));
    } catch (Exception e) {
        log.warn("[{}] Failed to create ruleChain relation. Edge Id: [{}]", ruleChainId, edgeId);
        throw new RuntimeException(e);
    }
    return ruleChain;
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) RuleChain(org.thingsboard.server.common.data.rule.RuleChain) Edge(org.thingsboard.server.common.data.edge.Edge) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException)

Example 15 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class EdgeController method getEdgesByIds.

@ApiOperation(value = "Get Edges By Ids (getEdgesByIds)", notes = "Requested edges must be owned by tenant or assigned to customer which user is performing the request." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
@RequestMapping(value = "/edges", params = { "edgeIds" }, method = RequestMethod.GET)
@ResponseBody
public List<Edge> getEdgesByIds(@ApiParam(value = "A list of edges ids, separated by comma ','", required = true) @RequestParam("edgeIds") String[] strEdgeIds) throws ThingsboardException {
    checkArrayParameter("edgeIds", strEdgeIds);
    try {
        SecurityUser user = getCurrentUser();
        TenantId tenantId = user.getTenantId();
        CustomerId customerId = user.getCustomerId();
        List<EdgeId> edgeIds = new ArrayList<>();
        for (String strEdgeId : strEdgeIds) {
            edgeIds.add(new EdgeId(toUUID(strEdgeId)));
        }
        ListenableFuture<List<Edge>> edgesFuture;
        if (customerId == null || customerId.isNullUid()) {
            edgesFuture = edgeService.findEdgesByTenantIdAndIdsAsync(tenantId, edgeIds);
        } else {
            edgesFuture = edgeService.findEdgesByTenantIdCustomerIdAndIdsAsync(tenantId, customerId, edgeIds);
        }
        List<Edge> edges = edgesFuture.get();
        return checkNotNull(edges);
    } catch (Exception e) {
        throw handleException(e);
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) EdgeId(org.thingsboard.server.common.data.id.EdgeId) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Edge(org.thingsboard.server.common.data.edge.Edge) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) DataValidationException(org.thingsboard.server.dao.exception.DataValidationException) ThingsboardException(org.thingsboard.server.common.data.exception.ThingsboardException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13