Refactor: add javadoc, restrict visibility, etc for sona switching

Change-Id: I9f5d8e53fb037a34f31dc15f9038a8fd0e756356
diff --git a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/InstancePortEvent.java b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/InstancePortEvent.java
index 547e41f..00f15ed 100644
--- a/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/InstancePortEvent.java
+++ b/apps/openstacknetworking/api/src/main/java/org/onosproject/openstacknetworking/api/InstancePortEvent.java
@@ -22,6 +22,9 @@
  */
 public class InstancePortEvent extends AbstractEvent<InstancePortEvent.Type, InstancePort> {
 
+    /**
+     * Instance port event type.
+     */
     public enum Type {
 
         /**
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/HostBasedInstancePortManager.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/HostBasedInstancePortManager.java
index 207cd2b..d5d1aac 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/HostBasedInstancePortManager.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/HostBasedInstancePortManager.java
@@ -16,6 +16,7 @@
 package org.onosproject.openstacknetworking.impl;
 
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -33,10 +34,12 @@
 import org.onosproject.net.host.HostService;
 import org.onosproject.openstacknetworking.api.InstancePort;
 import org.onosproject.openstacknetworking.api.InstancePortEvent;
+import org.onosproject.openstacknetworking.api.InstancePortEvent.Type;
 import org.onosproject.openstacknetworking.api.InstancePortListener;
 import org.onosproject.openstacknetworking.api.InstancePortService;
 import org.slf4j.Logger;
 
+import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -127,6 +130,12 @@
                 host.annotations().value(ANNOTATION_PORT_ID) != null;
     }
 
+    /**
+     * An internal listener that listens host event generated by HostLocationTracker
+     * in DistributedHostStore. The role of this listener is to convert host event
+     * to instance port event and post to the subscribers that have interested on
+     * this type of event.
+     */
     private class InternalHostListener implements HostListener {
 
         @Override
@@ -142,32 +151,30 @@
         @Override
         public void event(HostEvent event) {
             InstancePort instPort = HostBasedInstancePort.of(event.subject());
-            InstancePortEvent instPortEvent;
             switch (event.type()) {
                 case HOST_UPDATED:
-                    instPortEvent = new InstancePortEvent(
-                            OPENSTACK_INSTANCE_PORT_UPDATED,
-                            instPort);
-                    log.debug("Instance port is updated: {}", instPort);
-                    process(instPortEvent);
+                    processEvent(OPENSTACK_INSTANCE_PORT_UPDATED, instPort);
                     break;
                 case HOST_ADDED:
-                    instPortEvent = new InstancePortEvent(
-                            OPENSTACK_INSTANCE_PORT_DETECTED,
-                            instPort);
-                    log.debug("Instance port is detected: {}", instPort);
-                    process(instPortEvent);
+                    processEvent(OPENSTACK_INSTANCE_PORT_DETECTED, instPort);
                     break;
                 case HOST_REMOVED:
-                    instPortEvent = new InstancePortEvent(
-                            OPENSTACK_INSTANCE_PORT_VANISHED,
-                            instPort);
-                    log.debug("Instance port is disabled: {}", instPort);
-                    process(instPortEvent);
+                    processEvent(OPENSTACK_INSTANCE_PORT_VANISHED, instPort);
                     break;
                 default:
                     break;
             }
         }
+
+        private void processEvent(Type type, InstancePort port) {
+            Map<Type, String> eventMap = Maps.newConcurrentMap();
+            eventMap.put(OPENSTACK_INSTANCE_PORT_UPDATED, "updated");
+            eventMap.put(OPENSTACK_INSTANCE_PORT_DETECTED, "detected");
+            eventMap.put(OPENSTACK_INSTANCE_PORT_VANISHED, "disabled");
+
+            InstancePortEvent instPortEvent = new InstancePortEvent(type, port);
+            log.debug("Instance port is {}: {}", eventMap.get(type), port);
+            process(instPortEvent);
+        }
     }
 }
diff --git a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
index 3cba0e3..e27b14a 100644
--- a/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
+++ b/apps/openstacknetworking/app/src/main/java/org/onosproject/openstacknetworking/impl/OpenstackSwitchingHandler.java
@@ -77,31 +77,31 @@
     private static final String ERR_SET_FLOWS_VNI = "Failed to set flows for %s: Failed to get VNI for %s";
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    CoreService coreService;
+    protected CoreService coreService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    MastershipService mastershipService;
+    protected MastershipService mastershipService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    DeviceService deviceService;
+    protected DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    OpenstackFlowRuleService osFlowRuleService;
+    protected OpenstackFlowRuleService osFlowRuleService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    InstancePortService instancePortService;
+    protected InstancePortService instancePortService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    OpenstackNetworkService osNetworkService;
+    protected OpenstackNetworkService osNetworkService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    OpenstackNodeService osNodeService;
+    protected OpenstackNodeService osNodeService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    DriverService driverService;
+    protected DriverService driverService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    OpenstackSecurityGroupService securityGroupService;
+    protected OpenstackSecurityGroupService securityGroupService;
 
     private final ExecutorService eventExecutor = newSingleThreadExecutor(
             groupedThreads(this.getClass().getSimpleName(), "event-handler"));
@@ -124,8 +124,16 @@
         log.info("Stopped");
     }
 
