eagle-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From qingwz...@apache.org
Subject eagle git commit: [EAGLE-1055] update policy proto APIs
Date Mon, 10 Jul 2017 08:04:54 GMT
Repository: eagle
Updated Branches:
  refs/heads/master 8c738eada -> d0f07ae3e


[EAGLE-1055] update policy proto APIs

https://issues.apache.org/jira/browse/EAGLE-1055

Author: Zhao, Qingwen <qingwzhao@apache.org>

Closes #958 from qingwen220/EAGLE-1055.


Project: http://git-wip-us.apache.org/repos/asf/eagle/repo
Commit: http://git-wip-us.apache.org/repos/asf/eagle/commit/d0f07ae3
Tree: http://git-wip-us.apache.org/repos/asf/eagle/tree/d0f07ae3
Diff: http://git-wip-us.apache.org/repos/asf/eagle/diff/d0f07ae3

Branch: refs/heads/master
Commit: d0f07ae3e41688b82e8e1d55aef60caa77beee4e
Parents: 8c738ea
Author: Zhao, Qingwen <qingwzhao@apache.org>
Authored: Mon Jul 10 16:04:50 2017 +0800
Committer: Zhao, Qingwen <qingwzhao@apache.org>
Committed: Mon Jul 10 16:04:50 2017 +0800

----------------------------------------------------------------------
 .../eagle/metadata/resource/PolicyResource.java | 114 +++++++++++++++----
 .../metadata/service/PolicyEntityService.java   |   2 +-
 .../memory/PolicyEntityServiceMemoryImpl.java   |  11 +-
 .../TestPolicyEntityServiceMemoryImpl.java      |   6 +-
 .../service/PolicyEntityServiceJDBCImpl.java    |  22 ++--
 .../jdbc/PolicyEntityServiceJDBCImplTest.java   |   4 +-
 6 files changed, 121 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/PolicyResource.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/PolicyResource.java
b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/PolicyResource.java
index ce900fd..3b3dba9 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/PolicyResource.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/resource/PolicyResource.java
@@ -20,6 +20,7 @@ package org.apache.eagle.metadata.resource;
 import com.google.common.base.Preconditions;
 import com.google.inject.Inject;
 import org.apache.eagle.alert.engine.coordinator.PolicyDefinition;
+import org.apache.eagle.alert.engine.coordinator.Publishment;
 import org.apache.eagle.alert.engine.interpreter.PolicyValidationResult;
 import org.apache.eagle.alert.metadata.resource.OpResult;
 import org.apache.eagle.common.rest.RESTResponse;
@@ -60,37 +61,85 @@ public class PolicyResource {
     }
 
     @POST
