[AETHER-1295][AETHER-1294] First stab to the TOST APIs framework
and to the DROP policies.

Patch includes also some CLI commands. REST APIs will be implemented
in a separate review. Other review will follow to implement the logic
of the REDIRECT policies

Change-Id: I34aa3da700c5a16682196e4dd8db9c4757d609c4
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/AbstractPolicy.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/AbstractPolicy.java
new file mode 100644
index 0000000..4be54a7
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/AbstractPolicy.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+/**
+ * Abstract implementation of the policy interface.
+ */
+public abstract class AbstractPolicy implements Policy {
+    // Shared state among policies
+    protected PolicyId policyId;
+    private PolicyType policyType;
+
+    /**
+     * Init the basic information of a policy.
+     *
+     * @param pType the policy type
+     */
+    protected AbstractPolicy(PolicyType pType) {
+        policyType = pType;
+    }
+
+    @Override
+    public PolicyId policyId() {
+        return policyId;
+    }
+
+    @Override
+    public PolicyType policyType() {
+        return policyType;
+    }
+
+    protected abstract PolicyId computePolicyId();
+
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/DropPolicy.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/DropPolicy.java
new file mode 100644
index 0000000..ac2e05e
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/DropPolicy.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Implementation of the drop policy.
+ */
+public final class DropPolicy extends AbstractPolicy {
+    /**
+     * Builds up a DROP policy.
+     */
+    public DropPolicy() {
+        super(PolicyType.DROP);
+        policyId = computePolicyId();
+    }
+
+    @Override
+    protected PolicyId computePolicyId() {
+        return PolicyId.of(policyType().name());
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!(obj instanceof DropPolicy)) {
+            return false;
+        }
+        final DropPolicy other = (DropPolicy) obj;
+        return Objects.equals(policyType(), other.policyType()) &&
+                Objects.equals(policyId(), other.policyId());
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(policyId(), policyType());
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("policyId", policyId())
+                .add("policyType", policyType())
+                .toString();
+    }
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/Policy.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/Policy.java
new file mode 100644
index 0000000..bc1acf0
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/Policy.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+/**
+ * Represents a policy in TOST.
+ */
+public interface Policy {
+    /**
+     * Represents the type of a policy.
+     */
+    enum PolicyType {
+        /**
+         * The policy drops the associated traffic.
+         */
+        DROP,
+
+        /**
+         * The policy redirects traffic using custom routing.
+         */
+        REDIRECT
+    }
+
+    /**
+     * Returns the policy id.
+     *
+     * @return the policy id
+     */
+    PolicyId policyId();
+
+    /**
+     * Returns the policy type.
+     *
+     * @return the type of a policy
+     */
+    PolicyType policyType();
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyData.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyData.java
new file mode 100644
index 0000000..5e207ae
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyData.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import java.util.List;
+
+/**
+ * Policy data retrieved from the system.
+ */
+public final class PolicyData {
+    // We want to provide access to the policy data as well as
+    // the policy operations in the system and their status
+    private final PolicyState policyState;
+    private final Policy policy;
+    private final List<String> operations;
+
+    /**
+     * Creates a policy data.
+     *
+     * @param pState the policy state
+     * @param pol the policy
+     * @param ops the operations associated
+     */
+    public PolicyData(PolicyState pState, Policy pol, List<String> ops) {
+        policy = pol;
+        policyState = pState;
+        operations = ops;
+    }
+
+    /**
+     * Returns the current state of the policy.
+     *
+     * @return the policy state
+     */
+    public PolicyState policyState() {
+        return policyState;
+    }
+
+
+    /**
+     * Returns the policy associated.
+     *
+     * @return the policy
+     */
+    public Policy policy() {
+        return policy;
+    }
+
+    /**
+     * Returns the operations in the system in form of strings.
+     *
+     * @return the operations
+     */
+    public List<String> operations() {
+        return operations;
+    }
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyId.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyId.java
new file mode 100644
index 0000000..e3e0cc1
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyId.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import org.onlab.util.Identifier;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a policy id.
+ */
+public final class PolicyId extends Identifier<String> {
+
+    protected PolicyId(String id) {
+        super(id);
+    }
+
+    /**
+     * Returns the id of the policy given the value.
+     *
+     * @param name policy id value
+     * @return policy id
+     */
+    public static PolicyId of(String name) {
+        checkNotNull(name);
+        checkArgument(!name.isEmpty(), "Name cannot be empty");
+        return new PolicyId(name);
+    }
+
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyService.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyService.java
new file mode 100644
index 0000000..2e188ab
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyService.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import java.util.Set;
+
+/**
+ * Policies to drop, reroute, apply QoS and overlay the traffic.
+ */
+public interface PolicyService {
+    /**
+     * Traffic match priority.
+     */
+    int TRAFFIC_MATCH_PRIORITY = 60000;
+
+    /**
+     * Creates or updates a policy.
+     *
+     * @param policy the policy to create
+     * @return the id of the policy being created. Otherwise null.
+     */
+    PolicyId addOrUpdatePolicy(Policy policy);
+
+    /**
+     * Issues a policy removal.
+     *
+     * @param policyId the id of the policy to remove
+     * @return whether or not the operation was successful
+     */
+    boolean removePolicy(PolicyId policyId);
+
+    /**
+     * Returns a set of policies. The policy types can be used
+     * as filter.
+     *
+     * @param filter the policy types
+     * @return the policies stored in the system observing
+     * the filtering rule
+     */
+    Set<PolicyData> policies(Set<Policy.PolicyType> filter);
+
+    /**
+     * Attaches a traffic match to a policy.
+     *
+     * @param trafficMatch the traffic match
+     * @return the traffic match id or null if not successful
+     */
+    TrafficMatchId addOrUpdateTrafficMatch(TrafficMatch trafficMatch);
+
+    /**
+     * Issues a traffic match removal.
+     *
+     * @param trafficMatchId the id of the traffic match to remove
+     * @return whether or not the operation was successful
+     */
+    boolean removeTrafficMatch(TrafficMatchId trafficMatchId);
+
+    /**
+     * Returns a set of traffic matches.
+     *
+     * @return the traffic matches stored in the system
+     */
+    Set<TrafficMatchData> trafficMatches();
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyState.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyState.java
new file mode 100644
index 0000000..ad767ec
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/PolicyState.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+/**
+ * Represents the state of a policy as seen by the system.
+ */
+public enum PolicyState {
+    /**
+     * The policy is in the process of being added.
+     */
+    PENDING_ADD,
+
+    /**
+     * The policy has been added.
+     */
+    ADDED,
+
+    /**
+     * The policy is in the process of being removed.
+     */
+    PENDING_REMOVE
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatch.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatch.java
new file mode 100644
index 0000000..83098fe
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatch.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+
+import com.google.common.hash.Funnel;
+import com.google.common.hash.HashCode;
+import com.google.common.hash.HashFunction;
+import com.google.common.hash.Hashing;
+import org.onosproject.net.flow.TrafficSelector;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Representation of a traffic match.
+ */
+public final class TrafficMatch {
+    // Traffic match internal state
+    private TrafficMatchId trafficMatchId;
+    private TrafficSelector trafficSelector;
+    private PolicyId policyId;
+
+    /**
+     * Builds a traffic match.
+     *
+     * @param trafficselector the traffic selector
+     * @param policyid the associated policy id
+     */
+    public TrafficMatch(TrafficSelector trafficselector, PolicyId policyid) {
+        trafficSelector = trafficselector;
+        trafficMatchId = TrafficMatchId.trafficMatchId(computeTrafficMatchId());
+        policyId = policyid;
+    }
+
+    /**
+     * Returns the traffic match id.
+     *
+     * @return the id of the traffic match
+     */
+    public TrafficMatchId trafficMatchId() {
+        return trafficMatchId;
+    }
+
+    /**
+     * Returns the id of the policy associated with.
+     *
+     * @return the policy id
+     */
+    public PolicyId policyId() {
+        return policyId;
+    }
+
+    /**
+     * Returns the traffic selector associated with.
+     *
+     * @return the traffic selector
+     */
+    public TrafficSelector trafficSelector() {
+        return trafficSelector;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(trafficMatchId, trafficSelector, policyId);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof TrafficMatch) {
+            final TrafficMatch other = (TrafficMatch) obj;
+            return Objects.equals(this.trafficMatchId, other.trafficMatchId) &&
+                    Objects.equals(trafficSelector, other.trafficSelector) &&
+                    Objects.equals(policyId, other.policyId);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("trafficMatchId", trafficMatchId)
+                .add("trafficSelector", trafficSelector)
+                .add("policyId", policyId)
+                .toString();
+    }
+
+    // Compute the id using the traffic selector. This method results to be consistent across the cluster.
+    private int computeTrafficMatchId() {
+        Funnel<TrafficSelector> selectorFunnel = (from, into) -> from.criteria()
+                .forEach(c -> into.putUnencodedChars(c.toString()));
+        HashFunction hashFunction = Hashing.murmur3_32();
+        HashCode hashCode = hashFunction.newHasher()
+                .putObject(trafficSelector, selectorFunnel)
+                .hash();
+        return hashCode.asInt();
+    }
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchData.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchData.java
new file mode 100644
index 0000000..3932b4c
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchData.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import java.util.List;
+
+/**
+ * Traffic match data retrieved from the system.
+ */
+public final class TrafficMatchData {
+    // We want to provide access to the traffic match data as well as
+    // the traffic match operations in the system and their status
+    private final TrafficMatchState trafficMatchState;
+    private final TrafficMatch trafficMatch;
+    private final List<String> operations;
+
+
+    public TrafficMatchData(TrafficMatchState tState, TrafficMatch tMatch, List<String> ops) {
+        trafficMatch = tMatch;
+        trafficMatchState = tState;
+        operations = ops;
+    }
+
+    /**
+     * Returns the current state of the traffic match.
+     *
+     * @return the traffic match state
+     */
+    public TrafficMatchState trafficMatchState() {
+        return trafficMatchState;
+    }
+
+
+    /**
+     * Returns the traffic match associated.
+     *
+     * @return the traffic match
+     */
+    public TrafficMatch trafficMatch() {
+        return trafficMatch;
+    }
+
+    /**
+     * Returns the operations in the system in form of strings.
+     *
+     * @return the operations
+     */
+    public List<String> operations() {
+        return operations;
+    }
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchId.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchId.java
new file mode 100644
index 0000000..5833f66
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchId.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+import org.onlab.util.Identifier;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Representation of a traffic match id.
+ */
+public final class TrafficMatchId extends Identifier<Integer> {
+
+    protected TrafficMatchId(int value) {
+        super(value);
+    }
+
+    /**
+     * Converts an int into a traffic match id.
+     *
+     * @param value the value of the id
+     * @return the traffic match id
+     */
+    public static TrafficMatchId trafficMatchId(int value) {
+        return new TrafficMatchId(value);
+    }
+
+    /**
+     * Returns the id of the traffic match given the value.
+     *
+     * @param name traffic match id value
+     * @return traffic match id
+     */
+    public static TrafficMatchId of(String name) {
+        checkNotNull(name);
+        checkArgument(!name.isEmpty(), "Name cannot be empty");
+        return new TrafficMatchId(Integer.parseInt(name));
+    }
+
+    @Override
+    public String toString() {
+        return String.valueOf(this.identifier);
+    }
+
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchState.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchState.java
new file mode 100644
index 0000000..f775c50
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/TrafficMatchState.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * Licensed 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.onosproject.segmentrouting.policy.api;
+
+/**
+ * Represents the state of a traffic match as seen by the system.
+ */
+public enum TrafficMatchState {
+    /**
+     * The traffic match is in the process of being added.
+     */
+    PENDING_ADD,
+
+    /**
+     * The traffic match has been added.
+     */
+    ADDED,
+
+    /**
+     * The traffic match is in the process of being removed.
+     */
+    PENDING_REMOVE
+}
diff --git a/impl/src/main/java/org/onosproject/segmentrouting/policy/api/package-info.java b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/package-info.java
new file mode 100644
index 0000000..cfbaa28
--- /dev/null
+++ b/impl/src/main/java/org/onosproject/segmentrouting/policy/api/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed 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.
+ */
+
+/**
+ * Policy API.
+ */
+package org.onosproject.segmentrouting.policy.api;
\ No newline at end of file