@Override public PolicyEntity update(PolicyEntity policyProto) { Preconditions.checkNotNull(policyProto, "Entity should not be null"); Preconditions.checkNotNull(policyProto.getUuid(), "uuid should not be null"); PolicyEntity current = getByUUIDorName(policyProto.getUuid(), null); if (policyProto.getName() != null) { current.setName(policyProto.getName()); } if (policyProto.getAlertPublishmentIds() != null) { current.setAlertPublishmentIds(policyProto.getAlertPublishmentIds()); } if (policyProto.getDefinition() != null) { current.setDefinition(policyProto.getDefinition()); } current.ensureDefault(); try { if (!queryService.execute(updateSqlByUUID, current, policyEntityWriter)) { throw new IllegalArgumentException("Failed to update policyProto"); } } catch (SQLException e) { LOGGER.error("Error to execute {}: {}", updateSqlByUUID, policyProto, e); throw new IllegalArgumentException("SQL execution error: " + e.getMessage(), e); } return current; }
@Override public PolicyEntity create(PolicyEntity entity) { Preconditions.checkNotNull(entity, "PolicyEntity should not be null"); entity.ensureDefault(); try { int retCode = queryService.insert(insertSql, Collections.singletonList(entity), policyEntityWriter); if (retCode > 0) { return entity; } else { throw new SQLException("Insertion result: " + retCode); } } catch (SQLException e) { LOGGER.error("Error to insert entity {} (entity: {}): {}", insertSql, entity.toString(), e.getMessage(), e); throw new IllegalArgumentException("SQL execution error:" + e.getMessage(), e); } }
@Override public PolicyEntity create(PolicyEntity entity) { Preconditions.checkNotNull(entity, "entity is null: " + entity); entity.ensureDefault(); policyProtoMap.put(entity.getUuid(), entity); return entity; }
default PolicyEntity createOrUpdatePolicyProto(PolicyEntity policyProto) { Preconditions.checkNotNull(policyProto, "PolicyProto should not be null"); Preconditions.checkNotNull(policyProto.getDefinition(), "PolicyProto definition should not be null"); if (policyProto.getName() == null) { policyProto.setName(String.format("[%s]%s", policyProto.getDefinition().getAlertCategory(), policyProto.getDefinition().getName())); } if (policyProto.getUuid() == null) { return create(policyProto); } else { return update(policyProto); } }
@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); 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("/create") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public RESTResponse<PolicyEntity> saveAsPolicyProto(PolicyEntity policyEntity, @QueryParam("needPolicyProtoCreated") boolean needPolicyProtoCreated) { return RESTResponse.async(() -> { Preconditions.checkNotNull(policyEntity, "entity should not be null"); Preconditions.checkNotNull(policyEntity.getDefinition(), "policy definition should not be null"); Preconditions.checkNotNull(policyEntity.getAlertPublishmentIds(), "alert publisher list should not be null"); PolicyDefinition policyDefinition = policyEntity.getDefinition(); checkOutputStream(policyDefinition.getInputStreams(), policyDefinition.getOutputStreams()); 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); } if (needPolicyProtoCreated) { importPolicyProto(policyEntity); } return policyEntity; }).get(); }
private PolicyEntity importPolicyProto(PolicyEntity policyEntity) { PolicyDefinition policyDefinition = policyEntity.getDefinition(); checkOutputStream(policyDefinition.getInputStreams(), policyDefinition.getOutputStreams()); 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); policyDefinition.getPartitionSpec().clear(); policyEntity.setDefinition(policyDefinition); return policyEntityService.createOrUpdatePolicyProto(policyEntity); }
@Override public PolicyEntity update(PolicyEntity policyEntity) { Preconditions.checkNotNull(policyEntity, "entity is null: " + policyEntity); Preconditions.checkNotNull(policyEntity.getUuid(), "uuid is null: " + policyEntity.getUuid()); policyProtoMap.put(policyEntity.getUuid(), policyEntity); return policyEntity; } }
@Override 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 public String toString() { return String.format("{name=\"%s\",definition=%s}", this.name, this.getDefinition() == null ? "null" : this.getDefinition().getDefinition().toString()); }
@Override public boolean equals(Object that) { if (that == this) { return true; } if (!(that instanceof PolicyEntity)) { return false; } PolicyEntity another = (PolicyEntity) that; return Objects.equals(another.name, this.name) && Objects.equals(another.definition, this.definition) && CollectionUtils.isEqualCollection(another.getAlertPublishmentIds(), alertPublishmentIds); }
PolicyDefinition policyDefinition = policyProto.getDefinition(); List<String> inputStreams = new ArrayList<>(); String newDefinition = policyDefinition.getDefinition().getValue(); policyDefinition.getDefinition().setValue(newDefinition); policyDefinition.setSiteId(site); policyDefinition.setName(PolicyIdConversions.generateUniquePolicyId(site, policyProto.getDefinition().getName())); throw new IllegalArgumentException("fail to create policy: " + result.message); if (policyProto.getAlertPublishmentIds() != null && !policyProto.getAlertPublishmentIds().isEmpty()) { result = metadataResource.addPublishmentsToPolicy(policyDefinition.getName(), policyProto.getAlertPublishmentIds()); if (result.code != 200) { throw new IllegalArgumentException("fail to create policy publisherments: " + result.message);