Skip to content

Commit

Permalink
Merge pull request #909 from hashmapinc/Tempus-842-Metadata-Attribute…
Browse files Browse the repository at this point in the history
…-Integration

Tempus 842 metadata attribute integration
  • Loading branch information
shgupta22 committed Dec 6, 2018
2 parents 73cebf6 + c6090dc commit 214ba56
Show file tree
Hide file tree
Showing 43 changed files with 667 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public void testInsertMetadataRequestFromMetadataService() throws Exception {
keyValueMap.put("Key 1", "Value 1");
keyValueMap.put("Key 2", "Value 2");

IngestMetadataRequest content = new IngestMetadataRequest(metadataConfig.getId(), metadataConfig.getOwnerId(), metadataConfig.getName(), keyValueMap);
IngestMetadataRequest content = new IngestMetadataRequest(metadataConfig.getId(), metadataConfig.getOwnerId(), metadataConfig.getName(), "attribute", keyValueMap);
MockHttpServletRequestBuilder postRequest = post("/api/metadata")
.header("authorization", "bearer " + clientCredentialToken)
.contentType(contentType)
Expand All @@ -557,7 +557,7 @@ public void testInsertMetadataRequestAsATempusUser() throws Exception {
keyValueMap.put("Key 1", "Value 1");
keyValueMap.put("Key 2", "Value 2");

IngestMetadataRequest content = new IngestMetadataRequest(metadataConfig.getId(), metadataConfig.getOwnerId(), metadataConfig.getName(), keyValueMap);
IngestMetadataRequest content = new IngestMetadataRequest(metadataConfig.getId(), metadataConfig.getOwnerId(), metadataConfig.getName(), "attribute", keyValueMap);

doPost("/api/metadata", content).andExpect(status().isForbidden());
}
Expand Down Expand Up @@ -589,6 +589,7 @@ private MetadataQuery createMetadataQuery() throws Exception {
mq.setId(new MetadataQueryId(UUIDs.timeBased()));
mq.setMetadataConfigId(metadataConfig.getId());
mq.setQueryStmt(queryStmt);
mq.setAttribute("Test Attribute");
mq.setTriggerSchedule("0/10 * * ? * * *");
mq.setTriggerType(MetadataIngestionTriggerType.CRON);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.hashmapinc.server.common.data.asset.Asset;
import com.hashmapinc.server.common.data.audit.ActionType;
import com.hashmapinc.server.common.data.computation.ComputationJob;
import com.hashmapinc.server.common.data.datamodel.DataModelObject;
import com.hashmapinc.server.common.data.id.*;
import com.hashmapinc.server.common.data.page.TextPageLink;
import com.hashmapinc.server.common.data.page.TimePageLink;
Expand Down Expand Up @@ -469,6 +470,24 @@ protected void checkAsset(Asset asset) throws TempusException {
}
}

DataModelObject checkDataModelObjectId(DataModelObjectId dataModelObjectId) throws TempusException {
try {
validateId(dataModelObjectId, "Incorrect dataModelObjcetId " + dataModelObjectId);
DataModelObject dataModelObject = dataModelObjectService.findById(dataModelObjectId);
checkDataModelObject(dataModelObject);
return dataModelObject;
} catch (Exception e) {
throw handleException(e, false);
}
}

private void checkDataModelObject(DataModelObject dataModelObject) throws TempusException {
checkNotNull(dataModelObject);
if (dataModelObject.getCustomerId() != null && !dataModelObject.getCustomerId().getId().equals(ModelConstants.NULL_UUID)) {
checkCustomerId(dataModelObject.getCustomerId());
}
}

Alarm checkAlarmId(AlarmId alarmId) throws TempusException {
try {
validateId(alarmId, "Incorrect alarmId " + alarmId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@
*/
package com.hashmapinc.server.controller;

import com.hashmapinc.server.common.data.Device;
import com.hashmapinc.server.common.data.MetadataIngestionEntries;
import com.hashmapinc.server.common.data.UUIDConverter;
import com.hashmapinc.server.common.data.asset.Asset;
import com.hashmapinc.server.common.data.id.AssetId;
import com.hashmapinc.server.common.data.id.DataModelObjectId;
import com.hashmapinc.server.common.data.id.DeviceId;
import com.hashmapinc.server.common.data.id.TenantId;
import com.hashmapinc.server.common.data.kv.MetaDataKvEntry;
import com.hashmapinc.server.common.data.kv.StringDataEntry;
Expand All @@ -27,6 +32,7 @@
import com.hashmapinc.server.common.data.metadata.MetadataQueryId;
import com.hashmapinc.server.common.data.page.TextPageData;
import com.hashmapinc.server.common.data.page.TextPageLink;
import com.hashmapinc.server.exception.TempusErrorCode;
import com.hashmapinc.server.exception.TempusException;
import com.hashmapinc.server.requests.IngestMetadataRequest;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -35,6 +41,7 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
Expand All @@ -44,6 +51,9 @@ public class MetadataController extends BaseController {

private static final String METADATA_CONFIG_ID = "metadataConfigId";
private static final String METADATA_QUERY_ID = "metadataQueryId";
private static final String ENTITY_TYPE = "entityType";
private static final String ENTITY_ID = "entityId";
private static final String KEY_ATTRIBUTE_NOT_FOUND = "Key Attribute not found with this name.";

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@PostMapping(value = "/metadata/config")
Expand Down Expand Up @@ -186,6 +196,7 @@ public void insert(@RequestBody IngestMetadataRequest request) throws TempusExce
.metadataConfigId(request.getConfigId())
.metadataSourceName(request.getConfigName())
.tenantId(new TenantId(UUIDConverter.fromString(request.getOwnerId())))
.attribute(request.getAttribute())
.metaDataKvEntries(request.getData().entrySet().stream()
.map(e -> new MetaDataKvEntry(
new StringDataEntry(e.getKey(), e.getValue().toString()),
Expand All @@ -199,4 +210,45 @@ public void insert(@RequestBody IngestMetadataRequest request) throws TempusExce
throw handleException(e);
}
}

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@GetMapping(value = "/metadata/attribute/{entityType}/{entityId}", params = {"key", "value"})
@ResponseBody
public List<MetaDataKvEntry> getMetaDataKvEntryForEntity(
@PathVariable(ENTITY_TYPE) String entityType,
@PathVariable(ENTITY_ID) String entityId,
@RequestParam String key,
@RequestParam String value) throws TempusException {

checkParameter(ENTITY_TYPE, entityType);
checkParameter(ENTITY_ID, entityId);

DataModelObjectId dataModelObjectId = null;
String keyAttribute;
TenantId tenantId = getCurrentUser().getTenantId();

try {
if (entityType.equals("ASSET")) {
AssetId assetId = new AssetId(toUUID(entityId));
Asset asset = checkAssetId(assetId);
dataModelObjectId = asset.getDataModelObjectId();
} else {
DeviceId deviceId = new DeviceId(toUUID(entityId));
Device device = checkDeviceId(deviceId);
/* TODO : Remove this comment when we have Device and DataModelObject mapping
dataModelObjectId = device.getDataModelObjectId();
*/
}
checkDataModelObjectId(dataModelObjectId);
keyAttribute = dataModelObjectService.findKeyAttributeByDataModelObjectId(dataModelObjectId);
checkNotNull(keyAttribute);
if (key.equals(keyAttribute)) {
return metadataIngestionService.findKvEntryByKeyAttributeAndTenantId(value, tenantId);
} else {
throw new TempusException(KEY_ATTRIBUTE_NOT_FOUND, TempusErrorCode.ITEM_NOT_FOUND);
}
} catch (Exception e) {
throw handleException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public class IngestMetadataRequest {
private MetadataConfigId configId;
private String ownerId;
private String configName;
private String attribute;
private Map<String, Object> data;
}
43 changes: 22 additions & 21 deletions application/src/main/resources/tempus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ quota:
topSize: 10
intervalMin: 2

#Telementry database type option
database:
type: "${DATABASE_TYPE:cassandra}" # cassandra OR sql

Expand Down Expand Up @@ -334,22 +335,6 @@ configurations:
depthSeries: "true"

# HSQLDB DAO Configuration
spring:
data:
jpa:
repositories:
enabled: "true"
jpa:
hibernate:
ddl-auto: "validate"
database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}"
datasource:
driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}"
url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/tempusDb;sql.enforce_size=false}"
username: "${SPRING_DATASOURCE_USERNAME:sa}"
password: "${SPRING_DATASOURCE_PASSWORD:}"

# PostgreSQL DAO Configuration
#spring:
# data:
# jpa:
Expand All @@ -358,12 +343,28 @@ spring:
# jpa:
# hibernate:
# ddl-auto: "validate"
# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}"
# database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.HSQLDialect}"
# datasource:
# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}"
# url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://postgres:5432/tempus}"
# username: "${SPRING_DATASOURCE_USERNAME:postgres}"
# password: "${SPRING_DATASOURCE_PASSWORD:postgres}"
# driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.hsqldb.jdbc.JDBCDriver}"
# url: "${SPRING_DATASOURCE_URL:jdbc:hsqldb:file:${SQL_DATA_FOLDER:/tmp}/tempusDb;sql.enforce_size=false}"
# username: "${SPRING_DATASOURCE_USERNAME:sa}"
# password: "${SPRING_DATASOURCE_PASSWORD:}"

# PostgreSQL DAO Configuration
spring:
data:
jpa:
repositories:
enabled: "true"
jpa:
hibernate:
ddl-auto: "validate"
database-platform: "${SPRING_JPA_DATABASE_PLATFORM:org.hibernate.dialect.PostgreSQLDialect}"
datasource:
driverClassName: "${SPRING_DRIVER_CLASS_NAME:org.postgresql.Driver}"
url: "${SPRING_DATASOURCE_URL:jdbc:postgresql://postgres:5432/tempus}"
username: "${SPRING_DATASOURCE_USERNAME:postgres}"
password: "${SPRING_DATASOURCE_PASSWORD:postgres}"

#max file and request size
spring.http:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ public class MetadataIngestionEntries {
private TenantId tenantId;
private MetadataConfigId metadataConfigId;
private String metadataSourceName;
private String attribute;
private List<MetaDataKvEntry> metaDataKvEntries;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package com.hashmapinc.server.common.data.datamodel;

import com.hashmapinc.server.common.data.id.DataModelObjectId;
import lombok.ToString;

import java.io.Serializable;

@ToString
public class AttributeDefinition implements Serializable{

private static final long serialVersionUID = -4403266614883680756L;
Expand All @@ -29,6 +31,7 @@ public class AttributeDefinition implements Serializable{
private String valueType;
private DataModelObjectId dataModelObjectId;
private String source;
private boolean keyAttribute;

public AttributeDefinition(){

Expand All @@ -40,6 +43,7 @@ public AttributeDefinition(AttributeDefinition attributeDefinition){
this.source = attributeDefinition.source;
this.value = attributeDefinition.value;
this.valueType = attributeDefinition.valueType;
this.keyAttribute = attributeDefinition.keyAttribute;
}

@Override
Expand All @@ -53,6 +57,7 @@ public boolean equals(Object o) {
if (dataModelObjectId != null ? !dataModelObjectId.equals(that.dataModelObjectId) : that.dataModelObjectId != null) return false;
if (source != null ? !source.equals(that.source) : that.source != null) return false;
if (value != null ? !value.equals(that.value) : that.value != null) return false;
if (keyAttribute != that.keyAttribute) return false;
return (valueType != null ? valueType.equals(that.valueType) : that.valueType == null);

}
Expand Down Expand Up @@ -107,4 +112,12 @@ public String getSource() {
public void setSource(String source) {
this.source = source;
}

public boolean isKeyAttribute() {
return keyAttribute;
}

public void setKeyAttribute(boolean keyAttribute) {
this.keyAttribute = keyAttribute;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class MetadataQuery extends SearchTextBased<MetadataQueryId> {
private String queryStmt;
private MetadataIngestionTriggerType triggerType;
private String triggerSchedule;
private String attribute;

@Override
public String getSearchText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ public void testDeleteDataModelObjectsByDataModelId() {
Assert.assertEquals(0,foundDataModelObjects.size());
}

@Test
public void testFindKeyAttributeByDataModelObjectId() {
AttributeDefinition attributeDef = new AttributeDefinition();
attributeDef.setName("Attr_Id");
attributeDef.setValue("ATTR_101");
attributeDef.setValueType(DataType.STRING.name());
attributeDef.setKeyAttribute(true);

DataModelObject dataModelObj = getDataModelObjectWithOneAttributeDef(attributeDef);
dataModelObj.setName("Test_DMO");
dataModelObject = dataModelObjectService.save(dataModelObj);
Assert.assertNotNull(dataModelObject);
String keyAttribute = dataModelObjectService.findKeyAttributeByDataModelObjectId(dataModelObject.getId());
Assert.assertEquals("Attr_Id", keyAttribute);
}

private void createDataModelObject() {
AttributeDefinition attributeDef = new AttributeDefinition();
attributeDef.setName("Version");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void ingestAndFindMetadataEntries() throws Exception {
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataConfigId(metadataConfigId)
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(metaDataKvEntry1, metaDataKvEntry2))
.build();

Expand All @@ -70,6 +71,7 @@ public void testSaveMetadataEntriesWithIncorrectTenant() {
MetadataIngestionEntries metadataEntries = MetadataIngestionEntries.builder()
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("Key 1", "Value 1"), DateTime.now().getMillis())))
.build();

Expand All @@ -84,6 +86,7 @@ public void testSaveMetadataEntriesWithIncorrectConfig() {
MetadataIngestionEntries metadataEntries = MetadataIngestionEntries.builder()
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("Key 1", "Value 1"), DateTime.now().getMillis())))
.build();

Expand All @@ -97,6 +100,7 @@ public void testSaveMetadataEntriesWithIncorrectSource() {

MetadataIngestionEntries metadataEntries = MetadataIngestionEntries.builder()
.tenantId(new TenantId(UUIDs.timeBased()))
.attribute("Test Attribute")
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("Key 1", "Value 1"), DateTime.now().getMillis())))
.build();
Expand All @@ -113,6 +117,7 @@ public void testSaveMetadataEntriesWithNoMetadataEntry() {
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.build();

metadataIngestionService.save(metadataEntries);
Expand All @@ -127,6 +132,7 @@ public void testSaveMetadataEntriesWithInvalidMetadataKey() {
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("", "Value 1"), DateTime.now().getMillis())))
.build();

Expand All @@ -142,6 +148,7 @@ public void testSaveMetadataEntriesWithInvalidMetadataValue() {
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("Key 1", ""), DateTime.now().getMillis())))
.build();

Expand All @@ -157,6 +164,7 @@ public void testSaveMetadataEntriesWithInvalidMetadataTs() {
.tenantId(new TenantId(UUIDs.timeBased()))
.metadataConfigId(new MetadataConfigId(UUIDs.timeBased()))
.metadataSourceName("Metadata Source 1")
.attribute("Test Attribute")
.metaDataKvEntries(Arrays.asList(new MetaDataKvEntry(new StringDataEntry("Key 1", "Value 1"), -1L)))
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@
import java.util.UUID;

public interface AttributeDefinitionDao {

AttributeDefinition save(AttributeDefinition attributeDefinition);

AttributeDefinition findByNameAndDataModelObjectId(String name, UUID id);

List<AttributeDefinition> findByDataModelObjectId(DataModelObjectId dataModelObjectId);

void removeByNameAndDataModelObjectId(String name,DataModelObjectId dataModelObjectId);

AttributeDefinition findKeyAttributeDefinitionByDataModelObjectId(DataModelObjectId dataModelObjectId);
}
Loading

0 comments on commit 214ba56

Please sign in to comment.