+    /**
+     * Configures L2 forwarding rules.
+     * Currently, SONA only supports VXLAN and VLAN tunnelled L2 forwarding.
+     *
+     * @param instPort instance port object
+     * @param install install flag, add the rule if true, remove it otherwise
+     */
     private void setNetworkRules(InstancePort instPort, boolean install) {
-        switch (osNetworkService.network(instPort.networkId()).getNetworkType()) {
+        NetworkType type = osNetworkService.network(instPort.networkId()).getNetworkType();
+        switch (type) {
             case VXLAN:
                 setTunnelTagFlowRules(instPort, install);
                 setForwardingRules(instPort, install);
@@ -135,19 +143,29 @@
                 setForwardingRulesForVlan(instPort, install);
                 break;
             default:
+                log.warn("Unsupported network tunnel type {}", type.name());
                 break;
         }
     }
 
+    /**
+     * Configures the flow rules which are used for L2 packet switching.
+     * Note that these rules will be inserted in switching table (table 5).
+     *
+     * @param instPort instance port object
+     * @param install install flag, add the rule if true, remove it otherwise
+     */
     private void setForwardingRules(InstancePort instPort, boolean install) {
         // switching rules for the instPorts in the same node
         TrafficSelector selector = DefaultTrafficSelector.builder()
+                // TODO: need to handle IPv6 in near future
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchIPDst(instPort.ipAddress().toIpPrefix())
                 .matchTunnelId(getVni(instPort))
                 .build();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
+                // TODO: this might not be necessary for the VMs located in the same subnet
                 .setEthDst(instPort.macAddress())
                 .setOutput(instPort.portNumber())
                 .build();
@@ -191,9 +209,17 @@
                 });
     }
 
+    /**
+     * Configures the flow rules which are used for L2 VLAN packet switching.
+     * Note that these rules will be inserted in switching table (table 5).
+     *
+     * @param instPort instance port object
+     * @param install install flag, add the rule if true, remove it otherwise
+     */
     private void setForwardingRulesForVlan(InstancePort instPort, boolean install) {
         // switching rules for the instPorts in the same node
         TrafficSelector selector = DefaultTrafficSelector.builder()
+                // TODO: need to handle IPv6 in near future
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchIPDst(instPort.ipAddress().toIpPrefix())
                 .matchVlanId(getVlanId(instPort))
@@ -201,6 +227,7 @@
 
         TrafficTreatment treatment = DefaultTrafficTreatment.builder()
                 .popVlan()
+                // TODO: this might not be necessary for the VMs located in the same subnet
                 .setEthDst(instPort.macAddress())
                 .setOutput(instPort.portNumber())
                 .build();
@@ -234,13 +261,23 @@
                 });
     }
 
