[CORD-2785] Refactor McastHandler and move to separated folder

Change-Id: I1f8ef7d51d57c90c4d29d79fa7e1ac9d0dbd4676
(cherry picked from commit 004d7cf78bf0228e23cbd17d19b2856c339c53e8)
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index 41bb452..559f60c 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -100,6 +100,8 @@
 import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
 import org.onosproject.segmentrouting.grouphandler.DestinationSet;
 import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
+import org.onosproject.segmentrouting.mcast.McastHandler;
+import org.onosproject.segmentrouting.mcast.McastRole;
 import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
 import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
 import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
@@ -164,7 +166,7 @@
     private NeighbourResolutionService neighbourResolutionService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    CoreService coreService;
+    public CoreService coreService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     PacketService packetService;
@@ -176,7 +178,7 @@
     HostLocationProbingService probingService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    DeviceService deviceService;
+    public DeviceService deviceService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     DeviceAdminService deviceAdminService;
@@ -185,7 +187,7 @@
     public FlowObjectiveService flowObjectiveService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    LinkService linkService;
+    public LinkService linkService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     public MastershipService mastershipService;
@@ -194,7 +196,7 @@
     public StorageService storageService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    MulticastRouteService multicastRouteService;
+    public MulticastRouteService multicastRouteService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     public TopologyService topologyService;
@@ -232,7 +234,7 @@
     private InternalLinkListener linkListener = null;
     private InternalDeviceListener deviceListener = null;
     private AppConfigHandler appCfgHandler = null;
-    XConnectHandler xConnectHandler = null;
+    public XConnectHandler xConnectHandler = null;
     McastHandler mcastHandler = null;
     HostHandler hostHandler = null;
     private RouteHandler routeHandler = null;
@@ -706,7 +708,7 @@
     }
 
     @Override
-    public Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp) {
+    public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
         return mcastHandler.getMcastRoles(mcastIp);
     }
 
@@ -847,7 +849,7 @@
      * @param connectPoint connect point
      * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
      */
