Merge pull request #151 from thingsboard/asset-alarm-mgmt
Asset & Alarm management
This commit is contained in:
		
						commit
						6b446023b1
					
				@ -0,0 +1,129 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.controller;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.security.access.prepost.PreAuthorize;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
import org.thingsboard.server.common.data.Customer;
 | 
			
		||||
import org.thingsboard.server.common.data.Event;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
import org.thingsboard.server.common.data.asset.Asset;
 | 
			
		||||
import org.thingsboard.server.common.data.id.*;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TextPageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TextPageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.dao.asset.AssetSearchQuery;
 | 
			
		||||
import org.thingsboard.server.dao.exception.IncorrectParameterException;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.exception.ThingsboardErrorCode;
 | 
			
		||||
import org.thingsboard.server.exception.ThingsboardException;
 | 
			
		||||
import org.thingsboard.server.service.security.model.SecurityUser;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
@RequestMapping("/api")
 | 
			
		||||
public class AlarmController extends BaseController {
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
 | 
			
		||||
    @RequestMapping(value = "/alarm/{alarmId}", method = RequestMethod.GET)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Alarm getAlarmById(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException {
 | 
			
		||||
        checkParameter("alarmId", strAlarmId);
 | 
			
		||||
        try {
 | 
			
		||||
            AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
 | 
			
		||||
            return checkAlarmId(alarmId);
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
 | 
			
		||||
    @RequestMapping(value = "/alarm", method = RequestMethod.POST)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public Alarm saveAlarm(@RequestBody Alarm alarm) throws ThingsboardException {
 | 
			
		||||
        try {
 | 
			
		||||
            alarm.setTenantId(getCurrentUser().getTenantId());
 | 
			
		||||
            return checkNotNull(alarmService.createOrUpdateAlarm(alarm));
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("hasAuthority('TENANT_ADMIN')")
 | 
			
		||||
    @RequestMapping(value = "/alarm/{alarmId}/ack", method = RequestMethod.POST)
 | 
			
		||||
    @ResponseStatus(value = HttpStatus.OK)
 | 
			
		||||
    public void ackAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException {
 | 
			
		||||
        checkParameter("alarmId", strAlarmId);
 | 
			
		||||
        try {
 | 
			
		||||
            AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
 | 
			
		||||
            checkAlarmId(alarmId);
 | 
			
		||||
            alarmService.ackAlarm(alarmId, System.currentTimeMillis()).get();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("hasAuthority('TENANT_ADMIN')")
 | 
			
		||||
    @RequestMapping(value = "/alarm/{alarmId}/clear", method = RequestMethod.POST)
 | 
			
		||||
    @ResponseStatus(value = HttpStatus.OK)
 | 
			
		||||
    public void clearAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException {
 | 
			
		||||
        checkParameter("alarmId", strAlarmId);
 | 
			
		||||
        try {
 | 
			
		||||
            AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
 | 
			
		||||
            checkAlarmId(alarmId);
 | 
			
		||||
            alarmService.clearAlarm(alarmId, System.currentTimeMillis()).get();
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
 | 
			
		||||
    @RequestMapping(value = "/alarm/{entityType}/{entityId}", method = RequestMethod.GET)
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public TimePageData<Alarm> getAlarms(
 | 
			
		||||
            @PathVariable("entityType") String strEntityType,
 | 
			
		||||
            @PathVariable("entityId") String strEntityId,
 | 
			
		||||
            @RequestParam(required = false) String status,
 | 
			
		||||
            @RequestParam int limit,
 | 
			
		||||
            @RequestParam(required = false) Long startTime,
 | 
			
		||||
            @RequestParam(required = false) Long endTime,
 | 
			
		||||
            @RequestParam(required = false, defaultValue = "false") boolean ascOrder,
 | 
			
		||||
            @RequestParam(required = false) String offset
 | 
			
		||||
    ) throws ThingsboardException {
 | 
			
		||||
        checkParameter("EntityId", strEntityId);
 | 
			
		||||
        checkParameter("EntityType", strEntityType);
 | 
			
		||||
        EntityId entityId = EntityIdFactory.getByTypeAndId(strEntityType, strEntityId);
 | 
			
		||||
        AlarmStatus alarmStatus = StringUtils.isEmpty(status) ? null : AlarmStatus.valueOf(status);
 | 
			
		||||
        checkEntityId(entityId);
 | 
			
		||||
        try {
 | 
			
		||||
            TimePageLink pageLink = createPageLink(limit, startTime, endTime, ascOrder, offset);
 | 
			
		||||
            return checkNotNull(alarmService.findAlarms(new AlarmQuery(entityId, pageLink, alarmStatus)).get());
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -25,6 +25,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
 | 
			
		||||
import org.springframework.web.bind.annotation.ExceptionHandler;
 | 
			
		||||
import org.thingsboard.server.actors.service.ActorService;
 | 
			
		||||
import org.thingsboard.server.common.data.*;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.asset.Asset;
 | 
			
		||||
import org.thingsboard.server.common.data.id.*;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TextPageLink;
 | 
			
		||||
@ -36,6 +38,7 @@ import org.thingsboard.server.common.data.rule.RuleMetaData;
 | 
			
		||||
import org.thingsboard.server.common.data.security.Authority;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetType;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
 | 
			
		||||
import org.thingsboard.server.dao.alarm.AlarmService;
 | 
			
		||||
import org.thingsboard.server.dao.asset.AssetService;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerService;
 | 
			
		||||
import org.thingsboard.server.dao.dashboard.DashboardService;
 | 
			
		||||
@ -83,6 +86,9 @@ public abstract class BaseController {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected AssetService assetService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected AlarmService alarmService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected DeviceCredentialsService deviceCredentialsService;
 | 
			
		||||
 | 
			
		||||
@ -334,6 +340,22 @@ public abstract class BaseController {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Alarm checkAlarmId(AlarmId alarmId) throws ThingsboardException {
 | 
			
		||||
        try {
 | 
			
		||||
            validateId(alarmId, "Incorrect alarmId " + alarmId);
 | 
			
		||||
            Alarm alarm = alarmService.findAlarmByIdAsync(alarmId).get();
 | 
			
		||||
            checkAlarm(alarm);
 | 
			
		||||
            return alarm;
 | 
			
		||||
        } catch (Exception e) {
 | 
			
		||||
            throw handleException(e, false);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected void checkAlarm(Alarm alarm) throws ThingsboardException {
 | 
			
		||||
        checkNotNull(alarm);
 | 
			
		||||
        checkTenantId(alarm.getTenantId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    WidgetsBundle checkWidgetsBundleId(WidgetsBundleId widgetsBundleId, boolean modify) throws ThingsboardException {
 | 
			
		||||
        try {
 | 
			
		||||
            validateId(widgetsBundleId, "Incorrect widgetsBundleId " + widgetsBundleId);
 | 
			
		||||
 | 
			
		||||
@ -16,28 +16,43 @@
 | 
			
		||||
package org.thingsboard.server.common.data.alarm;
 | 
			
		||||
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.common.data.BaseData;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetId;
 | 
			
		||||
import org.thingsboard.server.common.data.HasName;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by ashvayka on 11.05.17.
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
@Builder
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class Alarm extends BaseData<AlarmId> implements HasName {
 | 
			
		||||
 | 
			
		||||
    private long startTs;
 | 
			
		||||
    private long endTs;
 | 
			
		||||
    private long ackTs;
 | 
			
		||||
    private long clearTs;
 | 
			
		||||
    private TenantId tenantId;
 | 
			
		||||
    private String type;
 | 
			
		||||
    private EntityId originator;
 | 
			
		||||
    private AlarmSeverity severity;
 | 
			
		||||
    private AlarmStatus status;
 | 
			
		||||
    private long startTs;
 | 
			
		||||
    private long endTs;
 | 
			
		||||
    private long ackTs;
 | 
			
		||||
    private long clearTs;
 | 
			
		||||
    private JsonNode details;
 | 
			
		||||
    private boolean propagate;
 | 
			
		||||
 | 
			
		||||
    public Alarm() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Alarm(AlarmId id) {
 | 
			
		||||
        super(id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getName() {
 | 
			
		||||
        return type;
 | 
			
		||||
 | 
			
		||||
@ -15,14 +15,19 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.common.data.alarm;
 | 
			
		||||
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.Builder;
 | 
			
		||||
import lombok.Data;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by ashvayka on 11.05.17.
 | 
			
		||||
 */
 | 
			
		||||
@Data
 | 
			
		||||
@Builder
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class AlarmQuery {
 | 
			
		||||
 | 
			
		||||
    private EntityId affectedEntityId;
 | 
			
		||||
 | 
			
		||||
@ -22,4 +22,12 @@ public enum AlarmStatus {
 | 
			
		||||
 | 
			
		||||
    ACTIVE_UNACK, ACTIVE_ACK, CLEARED_UNACK, CLEARED_ACK;
 | 
			
		||||
 | 
			
		||||
    public boolean isAck() {
 | 
			
		||||
        return this == ACTIVE_ACK || this == CLEARED_ACK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isCleared() {
 | 
			
		||||
        return this == CLEARED_ACK || this == CLEARED_UNACK;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -16,6 +16,7 @@
 | 
			
		||||
package org.thingsboard.server.common.data.id;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
@ -50,6 +51,8 @@ public class EntityIdFactory {
 | 
			
		||||
                return new DeviceId(uuid);
 | 
			
		||||
            case ASSET:
 | 
			
		||||
                return new AssetId(uuid);
 | 
			
		||||
            case ALARM:
 | 
			
		||||
                return new AlarmId(uuid);
 | 
			
		||||
        }
 | 
			
		||||
        throw new IllegalArgumentException("EntityType " + type + " is not supported!");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -127,7 +127,7 @@ public abstract class AbstractModelDao<T extends BaseEntity<?>> extends Abstract
 | 
			
		||||
        log.debug("Save entity {}", entity);
 | 
			
		||||
        if (entity.getId() == null) {
 | 
			
		||||
            entity.setId(UUIDs.timeBased());
 | 
			
		||||
        } else {
 | 
			
		||||
        } else if (isDeleteOnSave()) {
 | 
			
		||||
            removeById(entity.getId());
 | 
			
		||||
        }
 | 
			
		||||
        Statement saveStatement = getSaveQuery(entity);
 | 
			
		||||
@ -136,6 +136,10 @@ public abstract class AbstractModelDao<T extends BaseEntity<?>> extends Abstract
 | 
			
		||||
        return new EntityResultSet<>(resultSet, entity);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected boolean isDeleteOnSave() {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public T save(T entity) {
 | 
			
		||||
        return saveWithResult(entity).getEntity();
 | 
			
		||||
    }
 | 
			
		||||
@ -161,9 +165,18 @@ public abstract class AbstractModelDao<T extends BaseEntity<?>> extends Abstract
 | 
			
		||||
        return getSession().execute(delete);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    public List<T> find() {
 | 
			
		||||
        log.debug("Get all entities from column family {}", getColumnFamilyName());
 | 
			
		||||
        return findListByStatement(QueryBuilder.select().all().from(getColumnFamilyName()).setConsistencyLevel(cluster.getDefaultReadConsistencyLevel()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected static <T> Function<BaseEntity<T>, T> toDataFunction() {
 | 
			
		||||
        return new Function<BaseEntity<T>, T>() {
 | 
			
		||||
            @Nullable
 | 
			
		||||
            @Override
 | 
			
		||||
            public T apply(@Nullable BaseEntity<T> entity) {
 | 
			
		||||
                return entity != null ? entity.toData() : null;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -47,8 +47,27 @@ public abstract class AbstractSearchTimeDao<T extends BaseEntity<?>> extends Abs
 | 
			
		||||
        return findPageWithTimeSearch(searchView, clauses, Collections.singletonList(ordering), pageLink);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    protected List<T> findPageWithTimeSearch(String searchView, List<Clause> clauses, List<Ordering> topLevelOrderings, TimePageLink pageLink) {
 | 
			
		||||
        return findPageWithTimeSearch(searchView, clauses, topLevelOrderings, pageLink, ModelConstants.ID_PROPERTY);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected List<T> findPageWithTimeSearch(String searchView, List<Clause> clauses, TimePageLink pageLink, String idColumn) {
 | 
			
		||||
        return findPageWithTimeSearch(searchView, clauses, Collections.emptyList(), pageLink, idColumn);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected List<T> findPageWithTimeSearch(String searchView, List<Clause> clauses, List<Ordering> topLevelOrderings, TimePageLink pageLink, String idColumn) {
 | 
			
		||||
        return findListByStatement(buildQuery(searchView, clauses, topLevelOrderings, pageLink, idColumn));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Where buildQuery(String searchView, List<Clause> clauses, TimePageLink pageLink, String idColumn) {
 | 
			
		||||
        return buildQuery(searchView, clauses, Collections.emptyList(), pageLink, idColumn);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Where buildQuery(String searchView, List<Clause> clauses, Ordering order, TimePageLink pageLink, String idColumn) {
 | 
			
		||||
        return buildQuery(searchView, clauses, Collections.singletonList(order), pageLink, idColumn);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static Where buildQuery(String searchView, List<Clause> clauses, List<Ordering> topLevelOrderings, TimePageLink pageLink, String idColumn) {
 | 
			
		||||
        Select select = select().from(searchView);
 | 
			
		||||
        Where query = select.where();
 | 
			
		||||
        for (Clause clause : clauses) {
 | 
			
		||||
@ -57,34 +76,35 @@ public abstract class AbstractSearchTimeDao<T extends BaseEntity<?>> extends Abs
 | 
			
		||||
        query.limit(pageLink.getLimit());
 | 
			
		||||
        if (pageLink.isAscOrder()) {
 | 
			
		||||
            if (pageLink.getIdOffset() != null) {
 | 
			
		||||
                query.and(QueryBuilder.gt(ModelConstants.ID_PROPERTY, pageLink.getIdOffset()));
 | 
			
		||||
                query.and(QueryBuilder.gt(idColumn, pageLink.getIdOffset()));
 | 
			
		||||
            } else if (pageLink.getStartTime() != null) {
 | 
			
		||||
                final UUID startOf = UUIDs.startOf(pageLink.getStartTime());
 | 
			
		||||
                query.and(QueryBuilder.gte(ModelConstants.ID_PROPERTY, startOf));
 | 
			
		||||
                query.and(QueryBuilder.gte(idColumn, startOf));
 | 
			
		||||
            }
 | 
			
		||||
            if (pageLink.getEndTime() != null) {
 | 
			
		||||
                final UUID endOf = UUIDs.endOf(pageLink.getEndTime());
 | 
			
		||||
                query.and(QueryBuilder.lte(ModelConstants.ID_PROPERTY, endOf));
 | 
			
		||||
                query.and(QueryBuilder.lte(idColumn, endOf));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            if (pageLink.getIdOffset() != null) {
 | 
			
		||||
                query.and(QueryBuilder.lt(ModelConstants.ID_PROPERTY, pageLink.getIdOffset()));
 | 
			
		||||
                query.and(QueryBuilder.lt(idColumn, pageLink.getIdOffset()));
 | 
			
		||||
            } else if (pageLink.getEndTime() != null) {
 | 
			
		||||
                final UUID endOf = UUIDs.endOf(pageLink.getEndTime());
 | 
			
		||||
                query.and(QueryBuilder.lte(ModelConstants.ID_PROPERTY, endOf));
 | 
			
		||||
                query.and(QueryBuilder.lte(idColumn, endOf));
 | 
			
		||||
            }
 | 
			
		||||
            if (pageLink.getStartTime() != null) {
 | 
			
		||||
                final UUID startOf = UUIDs.startOf(pageLink.getStartTime());
 | 
			
		||||
                query.and(QueryBuilder.gte(ModelConstants.ID_PROPERTY, startOf));
 | 
			
		||||
                query.and(QueryBuilder.gte(idColumn, startOf));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        List<Ordering> orderings = new ArrayList<>(topLevelOrderings);
 | 
			
		||||
        if (pageLink.isAscOrder()) {
 | 
			
		||||
            orderings.add(QueryBuilder.asc(ModelConstants.ID_PROPERTY));
 | 
			
		||||
            orderings.add(QueryBuilder.asc(idColumn));
 | 
			
		||||
        } else {
 | 
			
		||||
            orderings.add(QueryBuilder.desc(ModelConstants.ID_PROPERTY));
 | 
			
		||||
            orderings.add(QueryBuilder.desc(idColumn));
 | 
			
		||||
        }
 | 
			
		||||
        query.orderBy(orderings.toArray(new Ordering[orderings.size()]));
 | 
			
		||||
        return findListByStatement(query);
 | 
			
		||||
        return query;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,8 +15,27 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.alarm;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.dao.Dao;
 | 
			
		||||
import org.thingsboard.server.dao.model.AlarmEntity;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by ashvayka on 11.05.17.
 | 
			
		||||
 */
 | 
			
		||||
public interface AlarmDao {
 | 
			
		||||
public interface AlarmDao extends Dao<AlarmEntity> {
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findAlarmByIdAsync(UUID key);
 | 
			
		||||
 | 
			
		||||
    AlarmEntity save(Alarm alarm);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<List<Alarm>> findAlarms(AlarmQuery query);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,109 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.alarm;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
 | 
			
		||||
import com.datastax.driver.core.querybuilder.Select;
 | 
			
		||||
import com.google.common.util.concurrent.AsyncFunction;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.dao.AbstractModelDao;
 | 
			
		||||
import org.thingsboard.server.dao.AbstractSearchTimeDao;
 | 
			
		||||
import org.thingsboard.server.dao.model.AlarmEntity;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationDao;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
 | 
			
		||||
import static com.datastax.driver.core.querybuilder.QueryBuilder.select;
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.*;
 | 
			
		||||
 | 
			
		||||
@Component
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class AlarmDaoImpl extends AbstractModelDao<AlarmEntity> implements AlarmDao {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RelationDao relationDao;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected Class<AlarmEntity> getColumnFamilyClass() {
 | 
			
		||||
        return AlarmEntity.class;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected String getColumnFamilyName() {
 | 
			
		||||
        return ALARM_COLUMN_FAMILY_NAME;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected boolean isDeleteOnSave() {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public AlarmEntity save(Alarm alarm) {
 | 
			
		||||
        log.debug("Save asset [{}] ", alarm);
 | 
			
		||||
        return save(new AlarmEntity(alarm));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type) {
 | 
			
		||||
        Select select = select().from(ALARM_COLUMN_FAMILY_NAME);
 | 
			
		||||
        Select.Where query = select.where();
 | 
			
		||||
        query.and(eq(ALARM_TENANT_ID_PROPERTY, tenantId.getId()));
 | 
			
		||||
        query.and(eq(ALARM_ORIGINATOR_ID_PROPERTY, originator.getId()));
 | 
			
		||||
        query.and(eq(ALARM_ORIGINATOR_TYPE_PROPERTY, originator.getEntityType()));
 | 
			
		||||
        query.and(eq(ALARM_TYPE_PROPERTY, type));
 | 
			
		||||
        query.limit(1);
 | 
			
		||||
        query.orderBy(QueryBuilder.asc(ModelConstants.ALARM_TYPE_PROPERTY), QueryBuilder.desc(ModelConstants.ID_PROPERTY));
 | 
			
		||||
        return Futures.transform(findOneByStatementAsync(query), toDataFunction());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Alarm> findAlarmByIdAsync(UUID key) {
 | 
			
		||||
        log.debug("Get alarm by id {}", key);
 | 
			
		||||
        Select.Where query = select().from(ALARM_BY_ID_VIEW_NAME).where(eq(ModelConstants.ID_PROPERTY, key));
 | 
			
		||||
        query.limit(1);
 | 
			
		||||
        log.trace("Execute query {}", query);
 | 
			
		||||
        return Futures.transform(findOneByStatementAsync(query), toDataFunction());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<List<Alarm>> findAlarms(AlarmQuery query) {
 | 
			
		||||
        log.trace("Try to find alarms by entity [{}], status [{}] and pageLink [{}]", query.getAffectedEntityId(), query.getStatus(), query.getPageLink());
 | 
			
		||||
        EntityId affectedEntity = query.getAffectedEntityId();
 | 
			
		||||
        String relationType = query.getStatus() == null ? BaseAlarmService.ALARM_RELATION : BaseAlarmService.ALARM_RELATION_PREFIX + query.getStatus().name();
 | 
			
		||||
        ListenableFuture<List<EntityRelation>> relations = relationDao.findRelations(affectedEntity, relationType, EntityType.ALARM, query.getPageLink());
 | 
			
		||||
        return Futures.transform(relations, (AsyncFunction<List<EntityRelation>, List<Alarm>>) input -> {
 | 
			
		||||
            List<ListenableFuture<Alarm>> alarmFutures = new ArrayList<>(input.size());
 | 
			
		||||
            for (EntityRelation relation : input) {
 | 
			
		||||
                alarmFutures.add(findAlarmByIdAsync(relation.getTo().getId()));
 | 
			
		||||
            }
 | 
			
		||||
            return Futures.successfulAsList(alarmFutures);
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -21,25 +21,19 @@ import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageData;
 | 
			
		||||
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by ashvayka on 11.05.17.
 | 
			
		||||
 */
 | 
			
		||||
public interface AlarmService {
 | 
			
		||||
 | 
			
		||||
    Alarm findAlarmById(AlarmId alarmId);
 | 
			
		||||
    Alarm createOrUpdateAlarm(Alarm alarm);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> ackAlarm(AlarmId alarmId, long ackTs);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> clearAlarm(AlarmId alarmId, long ackTs);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findAlarmByIdAsync(AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    Optional<Alarm> saveIfNotExists(Alarm alarm);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> updateAlarm(Alarm alarm);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> ackAlarm(Alarm alarm);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> clearAlarm(AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<TimePageData<Alarm>> findAlarms(AlarmQuery query);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -15,52 +15,277 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.alarm;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Function;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.util.StringUtils;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageData;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.dao.entity.AbstractEntityService;
 | 
			
		||||
import org.thingsboard.server.dao.entity.BaseEntityService;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.model.*;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntityRelationsQuery;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntitySearchDirection;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationService;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationsSearchParameters;
 | 
			
		||||
import org.thingsboard.server.dao.service.DataValidator;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantDao;
 | 
			
		||||
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import javax.annotation.PostConstruct;
 | 
			
		||||
import javax.annotation.PreDestroy;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
import java.util.concurrent.ExecutorService;
 | 
			
		||||
import java.util.concurrent.Executors;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.server.dao.DaoUtil.*;
 | 
			
		||||
import static org.thingsboard.server.dao.service.Validator.*;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class BaseAlarmService implements AlarmService {
 | 
			
		||||
public class BaseAlarmService extends AbstractEntityService implements AlarmService {
 | 
			
		||||
 | 
			
		||||
    public static final String ALARM_RELATION_PREFIX = "ALARM_";
 | 
			
		||||
    public static final String ALARM_RELATION = "ALARM_ANY";
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private AlarmDao alarmDao;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private TenantDao tenantDao;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private RelationService relationService;
 | 
			
		||||
 | 
			
		||||
    protected ExecutorService readResultsProcessingExecutor;
 | 
			
		||||
 | 
			
		||||
    @PostConstruct
 | 
			
		||||
    public void startExecutor() {
 | 
			
		||||
        readResultsProcessingExecutor = Executors.newCachedThreadPool();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @PreDestroy
 | 
			
		||||
    public void stopExecutor() {
 | 
			
		||||
        if (readResultsProcessingExecutor != null) {
 | 
			
		||||
            readResultsProcessingExecutor.shutdownNow();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Alarm findAlarmById(AlarmId alarmId) {
 | 
			
		||||
        return null;
 | 
			
		||||
    public Alarm createOrUpdateAlarm(Alarm alarm) {
 | 
			
		||||
        alarmDataValidator.validate(alarm);
 | 
			
		||||
        try {
 | 
			
		||||
            if (alarm.getStartTs() == 0L) {
 | 
			
		||||
                alarm.setStartTs(System.currentTimeMillis());
 | 
			
		||||
            }
 | 
			
		||||
            if (alarm.getEndTs() == 0L) {
 | 
			
		||||
                alarm.setEndTs(alarm.getStartTs());
 | 
			
		||||
            }
 | 
			
		||||
            if (alarm.getId() == null) {
 | 
			
		||||
                Alarm existing = alarmDao.findLatestByOriginatorAndType(alarm.getTenantId(), alarm.getOriginator(), alarm.getType()).get();
 | 
			
		||||
                if (existing == null || existing.getStatus().isCleared()) {
 | 
			
		||||
                    return createAlarm(alarm);
 | 
			
		||||
                } else {
 | 
			
		||||
                    return updateAlarm(existing, alarm);
 | 
			
		||||
                }
 | 
			
		||||
            } else {
 | 
			
		||||
                return updateAlarm(alarm).get();
 | 
			
		||||
            }
 | 
			
		||||
        } catch (ExecutionException | InterruptedException e) {
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Alarm createAlarm(Alarm alarm) throws InterruptedException, ExecutionException {
 | 
			
		||||
        log.debug("New Alarm : {}", alarm);
 | 
			
		||||
        Alarm saved = getData(alarmDao.save(new AlarmEntity(alarm)));
 | 
			
		||||
        EntityRelationsQuery query = new EntityRelationsQuery();
 | 
			
		||||
        query.setParameters(new RelationsSearchParameters(saved.getOriginator(), EntitySearchDirection.TO, Integer.MAX_VALUE));
 | 
			
		||||
        List<EntityId> parentEntities = relationService.findByQuery(query).get().stream().map(r -> r.getFrom()).collect(Collectors.toList());
 | 
			
		||||
        for (EntityId parentId : parentEntities) {
 | 
			
		||||
            createRelation(new EntityRelation(parentId, saved.getId(), ALARM_RELATION));
 | 
			
		||||
            createRelation(new EntityRelation(parentId, saved.getId(), ALARM_RELATION_PREFIX + saved.getStatus().name()));
 | 
			
		||||
        }
 | 
			
		||||
        createRelation(new EntityRelation(alarm.getOriginator(), saved.getId(), ALARM_RELATION));
 | 
			
		||||
        createRelation(new EntityRelation(alarm.getOriginator(), saved.getId(), ALARM_RELATION_PREFIX + saved.getStatus().name()));
 | 
			
		||||
        return saved;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected ListenableFuture<Alarm> updateAlarm(Alarm update) {
 | 
			
		||||
        alarmDataValidator.validate(update);
 | 
			
		||||
        return getAndUpdate(update.getId(), new Function<Alarm, Alarm>() {
 | 
			
		||||
            @Nullable
 | 
			
		||||
            @Override
 | 
			
		||||
            public Alarm apply(@Nullable Alarm alarm) {
 | 
			
		||||
                if (alarm == null) {
 | 
			
		||||
                    return null;
 | 
			
		||||
                } else {
 | 
			
		||||
                    return updateAlarm(alarm, update);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Alarm updateAlarm(Alarm oldAlarm, Alarm newAlarm) {
 | 
			
		||||
        AlarmStatus oldStatus = oldAlarm.getStatus();
 | 
			
		||||
        AlarmStatus newStatus = newAlarm.getStatus();
 | 
			
		||||
        AlarmEntity result = alarmDao.save(new AlarmEntity(merge(oldAlarm, newAlarm)));
 | 
			
		||||
        if (oldStatus != newStatus) {
 | 
			
		||||
            updateRelations(oldAlarm, oldStatus, newStatus);
 | 
			
		||||
        }
 | 
			
		||||
        return result.toData();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Boolean> ackAlarm(AlarmId alarmId, long ackTime) {
 | 
			
		||||
        return getAndUpdate(alarmId, new Function<Alarm, Boolean>() {
 | 
			
		||||
            @Nullable
 | 
			
		||||
            @Override
 | 
			
		||||
            public Boolean apply(@Nullable Alarm alarm) {
 | 
			
		||||
                if (alarm == null || alarm.getStatus().isAck()) {
 | 
			
		||||
                    return false;
 | 
			
		||||
                } else {
 | 
			
		||||
                    AlarmStatus oldStatus = alarm.getStatus();
 | 
			
		||||
                    AlarmStatus newStatus = oldStatus.isCleared() ? AlarmStatus.CLEARED_ACK : AlarmStatus.ACTIVE_ACK;
 | 
			
		||||
                    alarm.setStatus(newStatus);
 | 
			
		||||
                    alarm.setAckTs(ackTime);
 | 
			
		||||
                    alarmDao.save(new AlarmEntity(alarm));
 | 
			
		||||
                    updateRelations(alarm, oldStatus, newStatus);
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Boolean> clearAlarm(AlarmId alarmId, long clearTime) {
 | 
			
		||||
        return getAndUpdate(alarmId, new Function<Alarm, Boolean>() {
 | 
			
		||||
            @Nullable
 | 
			
		||||
            @Override
 | 
			
		||||
            public Boolean apply(@Nullable Alarm alarm) {
 | 
			
		||||
                if (alarm == null || alarm.getStatus().isCleared()) {
 | 
			
		||||
                    return false;
 | 
			
		||||
                } else {
 | 
			
		||||
                    AlarmStatus oldStatus = alarm.getStatus();
 | 
			
		||||
                    AlarmStatus newStatus = oldStatus.isAck() ? AlarmStatus.CLEARED_ACK : AlarmStatus.CLEARED_UNACK;
 | 
			
		||||
                    alarm.setStatus(newStatus);
 | 
			
		||||
                    alarm.setClearTs(clearTime);
 | 
			
		||||
                    alarmDao.save(new AlarmEntity(alarm));
 | 
			
		||||
                    updateRelations(alarm, oldStatus, newStatus);
 | 
			
		||||
                    return true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Alarm> findAlarmByIdAsync(AlarmId alarmId) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Optional<Alarm> saveIfNotExists(Alarm alarm) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Boolean> updateAlarm(Alarm alarm) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Boolean> ackAlarm(Alarm alarm) {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Boolean> clearAlarm(AlarmId alarmId) {
 | 
			
		||||
        return null;
 | 
			
		||||
        log.trace("Executing findAlarmById [{}]", alarmId);
 | 
			
		||||
        validateId(alarmId, "Incorrect alarmId " + alarmId);
 | 
			
		||||
        return alarmDao.findAlarmByIdAsync(alarmId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<TimePageData<Alarm>> findAlarms(AlarmQuery query) {
 | 
			
		||||
        return null;
 | 
			
		||||
        ListenableFuture<List<Alarm>> alarms = alarmDao.findAlarms(query);
 | 
			
		||||
        return Futures.transform(alarms, new Function<List<Alarm>, TimePageData<Alarm>>() {
 | 
			
		||||
            @Nullable
 | 
			
		||||
            @Override
 | 
			
		||||
            public TimePageData<Alarm> apply(@Nullable List<Alarm> alarms) {
 | 
			
		||||
                return new TimePageData<>(alarms, query.getPageLink());
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void deleteRelation(EntityRelation alarmRelation) throws ExecutionException, InterruptedException {
 | 
			
		||||
        log.debug("Deleting Alarm relation: {}", alarmRelation);
 | 
			
		||||
        relationService.deleteRelation(alarmRelation).get();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void createRelation(EntityRelation alarmRelation) throws ExecutionException, InterruptedException {
 | 
			
		||||
        log.debug("Creating Alarm relation: {}", alarmRelation);
 | 
			
		||||
        relationService.saveRelation(alarmRelation).get();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Alarm merge(Alarm existing, Alarm alarm) {
 | 
			
		||||
        if (alarm.getStartTs() > existing.getEndTs()) {
 | 
			
		||||
            existing.setEndTs(alarm.getStartTs());
 | 
			
		||||
        }
 | 
			
		||||
        if (alarm.getEndTs() > existing.getEndTs()) {
 | 
			
		||||
            existing.setEndTs(alarm.getEndTs());
 | 
			
		||||
        }
 | 
			
		||||
        if (alarm.getClearTs() > existing.getClearTs()) {
 | 
			
		||||
            existing.setClearTs(alarm.getClearTs());
 | 
			
		||||
        }
 | 
			
		||||
        if (alarm.getAckTs() > existing.getAckTs()) {
 | 
			
		||||
            existing.setAckTs(alarm.getAckTs());
 | 
			
		||||
        }
 | 
			
		||||
        existing.setStatus(alarm.getStatus());
 | 
			
		||||
        existing.setSeverity(alarm.getSeverity());
 | 
			
		||||
        existing.setDetails(alarm.getDetails());
 | 
			
		||||
        return existing;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void updateRelations(Alarm alarm, AlarmStatus oldStatus, AlarmStatus newStatus) {
 | 
			
		||||
        try {
 | 
			
		||||
            EntityRelationsQuery query = new EntityRelationsQuery();
 | 
			
		||||
            query.setParameters(new RelationsSearchParameters(alarm.getOriginator(), EntitySearchDirection.TO, Integer.MAX_VALUE));
 | 
			
		||||
            List<EntityId> parentEntities = relationService.findByQuery(query).get().stream().map(r -> r.getFrom()).collect(Collectors.toList());
 | 
			
		||||
            for (EntityId parentId : parentEntities) {
 | 
			
		||||
                deleteRelation(new EntityRelation(parentId, alarm.getId(), ALARM_RELATION_PREFIX + oldStatus.name()));
 | 
			
		||||
                createRelation(new EntityRelation(parentId, alarm.getId(), ALARM_RELATION_PREFIX + newStatus.name()));
 | 
			
		||||
            }
 | 
			
		||||
            deleteRelation(new EntityRelation(alarm.getOriginator(), alarm.getId(), ALARM_RELATION_PREFIX + oldStatus.name()));
 | 
			
		||||
            createRelation(new EntityRelation(alarm.getOriginator(), alarm.getId(), ALARM_RELATION_PREFIX + newStatus.name()));
 | 
			
		||||
        } catch (ExecutionException | InterruptedException e) {
 | 
			
		||||
            log.warn("[{}] Failed to update relations. Old status: [{}], New status: [{}]", alarm.getId(), oldStatus, newStatus);
 | 
			
		||||
            throw new RuntimeException(e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private <T> ListenableFuture<T> getAndUpdate(AlarmId alarmId, Function<Alarm, T> function) {
 | 
			
		||||
        validateId(alarmId, "Alarm id should be specified!");
 | 
			
		||||
        ListenableFuture<Alarm> entity = alarmDao.findAlarmByIdAsync(alarmId.getId());
 | 
			
		||||
        return Futures.transform(entity, function, readResultsProcessingExecutor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private DataValidator<Alarm> alarmDataValidator =
 | 
			
		||||
            new DataValidator<Alarm>() {
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                protected void validateDataImpl(Alarm alarm) {
 | 
			
		||||
                    if (StringUtils.isEmpty(alarm.getType())) {
 | 
			
		||||
                        throw new DataValidationException("Alarm type should be specified!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (alarm.getOriginator() == null) {
 | 
			
		||||
                        throw new DataValidationException("Alarm originator should be specified!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (alarm.getSeverity() == null) {
 | 
			
		||||
                        throw new DataValidationException("Alarm severity should be specified!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (alarm.getStatus() == null) {
 | 
			
		||||
                        throw new DataValidationException("Alarm status should be specified!");
 | 
			
		||||
                    }
 | 
			
		||||
                    if (alarm.getTenantId() == null) {
 | 
			
		||||
                        throw new DataValidationException("Alarm should be assigned to tenant!");
 | 
			
		||||
                    } else {
 | 
			
		||||
                        TenantEntity tenant = tenantDao.findById(alarm.getTenantId().getId());
 | 
			
		||||
                        if (tenant == null) {
 | 
			
		||||
                            throw new DataValidationException("Alarm is referencing to non-existent tenant!");
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,236 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.model;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.utils.UUIDs;
 | 
			
		||||
import com.datastax.driver.mapping.annotations.*;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityIdFactory;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.dao.model.type.AlarmSeverityCodec;
 | 
			
		||||
import org.thingsboard.server.dao.model.type.AlarmStatusCodec;
 | 
			
		||||
import org.thingsboard.server.dao.model.type.EntityTypeCodec;
 | 
			
		||||
import org.thingsboard.server.dao.model.type.JsonCodec;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.*;
 | 
			
		||||
 | 
			
		||||
@Table(name = ALARM_COLUMN_FAMILY_NAME)
 | 
			
		||||
public final class AlarmEntity implements BaseEntity<Alarm> {
 | 
			
		||||
 | 
			
		||||
    @Transient
 | 
			
		||||
    private static final long serialVersionUID = -1265181166886910152L;
 | 
			
		||||
 | 
			
		||||
    @ClusteringColumn(value = 1)
 | 
			
		||||
    @Column(name = ID_PROPERTY)
 | 
			
		||||
    private UUID id;
 | 
			
		||||
 | 
			
		||||
    @PartitionKey(value = 0)
 | 
			
		||||
    @Column(name = ALARM_TENANT_ID_PROPERTY)
 | 
			
		||||
    private UUID tenantId;
 | 
			
		||||
 | 
			
		||||
    @PartitionKey(value = 1)
 | 
			
		||||
    @Column(name = ALARM_ORIGINATOR_ID_PROPERTY)
 | 
			
		||||
    private UUID originatorId;
 | 
			
		||||
 | 
			
		||||
    @PartitionKey(value = 2)
 | 
			
		||||
    @Column(name = ALARM_ORIGINATOR_TYPE_PROPERTY, codec = EntityTypeCodec.class)
 | 
			
		||||
    private EntityType originatorType;
 | 
			
		||||
 | 
			
		||||
    @ClusteringColumn(value = 0)
 | 
			
		||||
    @Column(name = ALARM_TYPE_PROPERTY)
 | 
			
		||||
    private String type;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_SEVERITY_PROPERTY, codec = AlarmSeverityCodec.class)
 | 
			
		||||
    private AlarmSeverity severity;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_STATUS_PROPERTY, codec = AlarmStatusCodec.class)
 | 
			
		||||
    private AlarmStatus status;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_START_TS_PROPERTY)
 | 
			
		||||
    private Long startTs;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_END_TS_PROPERTY)
 | 
			
		||||
    private Long endTs;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_ACK_TS_PROPERTY)
 | 
			
		||||
    private Long ackTs;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_CLEAR_TS_PROPERTY)
 | 
			
		||||
    private Long clearTs;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_DETAILS_PROPERTY, codec = JsonCodec.class)
 | 
			
		||||
    private JsonNode details;
 | 
			
		||||
 | 
			
		||||
    @Column(name = ALARM_PROPAGATE_PROPERTY)
 | 
			
		||||
    private Boolean propagate;
 | 
			
		||||
 | 
			
		||||
    public AlarmEntity() {
 | 
			
		||||
        super();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AlarmEntity(Alarm alarm) {
 | 
			
		||||
        if (alarm.getId() != null) {
 | 
			
		||||
            this.id = alarm.getId().getId();
 | 
			
		||||
        }
 | 
			
		||||
        if (alarm.getTenantId() != null) {
 | 
			
		||||
            this.tenantId = alarm.getTenantId().getId();
 | 
			
		||||
        }
 | 
			
		||||
        this.type = alarm.getType();
 | 
			
		||||
        this.originatorId = alarm.getOriginator().getId();
 | 
			
		||||
        this.originatorType = alarm.getOriginator().getEntityType();
 | 
			
		||||
        this.type = alarm.getType();
 | 
			
		||||
        this.severity = alarm.getSeverity();
 | 
			
		||||
        this.status = alarm.getStatus();
 | 
			
		||||
        this.propagate = alarm.isPropagate();
 | 
			
		||||
        this.startTs = alarm.getStartTs();
 | 
			
		||||
        this.endTs = alarm.getEndTs();
 | 
			
		||||
        this.ackTs = alarm.getAckTs();
 | 
			
		||||
        this.clearTs = alarm.getClearTs();
 | 
			
		||||
        this.details = alarm.getDetails();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UUID getId() {
 | 
			
		||||
        return id;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setId(UUID id) {
 | 
			
		||||
        this.id = id;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UUID getTenantId() {
 | 
			
		||||
        return tenantId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setTenantId(UUID tenantId) {
 | 
			
		||||
        this.tenantId = tenantId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public UUID getOriginatorId() {
 | 
			
		||||
        return originatorId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setOriginatorId(UUID originatorId) {
 | 
			
		||||
        this.originatorId = originatorId;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public EntityType getOriginatorType() {
 | 
			
		||||
        return originatorType;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setOriginatorType(EntityType originatorType) {
 | 
			
		||||
        this.originatorType = originatorType;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public String getType() {
 | 
			
		||||
        return type;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setType(String type) {
 | 
			
		||||
        this.type = type;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AlarmSeverity getSeverity() {
 | 
			
		||||
        return severity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setSeverity(AlarmSeverity severity) {
 | 
			
		||||
        this.severity = severity;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AlarmStatus getStatus() {
 | 
			
		||||
        return status;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setStatus(AlarmStatus status) {
 | 
			
		||||
        this.status = status;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Long getStartTs() {
 | 
			
		||||
        return startTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setStartTs(Long startTs) {
 | 
			
		||||
        this.startTs = startTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Long getEndTs() {
 | 
			
		||||
        return endTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setEndTs(Long endTs) {
 | 
			
		||||
        this.endTs = endTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Long getAckTs() {
 | 
			
		||||
        return ackTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setAckTs(Long ackTs) {
 | 
			
		||||
        this.ackTs = ackTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Long getClearTs() {
 | 
			
		||||
        return clearTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setClearTs(Long clearTs) {
 | 
			
		||||
        this.clearTs = clearTs;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public JsonNode getDetails() {
 | 
			
		||||
        return details;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setDetails(JsonNode details) {
 | 
			
		||||
        this.details = details;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public Boolean getPropagate() {
 | 
			
		||||
        return propagate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPropagate(Boolean propagate) {
 | 
			
		||||
        this.propagate = propagate;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Alarm toData() {
 | 
			
		||||
        Alarm alarm = new Alarm(new AlarmId(id));
 | 
			
		||||
        alarm.setCreatedTime(UUIDs.unixTimestamp(id));
 | 
			
		||||
        if (tenantId != null) {
 | 
			
		||||
            alarm.setTenantId(new TenantId(tenantId));
 | 
			
		||||
        }
 | 
			
		||||
        alarm.setOriginator(EntityIdFactory.getByTypeAndUuid(originatorType, originatorId));
 | 
			
		||||
        alarm.setType(type);
 | 
			
		||||
        alarm.setSeverity(severity);
 | 
			
		||||
        alarm.setStatus(status);
 | 
			
		||||
        alarm.setPropagate(propagate);
 | 
			
		||||
        alarm.setStartTs(startTs);
 | 
			
		||||
        alarm.setEndTs(endTs);
 | 
			
		||||
        alarm.setAckTs(ackTs);
 | 
			
		||||
        alarm.setClearTs(clearTs);
 | 
			
		||||
        alarm.setDetails(details);
 | 
			
		||||
        return alarm;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -147,6 +147,25 @@ public class ModelConstants {
 | 
			
		||||
    public static final String ASSET_BY_TENANT_AND_NAME_VIEW_NAME = "asset_by_tenant_and_name";
 | 
			
		||||
    public static final String ASSET_TYPES_BY_TENANT_VIEW_NAME = "asset_types_by_tenant";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Cassandra alarm constants.
 | 
			
		||||
     */
 | 
			
		||||
    public static final String ALARM_COLUMN_FAMILY_NAME = "alarm";
 | 
			
		||||
    public static final String ALARM_TENANT_ID_PROPERTY = TENTANT_ID_PROPERTY;
 | 
			
		||||
    public static final String ALARM_TYPE_PROPERTY = "type";
 | 
			
		||||
    public static final String ALARM_DETAILS_PROPERTY = "details";
 | 
			
		||||
    public static final String ALARM_ORIGINATOR_ID_PROPERTY = "originator_id";
 | 
			
		||||
    public static final String ALARM_ORIGINATOR_TYPE_PROPERTY = "originator_type";
 | 
			
		||||
    public static final String ALARM_SEVERITY_PROPERTY = "severity";
 | 
			
		||||
    public static final String ALARM_STATUS_PROPERTY = "status";
 | 
			
		||||
    public static final String ALARM_START_TS_PROPERTY = "start_ts";
 | 
			
		||||
    public static final String ALARM_END_TS_PROPERTY = "end_ts";
 | 
			
		||||
    public static final String ALARM_ACK_TS_PROPERTY = "ack_ts";
 | 
			
		||||
    public static final String ALARM_CLEAR_TS_PROPERTY = "clear_ts";
 | 
			
		||||
    public static final String ALARM_PROPAGATE_PROPERTY = "propagate";
 | 
			
		||||
 | 
			
		||||
    public static final String ALARM_BY_ID_VIEW_NAME = "alarm_by_id";
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Cassandra entity relation constants.
 | 
			
		||||
     */
 | 
			
		||||
@ -157,6 +176,7 @@ public class ModelConstants {
 | 
			
		||||
    public static final String RELATION_TO_TYPE_PROPERTY = "to_type";
 | 
			
		||||
    public static final String RELATION_TYPE_PROPERTY = "relation_type";
 | 
			
		||||
 | 
			
		||||
    public static final String RELATION_BY_TYPE_AND_CHILD_TYPE_VIEW_NAME = "relation_by_type_and_child_type";
 | 
			
		||||
    public static final String RELATION_REVERSE_VIEW_NAME = "reverse_relation";
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,29 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.model.type;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
import org.thingsboard.server.dao.alarm.AlarmService;
 | 
			
		||||
 | 
			
		||||
public class AlarmSeverityCodec extends EnumNameCodec<AlarmSeverity> {
 | 
			
		||||
 | 
			
		||||
    public AlarmSeverityCodec() {
 | 
			
		||||
        super(AlarmSeverity.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,27 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.model.type;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.extras.codecs.enums.EnumNameCodec;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
 | 
			
		||||
public class AlarmStatusCodec extends EnumNameCodec<AlarmStatus> {
 | 
			
		||||
 | 
			
		||||
    public AlarmStatusCodec() {
 | 
			
		||||
        super(AlarmStatus.class);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -16,23 +16,32 @@
 | 
			
		||||
package org.thingsboard.server.dao.relation;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.*;
 | 
			
		||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
 | 
			
		||||
import com.datastax.driver.core.querybuilder.Select;
 | 
			
		||||
import com.fasterxml.jackson.databind.JsonNode;
 | 
			
		||||
import com.google.common.base.Function;
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.stereotype.Component;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityIdFactory;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.dao.AbstractAsyncDao;
 | 
			
		||||
import org.thingsboard.server.dao.AbstractSearchTimeDao;
 | 
			
		||||
import org.thingsboard.server.dao.model.ModelConstants;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import javax.annotation.PostConstruct;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
import static com.datastax.driver.core.querybuilder.QueryBuilder.eq;
 | 
			
		||||
import static org.thingsboard.server.dao.model.ModelConstants.RELATION_COLUMN_FAMILY_NAME;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Created by ashvayka on 25.04.17.
 | 
			
		||||
 */
 | 
			
		||||
@ -145,6 +154,18 @@ public class BaseRelationDao extends AbstractAsyncDao implements RelationDao {
 | 
			
		||||
        return getBooleanListenableFuture(future);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<List<EntityRelation>> findRelations(EntityId from, String relationType, EntityType childType, TimePageLink pageLink) {
 | 
			
		||||
        Select.Where query = AbstractSearchTimeDao.buildQuery(ModelConstants.RELATION_BY_TYPE_AND_CHILD_TYPE_VIEW_NAME,
 | 
			
		||||
                Arrays.asList(eq(ModelConstants.RELATION_FROM_ID_PROPERTY, from.getId()),
 | 
			
		||||
                        eq(ModelConstants.RELATION_FROM_TYPE_PROPERTY, from.getEntityType().name()),
 | 
			
		||||
                        eq(ModelConstants.RELATION_TYPE_PROPERTY, relationType),
 | 
			
		||||
                        eq(ModelConstants.RELATION_TO_TYPE_PROPERTY, childType.name())),
 | 
			
		||||
                Arrays.asList(QueryBuilder.asc(ModelConstants.RELATION_TYPE_PROPERTY), QueryBuilder.asc(ModelConstants.RELATION_TO_TYPE_PROPERTY)),
 | 
			
		||||
                pageLink, ModelConstants.RELATION_TO_ID_PROPERTY);
 | 
			
		||||
        return getFuture(executeAsyncRead(query), rs -> getEntityRelations(rs));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private PreparedStatement getSaveStmt() {
 | 
			
		||||
        if (saveStmt == null) {
 | 
			
		||||
            saveStmt = getSession().prepare("INSERT INTO " + ModelConstants.RELATION_COLUMN_FAMILY_NAME + " " +
 | 
			
		||||
@ -235,31 +256,13 @@ public class BaseRelationDao extends AbstractAsyncDao implements RelationDao {
 | 
			
		||||
        return checkRelationStmt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private EntityRelation getEntityRelation(Row row) {
 | 
			
		||||
        EntityRelation relation = new EntityRelation();
 | 
			
		||||
        relation.setType(row.getString(ModelConstants.RELATION_TYPE_PROPERTY));
 | 
			
		||||
        relation.setAdditionalInfo(row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY, JsonNode.class));
 | 
			
		||||
        relation.setFrom(toEntity(row, ModelConstants.RELATION_FROM_ID_PROPERTY, ModelConstants.RELATION_FROM_TYPE_PROPERTY));
 | 
			
		||||
        relation.setTo(toEntity(row, ModelConstants.RELATION_TO_ID_PROPERTY, ModelConstants.RELATION_TO_TYPE_PROPERTY));
 | 
			
		||||
        return relation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private EntityId toEntity(Row row, String uuidColumn, String typeColumn) {
 | 
			
		||||
        return EntityIdFactory.getByTypeAndUuid(row.getString(typeColumn), row.getUUID(uuidColumn));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ListenableFuture<List<EntityRelation>> executeAsyncRead(EntityId from, BoundStatement stmt) {
 | 
			
		||||
        log.debug("Generated query [{}] for entity {}", stmt, from);
 | 
			
		||||
        return getFuture(executeAsyncRead(stmt), rs -> {
 | 
			
		||||
            List<Row> rows = rs.all();
 | 
			
		||||
            List<EntityRelation> entries = new ArrayList<>(rows.size());
 | 
			
		||||
            if (!rows.isEmpty()) {
 | 
			
		||||
                rows.forEach(row -> {
 | 
			
		||||
                    entries.add(getEntityRelation(row));
 | 
			
		||||
                });
 | 
			
		||||
            }
 | 
			
		||||
            return entries;
 | 
			
		||||
        });
 | 
			
		||||
        return getFuture(executeAsyncRead(stmt), rs -> getEntityRelations(rs));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ListenableFuture<Boolean> getBooleanListenableFuture(ResultSetFuture rsFuture) {
 | 
			
		||||
@ -276,4 +279,24 @@ public class BaseRelationDao extends AbstractAsyncDao implements RelationDao {
 | 
			
		||||
        }, readResultsProcessingExecutor);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private List<EntityRelation> getEntityRelations(ResultSet rs) {
 | 
			
		||||
        List<Row> rows = rs.all();
 | 
			
		||||
        List<EntityRelation> entries = new ArrayList<>(rows.size());
 | 
			
		||||
        if (!rows.isEmpty()) {
 | 
			
		||||
            rows.forEach(row -> {
 | 
			
		||||
                entries.add(getEntityRelation(row));
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        return entries;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private EntityRelation getEntityRelation(Row row) {
 | 
			
		||||
        EntityRelation relation = new EntityRelation();
 | 
			
		||||
        relation.setType(row.getString(ModelConstants.RELATION_TYPE_PROPERTY));
 | 
			
		||||
        relation.setAdditionalInfo(row.get(ModelConstants.ADDITIONAL_INFO_PROPERTY, JsonNode.class));
 | 
			
		||||
        relation.setFrom(toEntity(row, ModelConstants.RELATION_FROM_ID_PROPERTY, ModelConstants.RELATION_FROM_TYPE_PROPERTY));
 | 
			
		||||
        relation.setTo(toEntity(row, ModelConstants.RELATION_TO_ID_PROPERTY, ModelConstants.RELATION_TO_TYPE_PROPERTY));
 | 
			
		||||
        return relation;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -171,8 +171,7 @@ public class BaseRelationService implements RelationService {
 | 
			
		||||
        RelationsSearchParameters params = query.getParameters();
 | 
			
		||||
        final List<EntityTypeFilter> filters = query.getFilters();
 | 
			
		||||
        if (filters == null || filters.isEmpty()) {
 | 
			
		||||
            log.warn("Failed to query relations. Filters are not set [{}]", query);
 | 
			
		||||
            throw new RuntimeException("Filters are not set!");
 | 
			
		||||
            log.debug("Filters are not set [{}]", query);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int maxLvl = params.getMaxLevel() > 0 ? params.getMaxLevel() : Integer.MAX_VALUE;
 | 
			
		||||
@ -182,10 +181,14 @@ public class BaseRelationService implements RelationService {
 | 
			
		||||
            return Futures.transform(relationSet, (Function<Set<EntityRelation>, List<EntityRelation>>) input -> {
 | 
			
		||||
                List<EntityRelation> relations = new ArrayList<>();
 | 
			
		||||
                for (EntityRelation relation : input) {
 | 
			
		||||
                    for (EntityTypeFilter filter : filters) {
 | 
			
		||||
                        if (match(filter, relation, params.getDirection())) {
 | 
			
		||||
                            relations.add(relation);
 | 
			
		||||
                            break;
 | 
			
		||||
                    if (filters == null || filters.isEmpty()) {
 | 
			
		||||
                        relations.add(relation);
 | 
			
		||||
                    } else {
 | 
			
		||||
                        for (EntityTypeFilter filter : filters) {
 | 
			
		||||
                            if (match(filter, relation, params.getDirection())) {
 | 
			
		||||
                                relations.add(relation);
 | 
			
		||||
                                break;
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
@ -254,7 +257,8 @@ public class BaseRelationService implements RelationService {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ListenableFuture<Set<EntityRelation>> findRelationsRecursively(final EntityId rootId, final EntitySearchDirection direction, int lvl, final ConcurrentHashMap<EntityId, Boolean> uniqueMap) throws Exception {
 | 
			
		||||
    private ListenableFuture<Set<EntityRelation>> findRelationsRecursively(final EntityId rootId, final EntitySearchDirection direction, int lvl,
 | 
			
		||||
                                                                           final ConcurrentHashMap<EntityId, Boolean> uniqueMap) throws Exception {
 | 
			
		||||
        if (lvl == 0) {
 | 
			
		||||
            return Futures.immediateFuture(Collections.emptySet());
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,9 @@
 | 
			
		||||
package org.thingsboard.server.dao.relation;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
@ -44,4 +46,6 @@ public interface RelationDao {
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Boolean> deleteOutboundRelations(EntityId entity);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<List<EntityRelation>> findRelations(EntityId from, String relationType, EntityType toType, TimePageLink pageLink);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -49,4 +49,7 @@ public interface RelationService {
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<List<EntityRelation>> findByQuery(EntityRelationsQuery query);
 | 
			
		||||
 | 
			
		||||
//    TODO: This method may be useful for some validations in the future
 | 
			
		||||
//    ListenableFuture<Boolean> checkRecursiveRelation(EntityId from, EntityId to);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -277,6 +277,31 @@ CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.asset_types_by_tenant AS
 | 
			
		||||
    PRIMARY KEY ( (type, tenant_id), id, customer_id)
 | 
			
		||||
    WITH CLUSTERING ORDER BY ( id ASC, customer_id DESC);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS thingsboard.alarm (
 | 
			
		||||
	id timeuuid,
 | 
			
		||||
	tenant_id timeuuid,
 | 
			
		||||
	type text,
 | 
			
		||||
	originator_id timeuuid,
 | 
			
		||||
	originator_type text,
 | 
			
		||||
    severity text,
 | 
			
		||||
    status text,
 | 
			
		||||
	start_ts bigint,
 | 
			
		||||
	end_ts bigint,
 | 
			
		||||
	ack_ts bigint,
 | 
			
		||||
	clear_ts bigint,
 | 
			
		||||
	details text,
 | 
			
		||||
	propagate boolean,
 | 
			
		||||
	PRIMARY KEY ((tenant_id, originator_id, originator_type), type, id)
 | 
			
		||||
) WITH CLUSTERING ORDER BY ( type ASC, id DESC);
 | 
			
		||||
 | 
			
		||||
CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.alarm_by_id AS
 | 
			
		||||
    SELECT *
 | 
			
		||||
    from thingsboard.alarm
 | 
			
		||||
    WHERE tenant_id IS NOT NULL AND originator_id IS NOT NULL AND originator_type IS NOT NULL AND type IS NOT NULL
 | 
			
		||||
    AND type IS NOT NULL AND id IS NOT NULL
 | 
			
		||||
    PRIMARY KEY (id, tenant_id, originator_id, originator_type, type)
 | 
			
		||||
    WITH CLUSTERING ORDER BY ( tenant_id ASC, originator_id ASC, originator_type ASC, type ASC);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS thingsboard.relation (
 | 
			
		||||
	from_id timeuuid,
 | 
			
		||||
	from_type text,
 | 
			
		||||
@ -285,7 +310,14 @@ CREATE TABLE IF NOT EXISTS thingsboard.relation (
 | 
			
		||||
	relation_type text,
 | 
			
		||||
	additional_info text,
 | 
			
		||||
	PRIMARY KEY ((from_id, from_type), relation_type, to_id, to_type)
 | 
			
		||||
);
 | 
			
		||||
) WITH CLUSTERING ORDER BY ( relation_type ASC, to_id ASC, to_type ASC);
 | 
			
		||||
 | 
			
		||||
CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.relation_by_type_and_child_type AS
 | 
			
		||||
    SELECT *
 | 
			
		||||
    from thingsboard.relation
 | 
			
		||||
    WHERE from_id IS NOT NULL AND from_type IS NOT NULL AND relation_type IS NOT NULL AND to_id IS NOT NULL AND to_type IS NOT NULL
 | 
			
		||||
    PRIMARY KEY ((from_id, from_type), relation_type, to_type, to_id)
 | 
			
		||||
    WITH CLUSTERING ORDER BY ( relation_type ASC, to_type ASC, to_id DESC);
 | 
			
		||||
 | 
			
		||||
CREATE MATERIALIZED VIEW IF NOT EXISTS thingsboard.reverse_relation AS
 | 
			
		||||
    SELECT *
 | 
			
		||||
 | 
			
		||||
@ -40,6 +40,7 @@ import org.thingsboard.server.common.data.plugin.ComponentScope;
 | 
			
		||||
import org.thingsboard.server.common.data.plugin.ComponentType;
 | 
			
		||||
import org.thingsboard.server.common.data.plugin.PluginMetaData;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleMetaData;
 | 
			
		||||
import org.thingsboard.server.dao.alarm.AlarmService;
 | 
			
		||||
import org.thingsboard.server.dao.asset.AssetService;
 | 
			
		||||
import org.thingsboard.server.dao.component.ComponentDescriptorService;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerService;
 | 
			
		||||
@ -118,6 +119,9 @@ public abstract class AbstractServiceTest {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected RelationService relationService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    protected AlarmService alarmService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ComponentDescriptorService componentDescriptorService;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,172 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.service;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.utils.UUIDs;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.Tenant;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmQuery;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmSeverity;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.AlarmStatus;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageData;
 | 
			
		||||
import org.thingsboard.server.common.data.page.TimePageLink;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntityRelationsQuery;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntitySearchDirection;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntityTypeFilter;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationsSearchParameters;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
 | 
			
		||||
public class AlarmServiceTest extends AbstractServiceTest {
 | 
			
		||||
 | 
			
		||||
    public static final String TEST_ALARM = "TEST_ALARM";
 | 
			
		||||
    private TenantId tenantId;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void before() {
 | 
			
		||||
        Tenant tenant = new Tenant();
 | 
			
		||||
        tenant.setTitle("My tenant");
 | 
			
		||||
        Tenant savedTenant = tenantService.saveTenant(tenant);
 | 
			
		||||
        Assert.assertNotNull(savedTenant);
 | 
			
		||||
        tenantId = savedTenant.getId();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void after() {
 | 
			
		||||
        tenantService.deleteTenant(tenantId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSaveAndFetchAlarm() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.saveRelation(relation).get());
 | 
			
		||||
 | 
			
		||||
        long ts = System.currentTimeMillis();
 | 
			
		||||
        Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId)
 | 
			
		||||
                .type(TEST_ALARM)
 | 
			
		||||
                .severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK)
 | 
			
		||||
                .startTs(ts).build();
 | 
			
		||||
 | 
			
		||||
        Alarm created = alarmService.createOrUpdateAlarm(alarm);
 | 
			
		||||
 | 
			
		||||
        Assert.assertNotNull(created);
 | 
			
		||||
        Assert.assertNotNull(created.getId());
 | 
			
		||||
        Assert.assertNotNull(created.getOriginator());
 | 
			
		||||
        Assert.assertNotNull(created.getSeverity());
 | 
			
		||||
        Assert.assertNotNull(created.getStatus());
 | 
			
		||||
 | 
			
		||||
        Assert.assertEquals(tenantId, created.getTenantId());
 | 
			
		||||
        Assert.assertEquals(childId, created.getOriginator());
 | 
			
		||||
        Assert.assertEquals(TEST_ALARM, created.getType());
 | 
			
		||||
        Assert.assertEquals(AlarmSeverity.CRITICAL, created.getSeverity());
 | 
			
		||||
        Assert.assertEquals(AlarmStatus.ACTIVE_UNACK, created.getStatus());
 | 
			
		||||
        Assert.assertEquals(ts, created.getStartTs());
 | 
			
		||||
        Assert.assertEquals(ts, created.getEndTs());
 | 
			
		||||
        Assert.assertEquals(0L, created.getAckTs());
 | 
			
		||||
        Assert.assertEquals(0L, created.getClearTs());
 | 
			
		||||
 | 
			
		||||
        Alarm fetched = alarmService.findAlarmByIdAsync(created.getId()).get();
 | 
			
		||||
        Assert.assertEquals(created, fetched);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testFindAlarm() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.saveRelation(relation).get());
 | 
			
		||||
 | 
			
		||||
        long ts = System.currentTimeMillis();
 | 
			
		||||
        Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId)
 | 
			
		||||
                .type(TEST_ALARM)
 | 
			
		||||
                .severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK)
 | 
			
		||||
                .startTs(ts).build();
 | 
			
		||||
 | 
			
		||||
        Alarm created = alarmService.createOrUpdateAlarm(alarm);
 | 
			
		||||
 | 
			
		||||
        // Check child relation
 | 
			
		||||
        TimePageData<Alarm> alarms = alarmService.findAlarms(AlarmQuery.builder()
 | 
			
		||||
                .affectedEntityId(childId)
 | 
			
		||||
                .status(AlarmStatus.ACTIVE_UNACK).pageLink(
 | 
			
		||||
                        new TimePageLink(1, 0L, System.currentTimeMillis(), false)
 | 
			
		||||
                ).build()).get();
 | 
			
		||||
        Assert.assertNotNull(alarms.getData());
 | 
			
		||||
        Assert.assertEquals(1, alarms.getData().size());
 | 
			
		||||
        Assert.assertEquals(created, alarms.getData().get(0));
 | 
			
		||||
 | 
			
		||||
        // Check parent relation
 | 
			
		||||
        alarms = alarmService.findAlarms(AlarmQuery.builder()
 | 
			
		||||
                .affectedEntityId(parentId)
 | 
			
		||||
                .status(AlarmStatus.ACTIVE_UNACK).pageLink(
 | 
			
		||||
                        new TimePageLink(1, 0L, System.currentTimeMillis(), false)
 | 
			
		||||
                ).build()).get();
 | 
			
		||||
        Assert.assertNotNull(alarms.getData());
 | 
			
		||||
        Assert.assertEquals(1, alarms.getData().size());
 | 
			
		||||
        Assert.assertEquals(created, alarms.getData().get(0));
 | 
			
		||||
 | 
			
		||||
        alarmService.ackAlarm(created.getId(), System.currentTimeMillis()).get();
 | 
			
		||||
        created = alarmService.findAlarmByIdAsync(created.getId()).get();
 | 
			
		||||
 | 
			
		||||
        alarms = alarmService.findAlarms(AlarmQuery.builder()
 | 
			
		||||
                .affectedEntityId(childId)
 | 
			
		||||
                .status(AlarmStatus.ACTIVE_ACK).pageLink(
 | 
			
		||||
                        new TimePageLink(1, 0L, System.currentTimeMillis(), false)
 | 
			
		||||
                ).build()).get();
 | 
			
		||||
        Assert.assertNotNull(alarms.getData());
 | 
			
		||||
        Assert.assertEquals(1, alarms.getData().size());
 | 
			
		||||
        Assert.assertEquals(created, alarms.getData().get(0));
 | 
			
		||||
 | 
			
		||||
        // Check not existing relation
 | 
			
		||||
        alarms = alarmService.findAlarms(AlarmQuery.builder()
 | 
			
		||||
                .affectedEntityId(childId)
 | 
			
		||||
                .status(AlarmStatus.ACTIVE_UNACK).pageLink(
 | 
			
		||||
                        new TimePageLink(1, 0L, System.currentTimeMillis(), false)
 | 
			
		||||
                ).build()).get();
 | 
			
		||||
        Assert.assertNotNull(alarms.getData());
 | 
			
		||||
        Assert.assertEquals(0, alarms.getData().size());
 | 
			
		||||
 | 
			
		||||
        alarmService.clearAlarm(created.getId(), System.currentTimeMillis()).get();
 | 
			
		||||
        created = alarmService.findAlarmByIdAsync(created.getId()).get();
 | 
			
		||||
 | 
			
		||||
        alarms = alarmService.findAlarms(AlarmQuery.builder()
 | 
			
		||||
                .affectedEntityId(childId)
 | 
			
		||||
                .status(AlarmStatus.CLEARED_ACK).pageLink(
 | 
			
		||||
                        new TimePageLink(1, 0L, System.currentTimeMillis(), false)
 | 
			
		||||
                ).build()).get();
 | 
			
		||||
        Assert.assertNotNull(alarms.getData());
 | 
			
		||||
        Assert.assertEquals(1, alarms.getData().size());
 | 
			
		||||
        Assert.assertEquals(created, alarms.getData().get(0));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,283 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Copyright © 2016-2017 The Thingsboard Authors
 | 
			
		||||
 *
 | 
			
		||||
 * Licensed under the Apache License, Version 2.0 (the "License");
 | 
			
		||||
 * you may not use this file except in compliance with the License.
 | 
			
		||||
 * You may obtain a copy of the License at
 | 
			
		||||
 *
 | 
			
		||||
 *     http://www.apache.org/licenses/LICENSE-2.0
 | 
			
		||||
 *
 | 
			
		||||
 * Unless required by applicable law or agreed to in writing, software
 | 
			
		||||
 * distributed under the License is distributed on an "AS IS" BASIS,
 | 
			
		||||
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
			
		||||
 * See the License for the specific language governing permissions and
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.server.dao.service;
 | 
			
		||||
 | 
			
		||||
import com.datastax.driver.core.utils.UUIDs;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.asset.Asset;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.relation.EntityRelation;
 | 
			
		||||
import org.thingsboard.server.dao.exception.DataValidationException;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntityRelationsQuery;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntitySearchDirection;
 | 
			
		||||
import org.thingsboard.server.dao.relation.EntityTypeFilter;
 | 
			
		||||
import org.thingsboard.server.dao.relation.RelationsSearchParameters;
 | 
			
		||||
 | 
			
		||||
import java.util.Collections;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.ExecutionException;
 | 
			
		||||
 | 
			
		||||
public class RelationServiceTest extends AbstractServiceTest {
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void before() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void after() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testSaveRelation() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(saveRelation(relation));
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(parentId, childId, "NOT_EXISTING_TYPE").get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(childId, parentId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(childId, parentId, "NOT_EXISTING_TYPE").get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDeleteRelation() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId subChildId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationA);
 | 
			
		||||
        saveRelation(relationB);
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.deleteRelation(relationA).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.deleteRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testDeleteEntityRelations() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId subChildId = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationA = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationB = new EntityRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationA);
 | 
			
		||||
        saveRelation(relationB);
 | 
			
		||||
 | 
			
		||||
        Assert.assertTrue(relationService.deleteEntityRelations(childId).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(parentId, childId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
 | 
			
		||||
        Assert.assertFalse(relationService.checkRelation(childId, subChildId, EntityRelation.CONTAINS_TYPE).get());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testFindFrom() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId parentB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
 | 
			
		||||
        EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationA1);
 | 
			
		||||
        saveRelation(relationA2);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationB1);
 | 
			
		||||
        saveRelation(relationB2);
 | 
			
		||||
 | 
			
		||||
        List<EntityRelation> relations = relationService.findByFrom(parentA).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
        for (EntityRelation relation : relations) {
 | 
			
		||||
            Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType());
 | 
			
		||||
            Assert.assertEquals(parentA, relation.getFrom());
 | 
			
		||||
            Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(0, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByFrom(parentB).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
        for (EntityRelation relation : relations) {
 | 
			
		||||
            Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType());
 | 
			
		||||
            Assert.assertEquals(parentB, relation.getFrom());
 | 
			
		||||
            Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(0, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(0, relations.size());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private Boolean saveRelation(EntityRelation relationA1) throws ExecutionException, InterruptedException {
 | 
			
		||||
        return relationService.saveRelation(relationA1).get();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testFindTo() throws ExecutionException, InterruptedException {
 | 
			
		||||
        AssetId parentA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId parentB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId childB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
 | 
			
		||||
        EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationA1);
 | 
			
		||||
        saveRelation(relationA2);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationB1);
 | 
			
		||||
        saveRelation(relationB2);
 | 
			
		||||
 | 
			
		||||
        // Data propagation to views is async
 | 
			
		||||
        Thread.sleep(3000);
 | 
			
		||||
 | 
			
		||||
        List<EntityRelation> relations = relationService.findByTo(childA).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
        for (EntityRelation relation : relations) {
 | 
			
		||||
            Assert.assertEquals(childA, relation.getTo());
 | 
			
		||||
            Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByToAndType(childA, EntityRelation.CONTAINS_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(1, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByToAndType(childB, EntityRelation.MANAGES_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(1, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByToAndType(parentA, EntityRelation.MANAGES_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(0, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByToAndType(parentB, EntityRelation.MANAGES_TYPE).get();
 | 
			
		||||
        Assert.assertEquals(0, relations.size());
 | 
			
		||||
 | 
			
		||||
        relations = relationService.findByTo(childB).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
        for (EntityRelation relation : relations) {
 | 
			
		||||
            Assert.assertEquals(childB, relation.getTo());
 | 
			
		||||
            Assert.assertTrue(parentA.equals(relation.getFrom()) || parentB.equals(relation.getFrom()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testCyclicRecursiveRelation() throws ExecutionException, InterruptedException {
 | 
			
		||||
        // A -> B -> C -> A
 | 
			
		||||
        AssetId assetA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId assetB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId assetC = new AssetId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationA = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationB = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationC = new EntityRelation(assetC, assetA, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationA);
 | 
			
		||||
        saveRelation(relationB);
 | 
			
		||||
        saveRelation(relationC);
 | 
			
		||||
 | 
			
		||||
        EntityRelationsQuery query = new EntityRelationsQuery();
 | 
			
		||||
        query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
 | 
			
		||||
        query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
 | 
			
		||||
        List<EntityRelation> relations = relationService.findByQuery(query).get();
 | 
			
		||||
        Assert.assertEquals(3, relations.size());
 | 
			
		||||
        Assert.assertTrue(relations.contains(relationA));
 | 
			
		||||
        Assert.assertTrue(relations.contains(relationB));
 | 
			
		||||
        Assert.assertTrue(relations.contains(relationC));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void testRecursiveRelation() throws ExecutionException, InterruptedException {
 | 
			
		||||
        // A -> B -> [C,D]
 | 
			
		||||
        AssetId assetA = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId assetB = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        AssetId assetC = new AssetId(UUIDs.timeBased());
 | 
			
		||||
        DeviceId deviceD = new DeviceId(UUIDs.timeBased());
 | 
			
		||||
 | 
			
		||||
        EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        saveRelation(relationAB);
 | 
			
		||||
        saveRelation(relationBC);
 | 
			
		||||
        saveRelation(relationBD);
 | 
			
		||||
 | 
			
		||||
        EntityRelationsQuery query = new EntityRelationsQuery();
 | 
			
		||||
        query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
 | 
			
		||||
        query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
 | 
			
		||||
        List<EntityRelation> relations = relationService.findByQuery(query).get();
 | 
			
		||||
        Assert.assertEquals(2, relations.size());
 | 
			
		||||
        Assert.assertTrue(relations.contains(relationAB));
 | 
			
		||||
        Assert.assertTrue(relations.contains(relationBC));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    @Test(expected = DataValidationException.class)
 | 
			
		||||
    public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
 | 
			
		||||
        EntityRelation relation = new EntityRelation();
 | 
			
		||||
        relation.setTo(new AssetId(UUIDs.timeBased()));
 | 
			
		||||
        relation.setType(EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        Assert.assertTrue(saveRelation(relation));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test(expected = DataValidationException.class)
 | 
			
		||||
    public void testSaveRelationWithEmptyTo() throws ExecutionException, InterruptedException {
 | 
			
		||||
        EntityRelation relation = new EntityRelation();
 | 
			
		||||
        relation.setFrom(new AssetId(UUIDs.timeBased()));
 | 
			
		||||
        relation.setType(EntityRelation.CONTAINS_TYPE);
 | 
			
		||||
        Assert.assertTrue(saveRelation(relation));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test(expected = DataValidationException.class)
 | 
			
		||||
    public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException {
 | 
			
		||||
        EntityRelation relation = new EntityRelation();
 | 
			
		||||
        relation.setFrom(new AssetId(UUIDs.timeBased()));
 | 
			
		||||
        relation.setTo(new AssetId(UUIDs.timeBased()));
 | 
			
		||||
        Assert.assertTrue(saveRelation(relation));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user