use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class AtlasNotificationMapper method buildServiceTags.
private static ServiceTags buildServiceTags(RangerAtlasEntityWithTags entityWithTags, Map<String, ServiceTags> serviceTagsMap) {
ServiceTags ret = null;
RangerAtlasEntity entity = entityWithTags.getEntity();
RangerServiceResource serviceResource = AtlasResourceMapperUtil.getRangerServiceResource(entity);
if (serviceResource != null) {
List<RangerTag> tags = getTags(entityWithTags);
List<RangerTagDef> tagDefs = getTagDefs(entityWithTags);
String serviceName = serviceResource.getServiceName();
ret = createOrGetServiceTags(serviceTagsMap, serviceName);
serviceResource.setId((long) ret.getServiceResources().size());
ret.getServiceResources().add(serviceResource);
List<Long> tagIds = new ArrayList<>();
if (CollectionUtils.isNotEmpty(tags)) {
for (RangerTag tag : tags) {
tag.setId((long) ret.getTags().size());
ret.getTags().put(tag.getId(), tag);
tagIds.add(tag.getId());
}
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Entity " + entityWithTags + " does not have any tags associated with it");
}
}
ret.getResourceToTagIds().put(serviceResource.getId(), tagIds);
if (CollectionUtils.isNotEmpty(tagDefs)) {
for (RangerTagDef tagDef : tagDefs) {
tagDef.setId((long) ret.getTagDefinitions().size());
ret.getTagDefinitions().put(tagDef.getId(), tagDef);
}
}
} else {
LOG.error("Failed to build serviceResource for entity:" + entity.getGuid());
}
return ret;
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class AtlasNotificationMapper method buildServiceTags.
private static Map<String, ServiceTags> buildServiceTags(List<RangerAtlasEntityWithTags> entitiesWithTags) {
Map<String, ServiceTags> ret = new HashMap<>();
for (RangerAtlasEntityWithTags element : entitiesWithTags) {
RangerAtlasEntity entity = element.getEntity();
if (entity != null) {
buildServiceTags(element, ret);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Ignoring entity because its State is not ACTIVE: " + element);
}
}
}
// Remove duplicate tag definitions
if (CollectionUtils.isNotEmpty(ret.values())) {
for (ServiceTags serviceTag : ret.values()) {
if (MapUtils.isNotEmpty(serviceTag.getTagDefinitions())) {
Map<String, RangerTagDef> uniqueTagDefs = new HashMap<>();
for (RangerTagDef tagDef : serviceTag.getTagDefinitions().values()) {
RangerTagDef existingTagDef = uniqueTagDefs.get(tagDef.getName());
if (existingTagDef == null) {
uniqueTagDefs.put(tagDef.getName(), tagDef);
} else {
if (CollectionUtils.isNotEmpty(tagDef.getAttributeDefs())) {
for (RangerTagAttributeDef tagAttrDef : tagDef.getAttributeDefs()) {
boolean attrDefExists = false;
if (CollectionUtils.isNotEmpty(existingTagDef.getAttributeDefs())) {
for (RangerTagAttributeDef existingTagAttrDef : existingTagDef.getAttributeDefs()) {
if (StringUtils.equalsIgnoreCase(existingTagAttrDef.getName(), tagAttrDef.getName())) {
attrDefExists = true;
break;
}
}
}
if (!attrDefExists) {
existingTagDef.getAttributeDefs().add(tagAttrDef);
}
}
}
}
}
serviceTag.getTagDefinitions().clear();
for (RangerTagDef tagDef : uniqueTagDefs.values()) {
serviceTag.getTagDefinitions().put(tagDef.getId(), tagDef);
}
}
}
}
if (MapUtils.isNotEmpty(ret)) {
for (Map.Entry<String, ServiceTags> entry : ret.entrySet()) {
ServiceTags serviceTags = entry.getValue();
serviceTags.setOp(ServiceTags.OP_REPLACE);
}
}
return ret;
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class AtlasNotificationMapper method getTagDefs.
private static List<RangerTagDef> getTagDefs(RangerAtlasEntityWithTags entityWithTags) {
List<RangerTagDef> ret = new ArrayList<>();
if (entityWithTags != null && CollectionUtils.isNotEmpty(entityWithTags.getTags())) {
Map<String, String> tagNames = new HashMap<>();
for (EntityNotificationWrapper.RangerAtlasClassification tag : entityWithTags.getTags()) {
if (!tagNames.containsKey(tag.getName())) {
tagNames.put(tag.getName(), tag.getName());
RangerTagDef tagDef = new RangerTagDef(tag.getName(), "Atlas");
if (MapUtils.isNotEmpty(tag.getAttributes())) {
for (String attributeName : tag.getAttributes().keySet()) {
tagDef.getAttributeDefs().add(new RangerTagAttributeDef(attributeName, entityWithTags.getTagAttributeType(tag.getName(), attributeName)));
}
}
ret.add(tagDef);
}
}
}
return ret;
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestServiceTagProcessor method test3process.
@Test
public void test3process() throws Exception {
serviceTags = new ServiceTags();
Map<Long, RangerTagDef> fd = new HashMap<>();
List<RangerTag> associatedTags = new ArrayList<>();
RangerTagDef rTagDef = Mockito.mock(RangerTagDef.class);
rTagDef.setGuid("guid");
rTagDef.setName("rTagDefname");
fd.put(1l, rTagDef);
serviceTags.setTagDefinitions(fd);
List<RangerServiceResource> serviceResources = new ArrayList<RangerServiceResource>();
RangerServiceResource rserRes = new RangerServiceResource();
testServiceRest = new TestServiceREST();
RangerPolicy rp = testServiceRest.rangerPolicy();
rserRes.setResourceElements(rp.getResources());
rserRes.setGuid("guId");
rserRes.setId(1L);
serviceResources.add(rserRes);
serviceTags.setServiceResources(serviceResources);
Map<Long, List<Long>> resourceToTagIds = new HashMap<>();
resourceToTagIds.put(1L, new ArrayList<Long>(Arrays.asList(22L, 1L, 0L)));
serviceTags.setResourceToTagIds(resourceToTagIds);
RangerTag rTag = new RangerTag();
rTag.setId(22L);
rTag.setType("type1");
Map<Long, RangerTag> tags = new HashMap<>();
rTag.setOwner((short) 0);
tags.put(1L, rTag);
serviceTags.setTags(tags);
associatedTags.add(rTag);
TagStore tagStore = Mockito.mock(TagStore.class);
sTagProcessor = new ServiceTagsProcessor(tagStore);
Mockito.when(tagStore.createTagDef(rTagDef)).thenReturn(rTagDef);
Mockito.when(tagStore.getServiceResourceByGuid(rserRes.getGuid())).thenReturn(rserRes);
Mockito.when(tagStore.getTagsForResourceId(rserRes.getId())).thenReturn(associatedTags);
sTagProcessor.process(serviceTags);
Mockito.verify(tagStore).createTagDef(rTagDef);
Mockito.verify(tagStore).getServiceResourceByGuid(rserRes.getGuid());
Mockito.verify(tagStore).getTagsForResourceId(rserRes.getId());
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestServiceTagProcessor method test4processDelete.
@Test
public void test4processDelete() throws Exception {
serviceTags = new ServiceTags();
serviceTags.setOp(ServiceTags.OP_DELETE);
Map<Long, RangerTagDef> fd = new HashMap<>();
List<RangerTag> associatedTags = new ArrayList<>();
RangerTagDef rTagDef = Mockito.mock(RangerTagDef.class);
rTagDef.setGuid("guid");
rTagDef.setName("rTagDefName");
fd.put(1l, rTagDef);
serviceTags.setTagDefinitions(fd);
List<RangerServiceResource> serviceResources = new ArrayList<RangerServiceResource>();
RangerServiceResource rserRes = new RangerServiceResource();
testServiceRest = new TestServiceREST();
RangerPolicy rp = testServiceRest.rangerPolicy();
rserRes.setResourceElements(rp.getResources());
rserRes.setGuid("guId");
rserRes.setId(1L);
rserRes.setServiceName("serviceName1");
serviceResources.add(rserRes);
serviceTags.setServiceResources(serviceResources);
List<RangerTagResourceMap> tagResourceMaps = new ArrayList<RangerTagResourceMap>();
tagResourceMaps.add(new RangerTagResourceMap());
Map<Long, List<Long>> resourceToTagIds = new HashMap<>();
resourceToTagIds.put(1L, new ArrayList<Long>(Arrays.asList(22L, 1L, 0L)));
serviceTags.setResourceToTagIds(resourceToTagIds);
RangerTag rTag = new RangerTag();
rTag.setId(22L);
rTag.setType("type1");
rTag.setGuid("tagGuID");
Map<Long, RangerTag> tags = new HashMap<>();
rTag.setOwner((short) 0);
tags.put(1L, rTag);
serviceTags.setTags(tags);
RangerServiceResourceSignature serializer = new RangerServiceResourceSignature(rserRes);
String serviceResourceSignature = serializer.getSignature();
associatedTags.add(rTag);
TagStore tagStore = Mockito.mock(TagStore.class);
sTagProcessor = new ServiceTagsProcessor(tagStore);
Mockito.when(tagStore.getServiceResourceByGuid(rserRes.getGuid())).thenReturn(null);
Mockito.when(tagStore.getServiceResourceByServiceAndResourceSignature(rserRes.getServiceName(), serviceResourceSignature)).thenReturn(rserRes);
Mockito.when(tagStore.getTagResourceMapsForResourceGuid(rserRes.getGuid())).thenReturn(tagResourceMaps);
Mockito.doNothing().when(tagStore).deleteServiceResource(rserRes.getId());
Mockito.when(tagStore.getTagByGuid(rTag.getGuid())).thenReturn(rTag);
Mockito.doNothing().when(tagStore).deleteTag(rTag.getId());
Mockito.when(tagStore.getTagDefByGuid(rTagDef.getGuid())).thenReturn(rTagDef);
sTagProcessor.process(serviceTags);
Mockito.verify(tagStore).getServiceResourceByGuid(rserRes.getGuid());
Mockito.verify(tagStore).getServiceResourceByServiceAndResourceSignature(rserRes.getServiceName(), serviceResourceSignature);
Mockito.verify(tagStore).getTagResourceMapsForResourceGuid(rserRes.getGuid());
Mockito.verify(tagStore).deleteServiceResource(rserRes.getId());
Mockito.verify(tagStore).getTagByGuid(rTag.getGuid());
Mockito.verify(tagStore).deleteTag(rTag.getId());
Mockito.verify(tagStore).getTagDefByGuid(rTagDef.getGuid());
}
Aggregations