jerryshao commented on code in PR #7361:
URL: https://github.com/apache/gravitino/pull/7361#discussion_r2192022715
##########
api/src/main/java/org/apache/gravitino/policy/Policy.java:
##########
@@ -33,6 +36,127 @@
@Evolving
public interface Policy extends Auditable {
+ /**
+ * The prefix for built-in policy types. All built-in policy types should
start with this prefix.
+ */
+ String BUILT_IN_TYPE_PREFIX = "policy_";
+
+ /** The built-in policy types. Predefined policy types that are provided by
the system. */
+ enum BuiltInType {
+ // todo: add built-in policies, such as:
+ // DATA_COMPACTION(BUILT_IN_TYPE_PREFIX + "data_compaction", true, true,
+ // SUPPORTS_ALL_OBJECT_TYPES
+ // PolicyContent.DataCompactionContent.class)
+
+ /**
+ * Custom policy type. "custom" is a dummy type for custom policies, all
non-built-in types are
+ * custom types.
+ */
+ CUSTOM("custom", null, null, null, PolicyContent.CustomContent.class);
+
+ private final String policyType;
+ private final Boolean exclusive;
+ private final Boolean inheritable;
+ private final ImmutableSet<MetadataObject.Type> supportedObjectTypes;
+ private final Class<? extends PolicyContent> contentClass;
+
+ BuiltInType(
+ String policyType,
+ Boolean exclusive,
+ Boolean inheritable,
+ Set<MetadataObject.Type> supportedObjectTypes,
+ Class<? extends PolicyContent> contentClass) {
+ this.policyType = policyType;
+ this.exclusive = exclusive;
+ this.inheritable = inheritable;
+ this.supportedObjectTypes =
+ supportedObjectTypes == null
+ ? ImmutableSet.of()
+ : ImmutableSet.copyOf(supportedObjectTypes);
+ this.contentClass = contentClass;
+ }
+
+ /**
+ * Get the built-in policy type from the policy type string.
+ *
+ * @param policyType the policy type string
+ * @return the built-in policy type if it matches, otherwise returns
CUSTOM type
+ */
+ public static BuiltInType fromPolicyType(String policyType) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(policyType),
"policyType cannot be blank");
+ for (BuiltInType type : BuiltInType.values()) {
+ if (type.policyType.equalsIgnoreCase(policyType)) {
+ return type;
+ }
+ }
+
+ if (policyType.startsWith(BUILT_IN_TYPE_PREFIX)) {
+ throw new IllegalPolicyException("Unknown built-in policy type: %s",
policyType);
+ }
+
+ // If the policy type is not a built-in type, it is a custom type.
+ return CUSTOM;
+ }
+
+ /**
+ * Get the policy type string.
+ *
+ * @return the policy type string
+ */
+ public String policyType() {
+ return policyType;
+ }
+
+ /**
+ * Check if the policy is exclusive.
+ *
+ * @return true if the policy is exclusive, false otherwise
+ */
+ public Boolean exclusive() {
+ return exclusive;
+ }
+
+ /**
+ * Check if the policy is inheritable.
+ *
+ * @return true if the policy is inheritable, false otherwise
+ */
+ public Boolean inheritable() {
+ return inheritable;
+ }
+
+ /**
+ * Get the set of metadata object types that the policy can be associated
with.
+ *
+ * @return the set of metadata object types that the policy can be
associated with
+ */
+ public Set<MetadataObject.Type> supportedObjectTypes() {
+ return supportedObjectTypes;
+ }
+
+ /**
+ * Get the content class of the policy.
+ *
+ * @return the content class of the policy
+ */
+ public Class<? extends PolicyContent> contentClass() {
+ return contentClass;
+ }
+ }
+
+ /** The set of metadata object types that the policy can be applied to. */
+ Set<MetadataObject.Type> SUPPORTS_ALL_OBJECT_TYPES =
+ new HashSet<MetadataObject.Type>() {
+ {
+ add(MetadataObject.Type.CATALOG);
+ add(MetadataObject.Type.SCHEMA);
+ add(MetadataObject.Type.FILESET);
+ add(MetadataObject.Type.TABLE);
+ add(MetadataObject.Type.TOPIC);
+ add(MetadataObject.Type.MODEL);
+ }
+ };
Review Comment:
We'd better move it to the top of the interface if it is a static variable.
Also, you should make it immutable.
##########
api/src/main/java/org/apache/gravitino/policy/Policy.java:
##########
@@ -112,20 +236,48 @@ public interface Policy extends Auditable {
*
* @throws IllegalPolicyException if the policy is not valid.
*/
- void validate() throws IllegalPolicyException;
+ default void validate() throws IllegalPolicyException {
Review Comment:
Who will call this `validate()` method?
##########
api/src/main/java/org/apache/gravitino/policy/Policy.java:
##########
@@ -33,6 +36,127 @@
@Evolving
public interface Policy extends Auditable {
+ /**
+ * The prefix for built-in policy types. All built-in policy types should
start with this prefix.
+ */
+ String BUILT_IN_TYPE_PREFIX = "policy_";
Review Comment:
How about starting with "system_"?
##########
api/src/main/java/org/apache/gravitino/policy/PolicyContent.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.policy;
+
+import java.util.Map;
+import java.util.Objects;
+import org.apache.gravitino.exceptions.IllegalPolicyException;
+
+/** The interface of the content of the policy. */
+public interface PolicyContent {
+
+ /** @return The additional properties of the policy. */
+ Map<String, String> properties();
+
+ /**
+ * Validates the policy content.
+ *
+ * @throws IllegalPolicyException if the content is invalid.
+ */
+ void validate() throws IllegalPolicyException;
+
+ /**
+ * Creates a custom policy content with the given rules and properties.
+ *
+ * @param rules The custom rules of the policy.
+ * @param properties The additional properties of the policy.
+ * @return A new instance of {@link PolicyContent} with the specified rules
and properties.
+ */
+ static PolicyContent custom(Map<String, Object> rules, Map<String, String>
properties) {
+ return new CustomContent(rules, properties);
+ }
+
+ /**
+ * A custom content implementation of {@link PolicyContent} that holds
custom rules and
+ * properties.
+ */
+ class CustomContent implements PolicyContent {
+ private final Map<String, Object> customRules;
+ private final Map<String, String> properties;
+
+ /** Default constructor for Jackson deserialization only. */
+ private CustomContent() {
+ this(null, null);
+ }
+
+ /**
+ * Constructor for CustomContent.
+ *
+ * @param customRules the custom rules of the policy
+ * @param properties the additional properties of the policy
+ */
+ private CustomContent(Map<String, Object> customRules, Map<String, String>
properties) {
+ this.customRules = customRules;
+ this.properties = properties;
+ }
+
+ /**
+ * Returns the custom rules of the policy.
+ *
+ * @return a map of custom rules
+ */
+ public Map<String, Object> customRules() {
+ return customRules;
+ }
+
+ @Override
+ public Map<String, String> properties() {
+ return properties;
+ }
+
+ @Override
+ public void validate() throws IllegalPolicyException {
+ // nothing to validate for custom content
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof CustomContent)) return false;
+ CustomContent that = (CustomContent) o;
+ return Objects.equals(customRules, that.customRules)
+ && Objects.equals(properties, that.properties);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(customRules, properties);
+ }
Review Comment:
You'd also implement the `toString` method.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]