-    @Path("/import")
+    @Path("/create")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public RESTResponse<PolicyEntity> saveAsPolicyProto(PolicyEntity policyEntity)
{
-        Preconditions.checkNotNull(policyEntity, "policyProto should not be null");
-        Preconditions.checkNotNull(policyEntity.getDefinition(), "policyDefinition should
not be null");
-        Preconditions.checkNotNull(policyEntity.getAlertPublishmentIds(), "alert publisher
list should not be null");
+    public RESTResponse<PolicyEntity> saveAsPolicyProto(PolicyEntity policyEntity,
+                                                        @QueryParam("needPolicyCreated")
boolean needPolicyCreated) {
+        return RESTResponse.async(() -> {
+            Preconditions.checkNotNull(policyEntity, "entity should not be null");
+            Preconditions.checkNotNull(policyEntity, "policy definition should not be null");
+            Preconditions.checkNotNull(policyEntity.getAlertPublishmentIds(), "alert publisher
list should not be null");
 
-        PolicyDefinition policyDefinition = policyEntity.getDefinition();
-        List<String> inputStreamType = new ArrayList<>();
-        String newDefinition = policyDefinition.getDefinition().getValue();
-        for (String inputStream : policyDefinition.getInputStreams()) {
-            String streamDef = StreamIdConversions.parseStreamTypeId(policyDefinition.getSiteId(),
inputStream);
-            inputStreamType.add(streamDef);
-            newDefinition = newDefinition.replaceAll(inputStream, streamDef);
-        }
-        policyDefinition.setInputStreams(inputStreamType);
-        policyDefinition.getDefinition().setValue(newDefinition);
-        policyDefinition.setName(PolicyIdConversions.parsePolicyId(policyDefinition.getSiteId(),
policyDefinition.getName()));
-        policyDefinition.setSiteId(null);
-        policyEntity.setDefinition(policyDefinition);
+            PolicyDefinition policyDefinition = policyEntity.getDefinition();
+            if (needPolicyCreated) {
+                OpResult result = metadataResource.addPolicy(policyDefinition);
+                if (result.code != 200) {
+                    throw new IllegalArgumentException(result.message);
+                }
+                result = metadataResource.addPublishmentsToPolicy(policyDefinition.getName(),
policyEntity.getAlertPublishmentIds());
+                if (result.code != 200) {
+                    throw new IllegalArgumentException(result.message);
+                }
+            }
+            return importPolicyProto(policyEntity);
+        }).get();
+    }
+
+    @POST
+    @Path("/create/{policyId}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public RESTResponse<PolicyEntity> saveAsPolicyProto(@PathParam("policyId") String
policyId) {
+        return RESTResponse.async(() -> {
+            Preconditions.checkNotNull(policyId, "policyId should not be null");
+            PolicyDefinition policyDefinition = metadataResource.getPolicyById(policyId);
+
+            if (policyDefinition == null) {
+                throw new IllegalArgumentException("policy does not exist: " + policyId);
+            }
+
+            PolicyEntity policyEntity = new PolicyEntity();
+            policyEntity.setDefinition(policyDefinition);
 
-        return createOrUpdatePolicyProto(policyEntity);
+            List<Publishment> alertPublishments = metadataResource.getPolicyPublishments(policyId);
+            if (alertPublishments != null && !alertPublishments.isEmpty()){
+                List<String> alertPublisherIds = new ArrayList<>();
+                for (Publishment publishment : alertPublishments) {
+                    alertPublisherIds.add(publishment.getName());
+                }
+                policyEntity.setAlertPublishmentIds(alertPublisherIds);
+            }
+            return importPolicyProto(policyEntity);
+        }).get();
     }
 
     @POST
     @Path("/export/{site}")
     @Consumes(MediaType.APPLICATION_JSON)
     @Produces(MediaType.APPLICATION_JSON)
-    public RESTResponse<List<PolicyDefinition>> loadPolicyDefinition(List<PolicyEntity>
policyProtoList, @PathParam("site") String site) {
-        return RESTResponse.async(() -> exportPolicyDefinition(policyProtoList, site)).get();
+    public RESTResponse<Boolean> loadPoliciesByProto(List<PolicyEntity> policyProtoList,
@PathParam("site") String site) {
+        return RESTResponse.async(() -> exportPolicyProto(policyProtoList, site)).get();
+    }
+
+    @POST
+    @Path("/exportByName/{site}")
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public RESTResponse<Boolean> loadPoliciesByName(List<String> policyProtoList,
@PathParam("site") String site) {
+        return RESTResponse.async(() -> {
+            if (policyProtoList == null || policyProtoList.isEmpty()) {
+                throw new IllegalArgumentException("policyProtoList is null or empty");
+            }
+            List<PolicyEntity> policyEntities = new ArrayList<PolicyEntity>();
+            for (String name : policyProtoList) {
+                PolicyEntity entity = policyEntityService.getByUUIDorName(null, name);
+                if (entity != null) {
+                    policyEntities.add(entity);
+                }
+            }
+            return exportPolicyProto(policyEntities, site);
+        }).get();
     }
 
     @DELETE
@@ -101,12 +150,28 @@ public class PolicyResource {
         return RESTResponse.async(() -> policyEntityService.deletePolicyProtoByUUID(uuid)).get();
     }
 
-    private List<PolicyDefinition> exportPolicyDefinition(List<PolicyEntity>
policyProtoList, String site) {
+    private PolicyEntity importPolicyProto(PolicyEntity policyEntity) {
+        PolicyDefinition policyDefinition = policyEntity.getDefinition();
+        List<String> inputStreamType = new ArrayList<>();
+        String newDefinition = policyDefinition.getDefinition().getValue();
+        for (String inputStream : policyDefinition.getInputStreams()) {
+            String streamDef = StreamIdConversions.parseStreamTypeId(policyDefinition.getSiteId(),
inputStream);
+            inputStreamType.add(streamDef);
+            newDefinition = newDefinition.replaceAll(inputStream, streamDef);
+        }
+        policyDefinition.setInputStreams(inputStreamType);
+        policyDefinition.getDefinition().setValue(newDefinition);
+        policyDefinition.setName(PolicyIdConversions.parsePolicyId(policyDefinition.getSiteId(),
policyDefinition.getName()));
+        policyDefinition.setSiteId(null);
+        policyEntity.setDefinition(policyDefinition);
+        return policyEntityService.createOrUpdatePolicyProto(policyEntity);
+    }
+
+    private Boolean exportPolicyProto(List<PolicyEntity> policyProtoList, String site)
{
         Preconditions.checkNotNull(site, "site should not be null");
         if (policyProtoList == null || policyProtoList.isEmpty()) {
             throw new IllegalArgumentException("policy prototype list is empty or null");
         }
-        List<PolicyDefinition> policies = new ArrayList<>();
         for (PolicyEntity policyProto : policyProtoList) {
             PolicyDefinition policyDefinition = policyProto.getDefinition();
             List<String> inputStreams = new ArrayList<>();
@@ -134,9 +199,8 @@ public class PolicyResource {
                     throw new IllegalArgumentException("fail to create policy publisherments:
" + result.message);
                 }
             }
-            policies.add(policyDefinition);
         }
-        return policies;
+        return true;
     }
 
 }

http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/PolicyEntityService.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/PolicyEntityService.java
b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/PolicyEntityService.java
index 3e03251..3cf6611 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/PolicyEntityService.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/PolicyEntityService.java
@@ -27,7 +27,7 @@ public interface PolicyEntityService {
 
     Collection<PolicyEntity> getAllPolicyProto();
 
-    PolicyEntity getPolicyProtoByUUID(String uuid);
+    PolicyEntity getByUUIDorName(String uuid, String name);
 
     boolean deletePolicyProtoByUUID(String uuid);
 

http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/memory/PolicyEntityServiceMemoryImpl.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/memory/PolicyEntityServiceMemoryImpl.java
b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/memory/PolicyEntityServiceMemoryImpl.java
index 511b648..d4ce85a 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/memory/PolicyEntityServiceMemoryImpl.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/main/java/org/apache/eagle/metadata/service/memory/PolicyEntityServiceMemoryImpl.java
@@ -18,6 +18,7 @@
 package org.apache.eagle.metadata.service.memory;
 
 import com.google.common.base.Preconditions;
+import org.apache.eagle.metadata.exceptions.EntityNotFoundException;
 import org.apache.eagle.metadata.model.PolicyEntity;
 import org.apache.eagle.metadata.service.PolicyEntityService;
 
@@ -33,8 +34,14 @@ public class PolicyEntityServiceMemoryImpl implements PolicyEntityService
{
     }
 
     @Override
-    public PolicyEntity getPolicyProtoByUUID(String uuid) {
-        return policyProtoMap.get(uuid);
+    public PolicyEntity getByUUIDorName(String uuid, String name) {
+        Preconditions.checkArgument(uuid != null || name != null, "Both uuid and name are
null");
+        if (uuid != null) {
+            return policyProtoMap.get(uuid);
+        } else {
+            return policyProtoMap.values().stream().filter(o -> o.getName().equals(name)).findAny()
+                    .orElseThrow(() -> new IllegalArgumentException("Policy proto named
" + name + " is not found"));
+        }
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-base/src/test/java/org/apache/eagle/metadata/service/TestPolicyEntityServiceMemoryImpl.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-base/src/test/java/org/apache/eagle/metadata/service/TestPolicyEntityServiceMemoryImpl.java
b/eagle-core/eagle-metadata/eagle-metadata-base/src/test/java/org/apache/eagle/metadata/service/TestPolicyEntityServiceMemoryImpl.java
index bfcc1cd..0680c5d 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-base/src/test/java/org/apache/eagle/metadata/service/TestPolicyEntityServiceMemoryImpl.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-base/src/test/java/org/apache/eagle/metadata/service/TestPolicyEntityServiceMemoryImpl.java
@@ -45,6 +45,7 @@ public class TestPolicyEntityServiceMemoryImpl {
         // define publisher list
         List<String> alertPublisherIds = Arrays.asList("slack");
 
+        // create
         PolicyEntity policyEntity = new PolicyEntity();
         policyEntity.setDefinition(policyDefinition);
         policyEntity.setAlertPublishmentIds(alertPublisherIds);
@@ -55,7 +56,10 @@ public class TestPolicyEntityServiceMemoryImpl {
         Collection<PolicyEntity> policies =  policyEntityService.getAllPolicyProto();
         Assert.assertTrue(policies.size() == 1);
 
-        PolicyEntity entity = policyEntityService.getPolicyProtoByUUID(policies.iterator().next().getUuid());
+        PolicyEntity entity = policyEntityService.getByUUIDorName(policies.iterator().next().getUuid(),
null);
+        Assert.assertTrue(entity.equals(policies.iterator().next()));
+
+        entity = policyEntityService.getByUUIDorName(null, "[null]policy1");
         Assert.assertTrue(entity.equals(policies.iterator().next()));
 
         // test update

http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/main/java/org/apache/eagle/metadata/store/jdbc/service/PolicyEntityServiceJDBCImpl.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/main/java/org/apache/eagle/metadata/store/jdbc/service/PolicyEntityServiceJDBCImpl.java
b/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/main/java/org/apache/eagle/metadata/store/jdbc/service/PolicyEntityServiceJDBCImpl.java
index 65e3757..ac6aed8 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/main/java/org/apache/eagle/metadata/store/jdbc/service/PolicyEntityServiceJDBCImpl.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/main/java/org/apache/eagle/metadata/store/jdbc/service/PolicyEntityServiceJDBCImpl.java
@@ -42,9 +42,10 @@ public class PolicyEntityServiceJDBCImpl implements PolicyEntityService
{
     private static final Logger LOGGER = LoggerFactory.getLogger(PolicyEntityServiceJDBCImpl.class);
 
     private static final String selectSql = "SELECT * FROM policy_prototype";
-    private static final String queryByUUID = "SELECT * FROM policy_prototype where uuid
= '%s'";
-    private static final String deleteSqlByUUID = "DELETE FROM policy_prototype where uuid
= '%s'";
-    private static final String updateSqlByUUID = "UPDATE policy_prototype SET name = ?,
definition = ? , alertPublisherIds = ? , createdtime = ? , modifiedtime = ?  where uuid =
?";
+    private static final String queryByUUID = "SELECT * FROM policy_prototype WHERE uuid
= '%s'";
+    private static final String queryByUUIDorName = "SELECT * FROM policy_prototype WHERE
uuid = ? or name = ?";
+    private static final String deleteSqlByUUID = "DELETE FROM policy_prototype WHERE uuid
= '%s'";
+    private static final String updateSqlByUUID = "UPDATE policy_prototype SET name = ?,
definition = ? , alertPublisherIds = ? , createdtime = ? , modifiedtime = ?  WHERE uuid =
?";
     private static final String insertSql = "INSERT INTO policy_prototype (name, definition,
alertPublisherIds, createdtime, modifiedtime, uuid) VALUES (?, ?, ?, ?, ?, ?)";
 
     private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
@@ -62,11 +63,16 @@ public class PolicyEntityServiceJDBCImpl implements PolicyEntityService
{
     }
 
     @Override
-    public PolicyEntity getPolicyProtoByUUID(String uuid) {
-        Preconditions.checkNotNull(uuid, "uuid should not be null");
+    public PolicyEntity getByUUIDorName(String uuid, String name) {
+        Preconditions.checkArgument(uuid != null || name != null, "Both uuid and name are
null");
         try {
-            return queryService.query(String.format(queryByUUID, uuid), policyEntityMapper).stream()
-                    .findAny().orElseThrow(() -> new EntityNotFoundException("policyProto
is not found by uuid"));
+            return queryService.queryWithCond(queryByUUIDorName, o -> {
+                o.setString(1, uuid);
+                o.setString(2, name);
+            }, policyEntityMapper)
+                    .stream()
+                    .findAny()
+                    .orElseThrow(() -> new EntityNotFoundException("policyProto is not
found by uuid or name"));
         } catch (Exception e) {
             throw new IllegalArgumentException(e.getMessage(), e);
         }
@@ -87,7 +93,7 @@ public class PolicyEntityServiceJDBCImpl implements PolicyEntityService
{
     public PolicyEntity update(PolicyEntity policyProto) {
         Preconditions.checkNotNull(policyProto, "Entity should not be null");
         Preconditions.checkNotNull(policyProto.getUuid(), "uuid should not be null");
-        PolicyEntity current = getPolicyProtoByUUID(policyProto.getUuid());
+        PolicyEntity current = getByUUIDorName(policyProto.getUuid(), null);
 
         if (policyProto.getName() != null) {
             current.setName(policyProto.getName());

http://git-wip-us.apache.org/repos/asf/eagle/blob/d0f07ae3/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/test/java/org/apache/eagle/metadata/store/jdbc/PolicyEntityServiceJDBCImplTest.java
----------------------------------------------------------------------
diff --git a/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/test/java/org/apache/eagle/metadata/store/jdbc/PolicyEntityServiceJDBCImplTest.java
b/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/test/java/org/apache/eagle/metadata/store/jdbc/PolicyEntityServiceJDBCImplTest.java
index d68635e..0492bb1 100644
--- a/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/test/java/org/apache/eagle/metadata/store/jdbc/PolicyEntityServiceJDBCImplTest.java
+++ b/eagle-core/eagle-metadata/eagle-metadata-jdbc/src/test/java/org/apache/eagle/metadata/store/jdbc/PolicyEntityServiceJDBCImplTest.java
@@ -58,7 +58,9 @@ public class PolicyEntityServiceJDBCImplTest extends JDBCMetadataTestBase
{
         Collection<PolicyEntity> policies =  policyEntityService.getAllPolicyProto();
         Assert.assertTrue(policies.size() == 1);
 
-        PolicyEntity entity = policyEntityService.getPolicyProtoByUUID(policies.iterator().next().getUuid());
+        PolicyEntity entity = policyEntityService.getByUUIDorName(policies.iterator().next().getUuid(),
null);
+        Assert.assertTrue(entity.equals(policies.iterator().next()));
+        entity = policyEntityService.getByUUIDorName(null, "[null]policy1");
         Assert.assertTrue(entity.equals(policies.iterator().next()));
 
         // test update


Mime
View raw message