Introduce Pi Register model to PI Framework

Change-Id: I7fae87d5b5ed5fff34b3addfc148cee6fc98137c
diff --git a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
index dd365f6..f8d6082 100644
--- a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
+++ b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4InfoParser.java
@@ -42,9 +42,12 @@
 import org.onosproject.net.pi.model.PiPacketOperationModel;
 import org.onosproject.net.pi.model.PiPacketOperationType;
 import org.onosproject.net.pi.model.PiPipelineModel;
+import org.onosproject.net.pi.model.PiRegisterId;
+import org.onosproject.net.pi.model.PiRegisterModel;
 import org.onosproject.net.pi.model.PiTableId;
 import org.onosproject.net.pi.model.PiTableModel;
 import org.onosproject.net.pi.model.PiTableType;
+import p4.config.P4InfoOuterClass;
 import p4.config.P4InfoOuterClass.Action;
 import p4.config.P4InfoOuterClass.ActionProfile;
 import p4.config.P4InfoOuterClass.ActionRef;
@@ -142,6 +145,10 @@
         meterMap.putAll(parseMeters(p4info));
         meterMap.putAll(parseDirectMeters(p4info));
 
+        // Registers.
+        final Map<Integer, PiRegisterModel> registerMap = Maps.newHashMap();
+        registerMap.putAll(parseRegisters(p4info));
+
         // Action profiles.
         final Map<Integer, PiActionProfileModel> actProfileMap = parseActionProfiles(p4info);
 
@@ -219,6 +226,9 @@
         ImmutableMap<PiMeterId, PiMeterModel> meterImmMap = ImmutableMap.copyOf(
                 meterMap.values().stream()
                         .collect(Collectors.toMap(PiMeterModel::id, m -> m)));
+        ImmutableMap<PiRegisterId, PiRegisterModel> registerImmMap = ImmutableMap.copyOf(
+                registerMap.values().stream()
+                        .collect(Collectors.toMap(PiRegisterModel::id, r -> r)));
         ImmutableMap<PiActionProfileId, PiActionProfileModel> actProfileImmMap = ImmutableMap.copyOf(
                 actProfileMap.values().stream()
                         .collect(Collectors.toMap(PiActionProfileModel::id, a -> a)));
@@ -227,6 +237,7 @@
                 tableImmMapBuilder.build(),
                 counterImmMap,
                 meterImmMap,
+                registerImmMap,
                 actProfileImmMap,
                 ImmutableMap.copyOf(pktOpMap));
     }
@@ -296,6 +307,17 @@
         return meterMap;
     }
 
+    private static Map<Integer, PiRegisterModel> parseRegisters(P4Info p4info)
+            throws P4InfoParserException {
+        final Map<Integer, PiRegisterModel> registerMap = Maps.newHashMap();
+        for (P4InfoOuterClass.Register registerMsg : p4info.getRegistersList()) {
+            registerMap.put(registerMsg.getPreamble().getId(),
+                            new P4RegisterModel(PiRegisterId.of(registerMsg.getPreamble().getName()),
+                                                registerMsg.getSize()));
+        }
+        return registerMap;
+    }
+
     private static Map<Integer, PiActionProfileModel> parseActionProfiles(P4Info p4info)
             throws P4InfoParserException {
         final Map<Integer, PiActionProfileModel> actProfileMap = Maps.newHashMap();
diff --git a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4PipelineModel.java b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4PipelineModel.java
index 622cc23..ce9ec13 100644
--- a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4PipelineModel.java
+++ b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4PipelineModel.java
@@ -26,6 +26,8 @@
 import org.onosproject.net.pi.model.PiPacketOperationModel;
 import org.onosproject.net.pi.model.PiPacketOperationType;
 import org.onosproject.net.pi.model.PiPipelineModel;
+import org.onosproject.net.pi.model.PiRegisterId;
+import org.onosproject.net.pi.model.PiRegisterModel;
 import org.onosproject.net.pi.model.PiTableId;
 import org.onosproject.net.pi.model.PiTableModel;
 
@@ -41,6 +43,7 @@
     private final ImmutableMap<PiTableId, PiTableModel> tables;
     private final ImmutableMap<PiCounterId, PiCounterModel> counters;
     private final ImmutableMap<PiMeterId, PiMeterModel> meters;
+    private final ImmutableMap<PiRegisterId, PiRegisterModel> registers;
     private final ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles;
     private final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations;
 
@@ -48,11 +51,13 @@
             ImmutableMap<PiTableId, PiTableModel> tables,
             ImmutableMap<PiCounterId, PiCounterModel> counters,
             ImmutableMap<PiMeterId, PiMeterModel> meters,
+            ImmutableMap<PiRegisterId, PiRegisterModel> registers,
             ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles,
             ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations) {
         this.tables = tables;
         this.counters = counters;
         this.meters = meters;
+        this.registers = registers;
         this.actionProfiles = actionProfiles;
         this.packetOperations = packetOperations;
     }
@@ -88,6 +93,16 @@
     }
 
     @Override