+    /**
+     * Configures the flow rule which is for using VXLAN to tag the packet
+     * based on the in_port number of a virtual instance.
+     * Note that this rule will be inserted in VNI table (table 0).
+     *
+     * @param instPort instance port object
+     * @param install install flag, add the rule if true, remove it otherwise
+     */
     private void setTunnelTagFlowRules(InstancePort instPort, boolean install) {
         TrafficSelector selector = DefaultTrafficSelector.builder()
+                // TODO: need to handle IPv6 in near future
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchInPort(instPort.portNumber())
                 .build();
 
-        // XXX All egress traffic needs to go through connection tracking module, which might hurt its performance.
+        // XXX All egress traffic needs to go through connection tracking module,
+        // which might hurt its performance.
         ExtensionTreatment ctTreatment =
                 RulePopulatorUtil.niciraConnTrackTreatmentBuilder(driverService, instPort.deviceId())
                         .commit(true).build();
@@ -263,8 +300,17 @@
                 install);
     }
 
+    /**
+     * Configures the flow rule which is for using VLAN to tag the packet
+     * based on the in_port number of a virtual instance.
+     * Note that this rule will be inserted in VNI table (table 0).
+     *
+     * @param instPort instance port object
+     * @param install install flag, add the rule if true, remove it otherwise
+     */
     private void setVlanTagFlowRules(InstancePort instPort, boolean install) {
         TrafficSelector selector = DefaultTrafficSelector.builder()
+                // TODO: need to handle IPv6 in near future
                 .matchEthType(Ethernet.TYPE_IPV4)
                 .matchInPort(instPort.portNumber())
                 .build();
@@ -283,7 +329,6 @@
                 PRIORITY_TUNNEL_TAG_RULE,
                 SRC_VNI_TABLE,
                 install);
-
     }
 
     private void setNetworkAdminRules(Network network, boolean install) {
@@ -305,7 +350,7 @@
 
         osNodeService.completeNodes().stream()
                 .filter(osNode -> osNode.type() == COMPUTE)
-                .forEach(osNode -> {
+                .forEach(osNode ->
                     osFlowRuleService.setRule(
                             appId,
                             osNode.intgBridge(),
@@ -313,12 +358,13 @@
                             treatment,
                             PRIORITY_ADMIN_RULE,
                             ACL_TABLE,
-                            install);
-                });
+                            install)
+                );
     }
 
     private void setPortAdminRules(Port port, boolean install) {
-        InstancePort instancePort = instancePortService.instancePort(MacAddress.valueOf(port.getMacAddress()));
+        InstancePort instancePort =
+                instancePortService.instancePort(MacAddress.valueOf(port.getMacAddress()));
         TrafficSelector selector = DefaultTrafficSelector.builder()
                 .matchInPort(instancePort.portNumber())
                 .build();
@@ -337,6 +383,12 @@
                 install);
     }
 
+    /**
+     * Obtains the VLAN ID from the given instance port.
+     *
+     * @param instPort instance port object
+     * @return VLAN ID
+     */
     private VlanId getVlanId(InstancePort instPort) {
         Network osNet = osNetworkService.network(instPort.networkId());
 
@@ -350,7 +402,12 @@
         return VlanId.vlanId(osNet.getProviderSegID());
     }
 
-
+    /**
+     * Obtains the VNI from the given instance port.
+     *
+     * @param instPort instance port object
+     * @return VXLAN Network Identifier (VNI)
+     */
     private Long getVni(InstancePort instPort) {
         Network osNet = osNetworkService.network(instPort.networkId());
         if (osNet == null || Strings.isNullOrEmpty(osNet.getProviderSegID())) {
@@ -362,6 +419,13 @@
         return Long.valueOf(osNet.getProviderSegID());
     }
 
+    /**
+     * An internal instance port listener which listens the port events generated
+     * from VM. The corresponding L2 forwarding rules will be generated and
+     * inserted to integration bridge only if a new VM port is detected. If the
+     * existing detected VM port is removed due to VM purge, we will remove the
+     * corresponding L2 forwarding to as well for the sake of resource saving.
+     */
     private class InternalInstancePortListener implements InstancePortListener {
 
         @Override