Abstract implementation of a behaviour for P4Runtime

Implements commonality of all other behaviours. Also, simplified
the implementation of all other P4Runtime behaviours by re-using that.

Change-Id: Ibb25bdd1e0d1c6e8863341df87fa83d4a782b8d9
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimeHandlerBehaviour.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimeHandlerBehaviour.java
new file mode 100644
index 0000000..8a5a2dd
--- /dev/null
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/AbstractP4RuntimeHandlerBehaviour.java
@@ -0,0 +1,116 @@
+/*
+ * Copyright 2017-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.drivers.p4runtime;
+
+import io.grpc.ManagedChannelBuilder;
+import io.grpc.netty.NettyChannelBuilder;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.driver.AbstractHandlerBehaviour;
+import org.onosproject.net.pi.model.PiPipeconf;
+import org.onosproject.net.pi.runtime.PiPipeconfService;
+import org.onosproject.p4runtime.api.P4RuntimeClient;
+import org.onosproject.p4runtime.api.P4RuntimeController;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract implementation of a behaviour handler for a P4Runtime device.
+ */
+public class AbstractP4RuntimeHandlerBehaviour extends AbstractHandlerBehaviour {
+
+    public static final String P4RUNTIME_SERVER_ADDR_KEY = "p4runtime_ip";
+    public static final String P4RUNTIME_SERVER_PORT_KEY = "p4runtime_port";
+    public static final String P4RUNTIME_DEVICE_ID_KEY = "p4runtime_deviceId";
+
+    protected final Logger log = LoggerFactory.getLogger(getClass());
+
+    // Initialized by setupBehaviour()
+    protected DeviceId deviceId;
+    protected DeviceService deviceService;
+    protected Device device;
+    protected P4RuntimeController controller;
+    protected PiPipeconf pipeconf;
+    protected P4RuntimeClient client;
+
+    /**
+     * Initializes this behaviour attributes. Returns true if the operation was successful, false otherwise. This method
+     * assumes that the P4runtime controller already has a client for this device and that the device has been created
+     * in the core.
+     *
+     * @return true if successful, false otherwise
+     */
+    protected boolean setupBehaviour() {
+        deviceId = handler().data().deviceId();
+
+        deviceService = handler().get(DeviceService.class);
+        device = deviceService.getDevice(deviceId);
+        if (device == null) {
+            log.warn("Unable to find device with id {}, aborting operation", deviceId);
+            return false;
+        }
+
+        controller = handler().get(P4RuntimeController.class);
+        if (!controller.hasClient(deviceId)) {
+            log.warn("Unable to find client for {}, aborting operation", deviceId);
+            return false;
+        }
+        client = controller.getClient(deviceId);
+
+        PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
+        if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
+                !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
+            log.warn("Unable to get the pipeconf of {}, aborting operation", deviceId);
+            return false;
+        }
+        pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
+
+        return true;
+    }
+
+    /**
+     * Create a P4Runtime client for this device. Returns true if the operation was successful, false otherwise.
+     *
+     * @return true if successful, false otherwise
+     */
+    protected boolean createClient() {
+        deviceId = handler().data().deviceId();
+        controller = handler().get(P4RuntimeController.class);
+
+        String serverAddr = this.data().value(P4RUNTIME_SERVER_ADDR_KEY);
+        String serverPortString = this.data().value(P4RUNTIME_SERVER_PORT_KEY);
+        String p4DeviceIdString = this.data().value(P4RUNTIME_DEVICE_ID_KEY);
+
+        if (serverAddr == null || serverPortString == null || p4DeviceIdString == null) {
+            log.warn("Unable to create client for {}, missing driver data key (required is {}, {}, and {})",
+                     deviceId, P4RUNTIME_SERVER_ADDR_KEY, P4RUNTIME_SERVER_PORT_KEY, P4RUNTIME_DEVICE_ID_KEY);
+            return false;
+        }
+
+        ManagedChannelBuilder channelBuilder = NettyChannelBuilder
+                .forAddress(serverAddr, Integer.valueOf(serverPortString))
+                .usePlaintext(true);
+
+        if (!controller.createClient(deviceId, Long.parseUnsignedLong(p4DeviceIdString), channelBuilder)) {
+            log.warn("Unable to create client for {}, aborting operation", deviceId);
+            return false;
+        }
+
+        return true;
+    }
+}
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
index 1a0b755..515640b 100644
--- a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeFlowRuleProgrammable.java
@@ -19,29 +19,19 @@
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
-import org.onosproject.net.Device;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.device.DeviceService;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.net.flow.DefaultFlowEntry;
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.FlowRule;
 import org.onosproject.net.flow.FlowRuleProgrammable;