+    public Optional<PiRegisterModel> register(PiRegisterId registerId) {
+        return Optional.ofNullable(registers.get(registerId));
+    }
+
+    @Override
+    public Collection<PiRegisterModel> registers() {
+        return registers.values();
+    }
+
+    @Override
     public Optional<PiActionProfileModel> actionProfiles(PiActionProfileId actionProfileId) {
         return Optional.ofNullable(actionProfiles.get(actionProfileId));
     }
diff --git a/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4RegisterModel.java b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4RegisterModel.java
new file mode 100644
index 0000000..7e4fcda
--- /dev/null
+++ b/protocols/p4runtime/model/src/main/java/org/onosproject/p4runtime/model/P4RegisterModel.java
@@ -0,0 +1,64 @@
+/*
+ * 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.p4runtime.model;
+
+import org.onosproject.net.pi.model.PiRegisterModel;
+import org.onosproject.net.pi.model.PiRegisterId;
+
+import java.util.Objects;
+
+/**
+ * Implementation of PiRegisterModel for P4Runtime.
+ */
+final class P4RegisterModel implements PiRegisterModel {
+
+    private final PiRegisterId id;
+    private final long size;
+
+    P4RegisterModel(PiRegisterId id, long size) {
+        this.id = id;
+        this.size = size;
+    }
+
+    @Override
+    public PiRegisterId id() {
+        return id;
+    }
+
+    @Override
+    public long size() {
+        return size;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(id, size);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null || getClass() != obj.getClass()) {
+            return false;
+        }
+        final P4RegisterModel other = (P4RegisterModel) obj;
+        return Objects.equals(this.id, other.id)
+                && Objects.equals(this.size, other.size);
+    }
+}
\ No newline at end of file
diff --git a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
index 918e6e8..84d3af8 100644
--- a/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
+++ b/protocols/p4runtime/model/src/test/java/org/onosproject/p4runtime/model/P4PipelineModelTest.java
@@ -41,6 +41,8 @@
 import org.onosproject.net.pi.model.PiPacketOperationModel;
 import org.onosproject.net.pi.model.PiPacketOperationType;
 import org.onosproject.net.pi.model.PiPipelineModel;
+import org.onosproject.net.pi.model.PiRegisterId;
+import org.onosproject.net.pi.model.PiRegisterModel;
 import org.onosproject.net.pi.model.PiTableId;
 import org.onosproject.net.pi.model.PiTableModel;
 import org.onosproject.net.pi.model.PiTableType;
@@ -142,6 +144,25 @@
                     .put(PI_METER_ID_2, P4_METER_MODEL_2)
                     .build();
 
+    /* Registers */
+    private static final PiRegisterId PI_REGISTER_ID_1 = PiRegisterId.of("Register1");
+    private static final PiRegisterId PI_REGISTER_ID_2 = PiRegisterId.of("Register2");
+
+    private static final long REGISTER_SIZE_1 = 1000;
+    private static final long REGISTER_SIZE_2 = 2000;
+
+    private static final P4RegisterModel P4_REGISTER_MODEL_1 = new P4RegisterModel(PI_REGISTER_ID_1, REGISTER_SIZE_1);
+    private static final P4RegisterModel P4_REGISTER_MODEL_2 = new P4RegisterModel(PI_REGISTER_ID_2, REGISTER_SIZE_2);
+
+    private static final ImmutableMap<PiRegisterId, PiRegisterModel> REGISTERS_1 =
+            new ImmutableMap.Builder<PiRegisterId, PiRegisterModel>()
+                    .put(PI_REGISTER_ID_1, P4_REGISTER_MODEL_1)
+                    .build();
+    private static final ImmutableMap<PiRegisterId, PiRegisterModel> REGISTERS_2 =
+            new ImmutableMap.Builder<PiRegisterId, PiRegisterModel>()
+                    .put(PI_REGISTER_ID_2, P4_REGISTER_MODEL_2)
+                    .build();
+
     /* Match Fields */
     private static final PiMatchFieldId PI_MATCH_FIELD_ID_1 = PiMatchFieldId.of("MatchField1");
     private static final PiMatchFieldId PI_MATCH_FIELD_ID_2 = PiMatchFieldId.of("MatchField2");
@@ -315,11 +336,11 @@
                     .build();
 
     private static final PiPipelineModel P4_PIPELINE_MODEL_1 =
-            new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
+            new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, REGISTERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
     private static final PiPipelineModel SAME_AS_P4_PIPELINE_MODEL_1 =
-            new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
+            new P4PipelineModel(TABLES_1, COUNTERS_1, METERS_1, REGISTERS_1, ACTION_PROFILES_1, PACKET_OPERATIONS_1);
     private static final PiPipelineModel P4_PIPELINE_MODEL_2 =
-            new P4PipelineModel(TABLES_2, COUNTERS_2, METERS_2, ACTION_PROFILES_2, PACKET_OPERATIONS_2);
+            new P4PipelineModel(TABLES_2, COUNTERS_2, METERS_2, REGISTERS_1, ACTION_PROFILES_2, PACKET_OPERATIONS_2);
 
     /**
      * Checks that the P4PipelineModel class is immutable.