Minor OFDPA refactoring according to IntelliJ code analysis

Change-Id: I75bd19f11c4500aafe1e6050fec46e70fd01da15
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/McastHandler.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/McastHandler.java
index b0be4e3..270307f 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/McastHandler.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/McastHandler.java
@@ -50,6 +50,7 @@
 import org.onosproject.net.mcast.McastEvent;
 import org.onosproject.net.mcast.McastRouteInfo;
 import org.onosproject.net.topology.TopologyService;
+import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
 import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
 import org.onosproject.segmentrouting.storekey.McastStoreKey;
 import org.onosproject.store.serializers.KryoNamespaces;
@@ -375,8 +376,16 @@
             return;
         }
 
+        MacAddress routerMac;
+        try {
+            routerMac = srManager.deviceConfiguration().getDeviceMac(deviceId);
+        } catch (DeviceConfigNotFoundException dcnfe) {
+            log.warn("Fail to push filtering objective since device is not configured. Abort");
+            return;
+        }
+
         FilteringObjective.Builder filtObjBuilder =
-                filterObjBuilder(deviceId, port, assignedVlan, mcastIp);
+                filterObjBuilder(deviceId, port, assignedVlan, mcastIp, routerMac);
         ObjectiveContext context = new DefaultObjectiveContext(
                 (objective) -> log.debug("Successfully add filter on {}/{}, vlan {}",
                         deviceId, port.toLong(), assignedVlan),
@@ -626,10 +635,12 @@
      * @param deviceId Device ID
      * @param ingressPort ingress port of the multicast stream
      * @param assignedVlan assigned VLAN ID
+     * @param routerMac router MAC. This is carried in metadata and used from some switches that
+     *                  need to put unicast entry before multicast entry in TMAC table.
      * @return filtering objective builder
      */
     private FilteringObjective.Builder filterObjBuilder(DeviceId deviceId, PortNumber ingressPort,
-            VlanId assignedVlan, IpAddress mcastIp) {
+            VlanId assignedVlan, IpAddress mcastIp, MacAddress routerMac) {
         FilteringObjective.Builder filtBuilder = DefaultFilteringObjective.builder();
 
         if (mcastIp.isIp4()) {
@@ -646,7 +657,9 @@
             .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
         }
         TrafficTreatment tt = DefaultTrafficTreatment.builder()
-                .pushVlan().setVlanId(assignedVlan).build();
+                .pushVlan().setVlanId(assignedVlan)
+                .setEthDst(routerMac)
+                .build();
         filtBuilder.withMeta(tt);
 
         return filtBuilder.permit().fromApp(srManager.appId);
@@ -825,8 +838,16 @@
             return;
         }
 
+        MacAddress routerMac;
+        try {
+            routerMac = srManager.deviceConfiguration().getDeviceMac(deviceId);
+        } catch (DeviceConfigNotFoundException dcnfe) {
+            log.warn("Fail to push filtering objective since device is not configured. Abort");
+            return;
+        }
+
         FilteringObjective.Builder filtObjBuilder =
-                filterObjBuilder(deviceId, port, assignedVlan, mcastIp);
+                filterObjBuilder(deviceId, port, assignedVlan, mcastIp, routerMac);
         ObjectiveContext context = new DefaultObjectiveContext(
                 (objective) -> log.debug("Successfully removed filter on {}/{}, vlan {}",
                                          deviceId, port.toLong(), assignedVlan),
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
index d8de983..98eeb42 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2Pipeline.java
@@ -179,7 +179,7 @@
 
         // ofdpa cannot match on ALL portnumber, so we need to use separate
         // rules for each port.
-        List<PortNumber> portnums = new ArrayList<PortNumber>();
+        List<PortNumber> portnums = new ArrayList<>();
         if (portCriterion.port() == PortNumber.ALL) {
             for (Port port : deviceService.getPorts(deviceId)) {
                 if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) {
@@ -335,7 +335,7 @@
         }
         // ofdpa cannot match on ALL portnumber, so we need to use separate
         // rules for each port.
-        List<PortNumber> portnums = new ArrayList<PortNumber>();
+        List<PortNumber> portnums = new ArrayList<>();
         if (portCriterion != null) {
             if (portCriterion.port() == PortNumber.ALL) {
                 for (Port port : deviceService.getPorts(deviceId)) {
@@ -348,7 +348,7 @@
             }
         }
 
-        List<FlowRule> rules = new ArrayList<FlowRule>();
+        List<FlowRule> rules = new ArrayList<>();
         for (PortNumber pnum : portnums) {
             // TMAC rules for unicast IP packets
             TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
@@ -459,7 +459,7 @@
             return Collections.emptySet();
         }
         boolean defaultRule = false;
-        int forTableId = -1;
+        int forTableId;
         TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder();
         TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder();
 
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
index 0279cb1..293f651 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/CpqdOfdpa2VlanPipeline.java
@@ -107,7 +107,7 @@
         }
         // ofdpa cannot match on ALL portnumber, so we need to use separate
         // rules for each port.
-        List<PortNumber> portnums = new ArrayList<PortNumber>();
+        List<PortNumber> portnums = new ArrayList<>();
         if (portCriterion != null) {
             if (portCriterion.port() == PortNumber.ALL) {
                 for (Port port : deviceService.getPorts(deviceId)) {
@@ -120,7 +120,7 @@
             }
         }
 
-        List<FlowRule> rules = new ArrayList<FlowRule>();
+        List<FlowRule> rules = new ArrayList<>();
         for (PortNumber pnum : portnums) {
             // for unicast IP packets
             TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
diff --git a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
index 0c4a852..e4770ba 100644
--- a/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
+++ b/drivers/default/src/main/java/org/onosproject/driver/pipeline/ofdpa/Ofdpa2Pipeline.java
@@ -106,7 +106,6 @@
  * Driver for Broadcom's OF-DPA v2.0 TTP.
  */
 public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner {
-
     protected static final int PORT_TABLE = 0;
     protected static final int VLAN_TABLE = 10;
     protected static final int VLAN_1_TABLE = 11;
@@ -129,12 +128,10 @@
     protected static final int LOWEST_PRIORITY = 0x0;
 
     protected static final int MPLS_L2_PORT_PRIORITY = 2;
-
     protected static final int MPLS_TUNNEL_ID_BASE = 0x10000;
     protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF;
 
     protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF;
-
     protected static final int MPLS_NNI_PORT_BASE = 0x00020000;
     protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF;
 
@@ -158,10 +155,10 @@
     protected Ofdpa2GroupHandler groupHandler;
 
     // flows installations to be retried
-    protected ScheduledExecutorService executorService
+    private ScheduledExecutorService executorService
         = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log));
-    protected static final int MAX_RETRY_ATTEMPTS = 10;
-    protected static final int RETRY_MS = 1000;
+    private static final int MAX_RETRY_ATTEMPTS = 10;
+    private static final int RETRY_MS = 1000;
 
     @Override
     public void init(DeviceId deviceId, PipelinerContext context) {
@@ -265,7 +262,7 @@
         sendForward(fwd, rules);
     }
 
-    protected void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
+    private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) {
         FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder();
         switch (fwd.op()) {
         case ADD:
@@ -890,7 +887,7 @@
         return builder.add(rule).build();
     }
 
-    protected List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
+    List<FlowRule> processMcastEthDstFilter(EthCriterion ethCriterion,
                                                       VlanId assignedVlan,
                                                       ApplicationId applicationId) {
         ImmutableList.Builder<FlowRule> builder = ImmutableList.builder();
@@ -1027,7 +1024,7 @@
             for (Instruction ins : fwd.treatment().allInstructions()) {
                 if (ins instanceof OutputInstruction) {
                     OutputInstruction o = (OutputInstruction) ins;
-                    if (o != null && PortNumber.CONTROLLER.equals(o.port())) {
+                    if (PortNumber.CONTROLLER.equals(o.port())) {
                         ttBuilder.add(o);
                     } else {
                         log.warn("Only allowed treatments in versatile forwarding "
@@ -1080,7 +1077,7 @@
      *            for this type of forwarding objective. An empty set may be
      *            returned if there is an issue in processing the objective.
      */
-    protected Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
+    private Collection<FlowRule> processSpecific(ForwardingObjective fwd) {
         log.debug("Processing specific fwd objective:{} in dev:{} with next:{}",
                   fwd.id(), deviceId, fwd.nextId());
         boolean isEthTypeObj = isSupportedEthTypeObjective(fwd);
@@ -1145,17 +1142,19 @@
             } else {
                 forTableId = UNICAST_ROUTING_TABLE;
             }
+
+            /*
+            // XXX decrementing IP ttl is done automatically for routing, this
+            // action is ignored or rejected in ofdpa as it is not fully implemented
             if (fwd.treatment() != null) {
                 for (Instruction instr : fwd.treatment().allInstructions()) {
                     if (instr instanceof L3ModificationInstruction &&
                             ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
-                        // XXX decrementing IP ttl is done automatically for routing, this
-                        // action is ignored or rejected in ofdpa as it is not fully implemented
-                        //tb.deferred().add(instr);
+                        tb.deferred().add(instr);
                     }
                 }
             }
-
+            */
         } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) {
             if (buildIpv6Selector(filteredSelector, fwd) < 0) {
                 return Collections.emptyList();
@@ -1168,16 +1167,18 @@
                 forTableId = UNICAST_ROUTING_TABLE;
             }
 
+            // XXX decrementing IP ttl is done automatically for routing, this
+            // action is ignored or rejected in ofdpa as it is not fully implemented
+            /*
             if (fwd.treatment() != null) {
                 for (Instruction instr : fwd.treatment().allInstructions()) {
                     if (instr instanceof L3ModificationInstruction &&
                             ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) {
-                        // XXX decrementing IP ttl is done automatically for routing, this
-                        // action is ignored or rejected in ofdpa as it is not fully implemented
-                        //tb.deferred().add(instr);
+                        tb.deferred().add(instr);
                     }
                 }
             }
+            */
         } else {
             filteredSelector
                 .matchEthType(Ethernet.MPLS_UNICAST)
@@ -1304,7 +1305,7 @@
         return flowRuleCollection;
     }
 
-    protected int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
+    private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate,
                                     TrafficSelector.Builder extBuilder,
                                     ForwardingObjective fwd,
                                     boolean allowDefaultRoute) {
@@ -1363,7 +1364,7 @@
      * @return 0 if the update ends correctly. -1 if the matches
      * are not yet supported
      */
-    protected int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
+    int buildIpv6Selector(TrafficSelector.Builder builderToUpdate,
                                     ForwardingObjective fwd) {
 
         TrafficSelector selector = fwd.selector();
@@ -1401,7 +1402,7 @@
         return 0;
     }
 
-    protected FlowRule defaultRoute(ForwardingObjective fwd,
+    FlowRule defaultRoute(ForwardingObjective fwd,
                                     TrafficSelector.Builder complementarySelector,
                                     int forTableId,
                                     TrafficTreatment.Builder tb) {
@@ -1532,7 +1533,7 @@
         return !(ethDst == null && vlanId == null);
     }
 
-    protected NextGroup getGroupForNextObjective(Integer nextId) {
+    NextGroup getGroupForNextObjective(Integer nextId) {
         NextGroup next = flowObjectiveStore.getNextGroup(nextId);
         if (next != null) {
             List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data());