-import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.model.PiPipelineInterpreter;
 import org.onosproject.net.pi.model.PiPipelineModel;
 import org.onosproject.net.pi.model.PiTableModel;
 import org.onosproject.net.pi.runtime.PiFlowRuleTranslationService;
-import org.onosproject.net.pi.runtime.PiPipeconfService;
 import org.onosproject.net.pi.runtime.PiTableEntry;
 import org.onosproject.net.pi.runtime.PiTableId;
-import org.onosproject.p4runtime.api.P4RuntimeClient;
 import org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType;
-import org.onosproject.p4runtime.api.P4RuntimeController;
 import org.onosproject.p4runtime.api.P4RuntimeFlowRuleWrapper;
 import org.onosproject.p4runtime.api.P4RuntimeTableEntryReference;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -58,9 +48,9 @@
 import static org.onosproject.p4runtime.api.P4RuntimeClient.WriteOperationType.*;
 
 /**
- * Implementation of the flow rule programmable behaviour for BMv2.
+ * Implementation of the flow rule programmable behaviour for P4Runtime.
  */
-public class P4RuntimeFlowRuleProgrammable extends AbstractHandlerBehaviour implements FlowRuleProgrammable {
+public class P4RuntimeFlowRuleProgrammable extends AbstractP4RuntimeHandlerBehaviour implements FlowRuleProgrammable {
 
     // TODO: make this attribute configurable by child drivers (e.g. BMv2 or Tofino)
     /*
@@ -75,8 +65,6 @@
      */
     private boolean checkEntryStoreBeforeUpdate = true;
 
-    private final Logger log = LoggerFactory.getLogger(getClass());
-
     // Needed to synchronize operations over the same table entry.
     private static final ConcurrentMap<P4RuntimeTableEntryReference, Lock> ENTRY_LOCKS = Maps.newConcurrentMap();
 
@@ -85,50 +73,31 @@
     private static final ConcurrentMap<P4RuntimeTableEntryReference, P4RuntimeFlowRuleWrapper> ENTRY_STORE =
             Maps.newConcurrentMap();
 
-    private DeviceId deviceId;
-    private P4RuntimeClient client;
-    private PiPipeconf pipeconf;
     private PiPipelineModel pipelineModel;
     private PiPipelineInterpreter interpreter;
     private PiFlowRuleTranslationService piFlowRuleTranslationService;
 
-    private boolean init() {
+    @Override
+    protected boolean setupBehaviour() {
 
-        deviceId = handler().data().deviceId();
-
-        P4RuntimeController controller = handler().get(P4RuntimeController.class);
-        if (!controller.hasClient(deviceId)) {
-            log.warn("Unable to find client for {}, aborting flow rule operation", deviceId);
+        if (!super.setupBehaviour()) {
             return false;
         }
 
-        PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
-        if (!piPipeconfService.ofDevice(deviceId).isPresent() ||
-                !piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
-            log.warn("Unable to get the pipeconf of {}", deviceId);
-            return false;
-        }
-
-        DeviceService deviceService = handler().get(DeviceService.class);
-        Device device = deviceService.getDevice(deviceId);
         if (!device.is(PiPipelineInterpreter.class)) {
             log.warn("Unable to get interpreter of {}", deviceId);
             return false;
         }
-
-        client = controller.getClient(deviceId);
-        pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
-        pipelineModel = pipeconf.pipelineModel();
         interpreter = device.as(PiPipelineInterpreter.class);
+        pipelineModel = pipeconf.pipelineModel();
         piFlowRuleTranslationService = handler().get(PiFlowRuleTranslationService.class);
-
         return true;
     }
 
     @Override
     public Collection<FlowEntry> getFlowEntries() {
 
-        if (!init()) {
+        if (!setupBehaviour()) {
             return Collections.emptyList();
         }
 
@@ -201,7 +170,7 @@
 
     private Collection<FlowRule> processFlowRules(Collection<FlowRule> rules, Operation operation) {
 
-        if (!init()) {
+        if (!setupBehaviour()) {
             return Collections.emptyList();
         }
 
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeHandshaker.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeHandshaker.java
index ee070eb..8416786 100644
--- a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeHandshaker.java
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimeHandshaker.java
@@ -16,27 +16,17 @@
 
 package org.onosproject.drivers.p4runtime;
 
-import io.grpc.ManagedChannelBuilder;
-import io.grpc.netty.NettyChannelBuilder;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.MastershipRole;
 import org.onosproject.net.device.DeviceHandshaker;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
-import org.onosproject.net.driver.DriverData;
 import org.onosproject.p4runtime.api.P4RuntimeController;
-import org.slf4j.Logger;
 
 import java.util.concurrent.CompletableFuture;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
 /**
- * Implementation of DeviceHandshaker for BMv2.
+ * Implementation of DeviceHandshaker for P4Runtime.
  */
-public class P4RuntimeHandshaker extends AbstractHandlerBehaviour
-        implements DeviceHandshaker {
-
-    private final Logger log = getLogger(getClass());
+public class P4RuntimeHandshaker extends AbstractP4RuntimeHandlerBehaviour implements DeviceHandshaker {
 
     // TODO: consider abstract class with empty connect method and  implementation into a protected one for reusability.
 
@@ -46,29 +36,7 @@
     }
 
     private boolean doConnect() {
-
-        P4RuntimeController controller = handler().get(P4RuntimeController.class);
-
-        DeviceId deviceId = handler().data().deviceId();
-        // DeviceKeyService deviceKeyService = handler().get(DeviceKeyService.class);
-        DriverData data = data();
-
-        String serverAddr = data.value("p4runtime_ip");
-        int serverPort = Integer.valueOf(data.value("p4runtime_port"));
-        long p4DeviceId = Long.parseUnsignedLong(data.value("p4runtime_deviceId"));
-
-        ManagedChannelBuilder channelBuilder = NettyChannelBuilder
-                .forAddress(serverAddr, serverPort)
-                .usePlaintext(true);
-
-        if (!controller.createClient(deviceId, p4DeviceId, channelBuilder)) {
-            log.warn("Unable to create P4runtime client for {}", deviceId);
-            return false;
-        }
-
-        // TODO: gNMI handling
-
-        return true;
+        return super.createClient();
     }
 
     @Override
diff --git a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimePacketProgrammable.java b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimePacketProgrammable.java
index 7a11376..e21dd19 100644
--- a/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimePacketProgrammable.java
+++ b/drivers/p4runtime/src/main/java/org/onosproject/drivers/p4runtime/P4RuntimePacketProgrammable.java
@@ -16,57 +16,29 @@
 
 package org.onosproject.drivers.p4runtime;
 
-import org.onosproject.net.Device;
-import org.onosproject.net.DeviceId;
-import org.onosproject.net.device.DeviceService;
-import org.onosproject.net.driver.AbstractHandlerBehaviour;
 import org.onosproject.net.packet.OutboundPacket;
 import org.onosproject.net.packet.PacketProgrammable;
-import org.onosproject.net.pi.model.PiPipeconf;
 import org.onosproject.net.pi.model.PiPipelineInterpreter;
 import org.onosproject.net.pi.runtime.PiPacketOperation;
-import org.onosproject.net.pi.runtime.PiPipeconfService;
-import org.onosproject.p4runtime.api.P4RuntimeClient;
-import org.onosproject.p4runtime.api.P4RuntimeController;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
 
 /**
- * Packet Programmable behaviour for BMv2 devices.
+ * Implementation of PacketProgrammable behaviour for P4Runtime.
  */
-public class P4RuntimePacketProgrammable extends AbstractHandlerBehaviour implements PacketProgrammable {
-    private final Logger log = LoggerFactory.getLogger(getClass());
+public class P4RuntimePacketProgrammable extends AbstractP4RuntimeHandlerBehaviour implements PacketProgrammable {
 
     @Override
     public void emit(OutboundPacket packet) {
 
-        DeviceId deviceId = handler().data().deviceId();
-        P4RuntimeController controller = handler().get(P4RuntimeController.class);
-        if (!controller.hasClient(deviceId)) {
-            log.warn("Unable to find client for {}, aborting the sending packet", deviceId);
+        if (!this.setupBehaviour()) {
             return;
         }
 
-        P4RuntimeClient client = controller.getClient(deviceId);
-        PiPipeconfService piPipeconfService = handler().get(PiPipeconfService.class);
-
-        final PiPipeconf pipeconf;
-        if (piPipeconfService.ofDevice(deviceId).isPresent() &&
-                piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).isPresent()) {
-            pipeconf = piPipeconfService.getPipeconf(piPipeconfService.ofDevice(deviceId).get()).get();
-        } else {
-            log.warn("Unable to get the pipeconf of {}", deviceId);
-            return;
-        }
-
-        DeviceService deviceService = handler().get(DeviceService.class);
-        Device device = deviceService.getDevice(deviceId);
         final PiPipelineInterpreter interpreter = device.is(PiPipelineInterpreter.class)
                 ? device.as(PiPipelineInterpreter.class) : null;
         if (!device.is(PiPipelineInterpreter.class)) {
-            log.warn("Device {} unable to instantiate interpreter of pipeconf {}", deviceId, pipeconf.id());
+            log.warn("Device {} with pipeconf {} has no interpreter, aborting emit operation", deviceId, pipeconf.id());
             return;
         }