-    VlanId getInternalVlanId(ConnectPoint connectPoint) {
+    public VlanId getInternalVlanId(ConnectPoint connectPoint) {
         VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
         VlanId nativeVlanId = getNativeVlanId(connectPoint);
         return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingService.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingService.java
index fa7b7d6..759495b 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingService.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingService.java
@@ -22,6 +22,7 @@
 import org.onosproject.net.Link;
 import org.onosproject.net.PortNumber;
 import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
+import org.onosproject.segmentrouting.mcast.McastRole;
 import org.onosproject.segmentrouting.pwaas.L2Tunnel;
 import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
 import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
@@ -215,7 +216,7 @@
      * @param mcastIp the group ip
      * @return the mapping mcastIp-device to mcast role
      */
-    Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp);
+    Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp);
 
     /**
      * Returns the associated paths to the mcast group.
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/McastTreeListCommand.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/McastTreeListCommand.java
index 73cbb1a..dc86bef 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/McastTreeListCommand.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/cli/McastTreeListCommand.java
@@ -24,8 +24,8 @@
 import org.onosproject.cli.AbstractShellCommand;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
-import org.onosproject.segmentrouting.McastHandler.McastRole;
 import org.onosproject.segmentrouting.SegmentRoutingService;
+import org.onosproject.segmentrouting.mcast.McastRole;
 import org.onosproject.segmentrouting.storekey.McastStoreKey;
 
 import java.util.Collection;
@@ -35,9 +35,9 @@
 import java.util.stream.Collectors;
 
 import static com.google.common.base.Strings.isNullOrEmpty;
-import static org.onosproject.segmentrouting.McastHandler.McastRole.EGRESS;
-import static org.onosproject.segmentrouting.McastHandler.McastRole.INGRESS;
-import static org.onosproject.segmentrouting.McastHandler.McastRole.TRANSIT;
+import static org.onosproject.segmentrouting.mcast.McastRole.EGRESS;
+import static org.onosproject.segmentrouting.mcast.McastRole.INGRESS;
+import static org.onosproject.segmentrouting.mcast.McastRole.TRANSIT;
 
 /**
  * Command to show the list of mcast trees.
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/McastHandler.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastHandler.java
similarity index 96%
rename from apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/McastHandler.java
rename to apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastHandler.java
index 95e264d..cd59ee5 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/McastHandler.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastHandler.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016-present Open Networking Foundation
+ * 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.
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package org.onosproject.segmentrouting;
+package org.onosproject.segmentrouting.mcast;
 
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
@@ -54,6 +54,8 @@
 import org.onosproject.net.mcast.McastRouteInfo;
 import org.onosproject.net.topology.Topology;
 import org.onosproject.net.topology.TopologyService;
+import org.onosproject.segmentrouting.SegmentRoutingManager;
+import org.onosproject.segmentrouting.SegmentRoutingService;
 import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
 import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
 import org.onosproject.segmentrouting.storekey.McastStoreKey;
@@ -85,7 +87,7 @@
 import static org.onosproject.segmentrouting.SegmentRoutingManager.INTERNAL_VLAN;
 
 /**
- * Handles multicast-related events.
+ * Handles Multicast related events.
  */
 public class McastHandler {
     private static final Logger log = LoggerFactory.getLogger(McastHandler.class);
@@ -141,24 +143,6 @@
             = newScheduledThreadPool(1, groupedThreads("mcastBktCorrector", "mcastbktC-%d", log));
 
     /**
-     * Role in the multicast tree.
-     */
-    public enum McastRole {
-        /**
-         * The device is the ingress device of this group.
-         */
-        INGRESS,
-        /**
-         * The device is the transit device of this group.
-         */
-        TRANSIT,
-        /**
-         * The device is the egress device of this group.
-         */
-        EGRESS
-    }
-
-    /**
      * Constructs the McastEventHandler.
      *
      * @param srManager Segment Routing manager
@@ -191,7 +175,7 @@
     /**
      * Read initial multicast from mcast store.
      */
-    protected void init() {
+    public void init() {
         srManager.multicastRouteService.getRoutes().forEach(mcastRoute -> {
             ConnectPoint source = srManager.multicastRouteService.fetchSource(mcastRoute);
             Set<ConnectPoint> sinks = srManager.multicastRouteService.fetchSinks(mcastRoute);
@@ -204,7 +188,7 @@
     /**
      * Clean up when deactivating the application.
      */
-    protected void terminate() {
+    public void terminate() {
         executorService.shutdown();
     }
 
@@ -213,7 +197,7 @@
      *
      * @param event McastEvent with SOURCE_ADDED type
      */
-    protected void processSourceAdded(McastEvent event) {
+    public void processSourceAdded(McastEvent event) {
         log.info("processSourceAdded {}", event);
         McastRouteInfo mcastRouteInfo = event.subject();
         if (!mcastRouteInfo.isComplete()) {
@@ -232,7 +216,7 @@
      *
      * @param event McastEvent with SOURCE_UPDATED type
      */
-    protected void processSourceUpdated(McastEvent event) {
+    public void processSourceUpdated(McastEvent event) {
         log.info("processSourceUpdated {}", event);
         // Get old and new data
         McastRouteInfo mcastRouteInfo = event.subject();
@@ -250,7 +234,7 @@
      *
      * @param event McastEvent with SINK_ADDED type
      */
-    protected void processSinkAdded(McastEvent event) {
+    public void processSinkAdded(McastEvent event) {
         log.info("processSinkAdded {}", event);
         McastRouteInfo mcastRouteInfo = event.subject();
         if (!mcastRouteInfo.isComplete()) {
@@ -269,7 +253,7 @@
      *
      * @param event McastEvent with SINK_REMOVED type
      */
-    protected void processSinkRemoved(McastEvent event) {
+    public void processSinkRemoved(McastEvent event) {
         log.info("processSinkRemoved {}", event);
         McastRouteInfo mcastRouteInfo = event.subject();
         if (!mcastRouteInfo.isComplete()) {
@@ -288,7 +272,7 @@
      *
      * @param event McastEvent with ROUTE_REMOVED type
      */
-    protected void processRouteRemoved(McastEvent event) {
+    public void processRouteRemoved(McastEvent event) {
         log.info("processRouteRemoved {}", event);
         McastRouteInfo mcastRouteInfo = event.subject();
         if (!mcastRouteInfo.source().isPresent()) {
@@ -522,7 +506,7 @@
      *
      * @param affectedLink Link that is going down
      */
-    protected void processLinkDown(Link affectedLink) {
+    public void processLinkDown(Link affectedLink) {
         lastMcastChange = Instant.now();
         mcastLock();
         try {
@@ -587,7 +571,7 @@
      *
      * @param deviceDown device going down
      */
-    protected void processDeviceDown(DeviceId deviceDown) {
+    public void processDeviceDown(DeviceId deviceDown) {
         lastMcastChange = Instant.now();
         mcastLock();
         try {
@@ -690,7 +674,7 @@
         // Do nothing if the port is configured as suppressed
         ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
         SegmentRoutingAppConfig appConfig = srManager.cfgService
-                .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
+                .getConfig(srManager.appId(), SegmentRoutingAppConfig.class);
         if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
             log.info("Ignore suppressed port {}", connectPoint);
             return;
@@ -904,7 +888,7 @@
 
         NextObjective.Builder nextObjBuilder = DefaultNextObjective
                 .builder().withId(nextId)
-                .withType(NextObjective.Type.BROADCAST).fromApp(srManager.appId)
+                .withType(NextObjective.Type.BROADCAST).fromApp(srManager.appId())
                 .withMeta(metadata);
 
         outPorts.forEach(port -> {
@@ -949,7 +933,7 @@
                 .withMeta(metabuilder.build())
                 .nextStep(nextId)
                 .withFlag(ForwardingObjective.Flag.SPECIFIC)
-                .fromApp(srManager.appId)
+                .fromApp(srManager.appId())
                 .withPriority(SegmentRoutingService.DEFAULT_PRIORITY);
         return fwdBuilder;
     }
@@ -983,7 +967,7 @@
                 .pushVlan().setVlanId(assignedVlan).build();
         filtBuilder.withMeta(tt);
 
-        return filtBuilder.permit().fromApp(srManager.appId);
+        return filtBuilder.permit().fromApp(srManager.appId());
     }
 
     /**
@@ -1015,11 +999,11 @@
         boolean isPairLink = true;
         try {
             // If one of this condition is not true; it is not a pair link
-            if (!(srManager.deviceConfiguration.isEdgeDevice(srcId) &&
-                  srManager.deviceConfiguration.isEdgeDevice(dstId) &&
-                  srManager.deviceConfiguration.getPairDeviceId(srcId).equals(dstId) &&
-                  srManager.deviceConfiguration.getPairLocalPort(srcId).equals(srcPort) &&
-                  srManager.deviceConfiguration.getPairLocalPort(dstId).equals(dstPort))) {
+            if (!(srManager.deviceConfiguration().isEdgeDevice(srcId) &&
+                  srManager.deviceConfiguration().isEdgeDevice(dstId) &&
+                  srManager.deviceConfiguration().getPairDeviceId(srcId).equals(dstId) &&
+                  srManager.deviceConfiguration().getPairLocalPort(srcId).equals(srcPort) &&
+                  srManager.deviceConfiguration().getPairLocalPort(dstId).equals(dstPort))) {
                     isPairLink = false;
                 }
         } catch (DeviceConfigNotFoundException e) {
@@ -1231,8 +1215,8 @@
 
             for (PortNumber port : ports) {
                 // Spine-facing port should have no subnet and no xconnect
-                if (srManager.deviceConfiguration != null &&
-                        srManager.deviceConfiguration.getPortSubnets(ingressDevice, port).isEmpty() &&
+                if (srManager.deviceConfiguration() != null &&
+                        srManager.deviceConfiguration().getPortSubnets(ingressDevice, port).isEmpty() &&
                         !srManager.xConnectHandler.hasXConnect(new ConnectPoint(ingressDevice, port))) {
                     return port;
                 }
@@ -1264,8 +1248,8 @@
                 // Tries to find at least one port that is not spine-facing
                 for (PortNumber port : ports) {
                     // Spine-facing port should have no subnet and no xconnect
-                    if (srManager.deviceConfiguration != null &&
-                            (!srManager.deviceConfiguration.getPortSubnets(deviceId, port).isEmpty() ||
+                    if (srManager.deviceConfiguration() != null &&
+                            (!srManager.deviceConfiguration().getPortSubnets(deviceId, port).isEmpty() ||
                             srManager.xConnectHandler.hasXConnect(new ConnectPoint(deviceId, port)))) {
                         return true;
                     }
@@ -1287,7 +1271,7 @@
         // Do nothing if the port is configured as suppressed
         ConnectPoint connectPoint = new ConnectPoint(deviceId, port);
         SegmentRoutingAppConfig appConfig = srManager.cfgService
-                .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
+                .getConfig(srManager.appId(), SegmentRoutingAppConfig.class);
         if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
             log.info("Ignore suppressed port {}", connectPoint);
             return;
@@ -1314,7 +1298,7 @@
      * @param vlanId assigned VLAN ID
      * @param install true to add, false to remove
      */
-    protected void updateFilterToDevice(DeviceId deviceId, PortNumber portNum,
+    public void updateFilterToDevice(DeviceId deviceId, PortNumber portNum,
                                         VlanId vlanId, boolean install) {
         lastMcastChange = Instant.now();
         mcastLock();
@@ -1465,7 +1449,7 @@
                                           entry -> entry.getValue().value().id()));
     }
 
-    public Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp) {
+    public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
         // If mcast ip is present
         if (mcastIp != null) {
             return mcastRoleStore.entrySet().stream()
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastRole.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastRole.java
new file mode 100644
index 0000000..454bb5c
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/McastRole.java
@@ -0,0 +1,35 @@
+/*
+ * 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.segmentrouting.mcast;
+
+/**
+ * Enum that defines role in the multicast tree.
+ */
+public enum McastRole {
+    /**
+     * The device is the ingress device of this group.
+     */
+    INGRESS,
+    /**
+     * The device is the transit device of this group.
+     */
+    TRANSIT,
+    /**
+     * The device is the egress device of this group.
+     */
+    EGRESS
+}
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/package-info.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/package-info.java
new file mode 100644
index 0000000..dfdd39a
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/mcast/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+
+/**
+ * Trellis Multicast service.
+ */
+package org.onosproject.segmentrouting.mcast;