Introduce Pi Register model to PI Framework

Change-Id: I7fae87d5b5ed5fff34b3addfc148cee6fc98137c
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/PiRegisterProgrammable.java b/core/api/src/main/java/org/onosproject/net/behaviour/PiRegisterProgrammable.java
new file mode 100644
index 0000000..9a8a6b0
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/PiRegisterProgrammable.java
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.net.behaviour;
+
+import com.google.common.annotations.Beta;
+import org.onosproject.net.driver.HandlerBehaviour;
+import org.onosproject.net.pi.model.PiRegisterId;
+import org.onosproject.net.pi.runtime.PiRegisterCell;
+import org.onosproject.net.pi.runtime.PiRegisterCellId;
+
+import java.util.Collection;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Protocol-independent register programmable device behaviour.
+ */
+@Beta
+public interface PiRegisterProgrammable extends HandlerBehaviour {
+
+    /**
+     * Write a register cell of this device.
+     *
+     * @param entry register entry
+     * @return Completable future with result of the operation
+     */
+    CompletableFuture<Boolean> writeRegisterCell(PiRegisterCell entry);
+
+    /**
+     * Gets the register cell given the register cell ID from the device.
+     *
+     * @param cellId the register cell Id
+     * @return completable future with the register cell
+     */
+    CompletableFuture<PiRegisterCell> readRegisterCell(PiRegisterCellId cellId);
+
+    /**
+     * Gets the register cells given the collection of cellIds from the device.
+     *
+     * @param cellIds the collection of register cell Id
+     * @return completable future with the collection of register cells
+     */
+    CompletableFuture<Collection<PiRegisterCell>> readRegisterCells(Set<PiRegisterCellId> cellIds);
+
+    /**
+     * Gets all of the register cells given register ID from the device.
+     * @param registerId the identifier of register
+     * @return completable future with the collection of register cells
+     */
+    CompletableFuture<Collection<PiRegisterCell>> readRegisterCells(PiRegisterId registerId);
+
+    /**
+     * Gets all of the register cells from the device.
+     *
+     * @return completable future with the collection of register cells
+     */
+    CompletableFuture<Collection<PiRegisterCell>> readAllRegisterCells();
+}
\ No newline at end of file
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineModel.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineModel.java
index c1840d9..bddbae8 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineModel.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiPipelineModel.java
@@ -43,7 +43,7 @@
     Collection<PiTableModel> tables();
 
     /**
-     * Returns the counter model associated with the given ID, id present.
+     * Returns the counter model associated with the given ID, if present.
      *
      * @param counterId counter ID
      * @return optional counter model
@@ -58,7 +58,7 @@
     Collection<PiCounterModel> counters();
 
     /**
-     * Returns the meter model associated with the given ID, id present.
+     * Returns the meter model associated with the given ID, if present.
      *
      * @param meterId meter ID
      * @return optional meter model
@@ -73,7 +73,22 @@
     Collection<PiMeterModel> meters();
 
     /**
-     * Returns the action profile model associated with the given ID, id present.
+     * Returns the register model associated with the given ID, if present.
+     *
+     * @param registerId register ID
+     * @return optional register model
+     */
+    Optional<PiRegisterModel> register(PiRegisterId registerId);
+
+    /**
+     * Returns all register models defined by this pipeline model.
+     *
+     * @return collection of register models
+     */
+    Collection<PiRegisterModel> registers();
+
+    /**
+     * Returns the action profile model associated with the given ID, if present.
      *
      * @param actionProfileId action profile ID
      * @return optional action profile model
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterId.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterId.java
new file mode 100644
index 0000000..ea8ca6f
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterId.java
@@ -0,0 +1,46 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.net.pi.model;
+
+import com.google.common.annotations.Beta;
+import org.onlab.util.Identifier;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Identifier of a register in a protocol-independent pipeline, unique within the scope of a pipeline model.
+ */
+@Beta
+public final class PiRegisterId extends Identifier<String> {
+
+    private PiRegisterId(String name) {
+        super(name);
+    }
+
+    /**
+     * Returns an identifier for the given register name.
+     *
+     * @param name register name
+     * @return register ID
+     */
+    public static PiRegisterId of(String name) {
+        checkNotNull(name);
+        checkArgument(!name.isEmpty(), "Name can't be empty");
+        return new PiRegisterId(name);
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterModel.java b/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterModel.java
new file mode 100644
index 0000000..0da02fc
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/model/PiRegisterModel.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.net.pi.model;
+
+import com.google.common.annotations.Beta;
+
+/**
+ * Model of a register in a protocol-independent pipeline.
+ */
+@Beta
+public interface PiRegisterModel {
+
+    /**
+     * Returns the ID of this register.
+     *
+     * @return register ID
+     */
+    PiRegisterId id();
+
+    /**
+     * Returns the number of cells of this register.
+     *
+     * @return size
+     */
+    long size();
+}
+
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
index 5ef452b..a6c612b 100644
--- a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiEntityType.java
@@ -41,5 +41,10 @@
     /**
      * Meter config.
      */
-    METER_CELL_CONFIG
+    METER_CELL_CONFIG,
+
+    /**
+     * Register entry.
+     */
+    REGISTER_CELL
 }
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCell.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCell.java
new file mode 100644
index 0000000..fd354ec
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCell.java
@@ -0,0 +1,140 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.net.pi.runtime;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Objects;
+import org.onosproject.net.pi.model.PiData;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * A register cell entry of a protocol-independent pipeline.
+ */
+@Beta
+public final class PiRegisterCell implements PiEntity {
+
+    private final PiRegisterCellId registerCellId;
+    private final PiData piData;
+
+    private PiRegisterCell(PiRegisterCellId registerCellId, PiData piData) {
+        this.registerCellId = registerCellId;
+        this.piData = piData;
+    }
+
+    /**
+     * Returns the cell identifier.
+     *
+     * @return cell identifier
+     */
+    public PiRegisterCellId cellId() {
+        return registerCellId;
+    }
+
+    /**
+     * Returns the data contained by this cell ID.
+     *
+     * @return PI data or null
+     */
+    public PiData data() {
+        return piData;
+    }
+
+    @Override
+    public PiEntityType piEntityType() {
+        return PiEntityType.REGISTER_CELL;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final PiRegisterCell other = (PiRegisterCell) obj;
+        return Objects.equal(this.registerCellId, other.registerCellId) &&
+                Objects.equal(this.piData, other.piData);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(registerCellId, piData);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("cellId", registerCellId)
+                .add("piData", piData)
+                .toString();
+    }
+
+    /**
+     * Returns a register cell entry builder.
+     *
+     * @return a new builder
+     */
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    public static final class Builder {
+        private PiRegisterCellId cellId;
+        private PiData piData;
+
+
+        private Builder() {
+            // Hides constructor.
+        }
+
+        /**
+         * Sets the register cell identifier for this register.
+         *
+         * @param registerCellId register cell identifier
+         * @return this
+         */
+        public PiRegisterCell.Builder withCellId(PiRegisterCellId registerCellId) {
+            this.cellId = registerCellId;
+            return this;
+        }
+
+
+        /**
+         * Sets the data of this register cell.
+         *
+         * @param data protocol-independent data
+         * @return this
+         */
+        public PiRegisterCell.Builder withData(PiData data) {
+            this.piData = data;
+            return this;
+        }
+
+        /**
+         * Builds the register cell entry.
+         *
+         * @return a new register cell entry
+         */
+        public PiRegisterCell build() {
+            checkNotNull(cellId);
+            return new PiRegisterCell(cellId, piData);
+        }
+    }
+}
\ No newline at end of file
diff --git a/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCellId.java b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCellId.java
new file mode 100644
index 0000000..e56aa17
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/net/pi/runtime/PiRegisterCellId.java
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+
+package org.onosproject.net.pi.runtime;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Objects;
+import org.onosproject.net.pi.model.PiRegisterId;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Identifier of a register cell in a protocol-independent pipeline.
+ */
+@Beta
+public final class PiRegisterCellId {
+
+    private final PiRegisterId registerId;
+    private final long index;
+
+    private PiRegisterCellId(PiRegisterId registerId, long index) {
+        this.registerId = registerId;
+        this.index = index;
+    }
+
+    /**
+     * Returns the identifier of the register instance where this cell is
+     * contained.
+     *
+     * @return register identifier
+     */
+    public PiRegisterId registerId() {
+        return registerId;
+    }
+
+    /**
+     * Returns the register index to which this cell ID is associated.
+     *
+     * @return register index
+     */
+    public long index() {
+        return index;
+    }
+
+    /**
+     * Return a register cell ID for the given register ID and index.
+     *
+     * @param registerId register ID
+     * @param index     index
+     * @return register cell ID
+     */
+    public static PiRegisterCellId of(PiRegisterId registerId, long index) {
+        checkNotNull(registerId);
+        checkArgument(index >= 0, "Index must be a positive number");
+        return new PiRegisterCellId(registerId, index);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final PiRegisterCellId other = (PiRegisterCellId) obj;
+        return Objects.equal(this.registerId, other.registerId)
+                && Objects.equal(this.index, other.index);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hashCode(registerId, index);
+    }
+
+    @Override
+    public String toString() {
+        return registerId.toString() + ':' + String.valueOf(index);
+    }
+}
\ No newline at end of file