Search in sources :

Example 6 with RangerTagDef

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;
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) ArrayList(java.util.ArrayList)

Example 7 with RangerTagDef

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;
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) RangerAtlasEntity(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity) RangerAtlasEntityWithTags(org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntityWithTags) HashMap(java.util.HashMap) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTagAttributeDef(org.apache.ranger.plugin.model.RangerTagDef.RangerTagAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with RangerTagDef

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;
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RangerTagAttributeDef(org.apache.ranger.plugin.model.RangerTagDef.RangerTagAttributeDef)

Example 9 with RangerTagDef

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());
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ArrayList(java.util.ArrayList) TagStore(org.apache.ranger.plugin.store.TagStore) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 10 with RangerTagDef

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());
}
Also used : RangerTagDef(org.apache.ranger.plugin.model.RangerTagDef) HashMap(java.util.HashMap) RangerServiceResource(org.apache.ranger.plugin.model.RangerServiceResource) ArrayList(java.util.ArrayList) RangerServiceResourceSignature(org.apache.ranger.plugin.store.RangerServiceResourceSignature) TagStore(org.apache.ranger.plugin.store.TagStore) RangerPolicy(org.apache.ranger.plugin.model.RangerPolicy) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerTag(org.apache.ranger.plugin.model.RangerTag) RangerTagResourceMap(org.apache.ranger.plugin.model.RangerTagResourceMap) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

RangerTagDef (org.apache.ranger.plugin.model.RangerTagDef)39 Test (org.junit.Test)24 ArrayList (java.util.ArrayList)15 XXTagDef (org.apache.ranger.entity.XXTagDef)14 HashMap (java.util.HashMap)10 RangerTag (org.apache.ranger.plugin.model.RangerTag)10 WebApplicationException (javax.ws.rs.WebApplicationException)9 RangerServiceResource (org.apache.ranger.plugin.model.RangerServiceResource)9 ExpectedException (org.junit.rules.ExpectedException)9 XXTagDefDao (org.apache.ranger.db.XXTagDefDao)7 ServiceTags (org.apache.ranger.plugin.util.ServiceTags)6 List (java.util.List)5 Map (java.util.Map)5 XXTagAttributeDef (org.apache.ranger.entity.XXTagAttributeDef)5 Date (java.util.Date)4 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)4 RangerTagResourceMap (org.apache.ranger.plugin.model.RangerTagResourceMap)4 TagStore (org.apache.ranger.plugin.store.TagStore)4 Predicate (org.apache.commons.collections.Predicate)3 RangerServiceResourceSignature (org.apache.ranger.plugin.store.RangerServiceResourceSignature)3