Initial BUCK build

Change-Id: I64e8a979301989062f4545a4841b06c15a372049
diff --git a/core/api/BUCK b/core/api/BUCK
new file mode 100644
index 0000000..387c502
--- /dev/null
+++ b/core/api/BUCK
@@ -0,0 +1,34 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-api'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:COMPILE',
+    '//utils/misc:onlab-misc',
+    '//utils/osgi:onlab-osgi',
+    '//utils/rest:onlab-rest',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    resources_root = 'src/test/resources',
+    resources = glob(['src/test/resources/**']),
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'onos-api-tests',
+    visibility = ['PUBLIC'],
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/common/BUCK b/core/common/BUCK
new file mode 100644
index 0000000..cbe9efc
--- /dev/null
+++ b/core/common/BUCK
@@ -0,0 +1,33 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-common'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+SRC_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/api:onos-incubator-api',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+    '//core/api:onos-api-tests',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = SRC_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'onos-core-common-tests',
+    visibility = ['PUBLIC'],
+    srcs = glob([TEST + '/*.java']),
+    resources_root = 'src/test/resources',
+    resources = glob(['src/test/resources/**']),
+    deps = SRC_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/net/BUCK b/core/net/BUCK
new file mode 100644
index 0000000..42cb80c
--- /dev/null
+++ b/core/net/BUCK
@@ -0,0 +1,29 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/main/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-net'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/api:onos-incubator-api',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/net/src/main/java/org/onosproject/net/statistic/impl/FlowStatisticManager.java b/core/net/src/main/java/org/onosproject/net/statistic/impl/FlowStatisticManager.java
index 859c4da..23b981e 100644
--- a/core/net/src/main/java/org/onosproject/net/statistic/impl/FlowStatisticManager.java
+++ b/core/net/src/main/java/org/onosproject/net/statistic/impl/FlowStatisticManager.java
@@ -25,9 +25,10 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
-import org.onosproject.utils.Comparators;
+
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Device;
+import org.onosproject.net.ElementId;
 import org.onosproject.net.Port;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.device.DeviceService;
@@ -49,6 +50,7 @@
 import org.slf4j.Logger;
 
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -81,6 +83,33 @@
 
     private final InternalFlowRuleStatsListener frListener = new InternalFlowRuleStatsListener();
 
+    // FIXME: refactor these comparators to be shared with the CLI implmentations
+    public static final Comparator<ElementId> ELEMENT_ID_COMPARATOR = new Comparator<ElementId>() {
+        @Override
+        public int compare(ElementId id1, ElementId id2) {
+            return id1.toString().compareTo(id2.toString());
+        }
+    };
+
+    public static final Comparator<ConnectPoint> CONNECT_POINT_COMPARATOR = new Comparator<ConnectPoint>() {
+        @Override
+        public int compare(ConnectPoint o1, ConnectPoint o2) {
+            int compareId = ELEMENT_ID_COMPARATOR.compare(o1.elementId(), o2.elementId());
+            return (compareId != 0) ?
+                    compareId :
+                    Long.signum(o1.port().toLong() - o2.port().toLong());
+        }
+    };
+
+    public static final Comparator<TypedFlowEntryWithLoad> TYPEFLOWENTRY_WITHLOAD_COMPARATOR =
+            new Comparator<TypedFlowEntryWithLoad>() {
+                @Override
+                public int compare(TypedFlowEntryWithLoad fe1, TypedFlowEntryWithLoad fe2) {
+                    long delta = fe1.load().rate() - fe2.load().rate();
+                    return delta == 0 ? 0 : (delta > 0 ? -1 : +1);
+                }
+            };
+
     @Activate
     public void activate() {
         flowRuleService.addListener(frListener);
@@ -97,7 +126,7 @@
     public Map<ConnectPoint, SummaryFlowEntryWithLoad> loadSummary(Device device) {
         checkPermission(STATISTIC_READ);
 
-        Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR);
+        Map<ConnectPoint, SummaryFlowEntryWithLoad> summaryLoad = new TreeMap<>(CONNECT_POINT_COMPARATOR);
 
         if (device == null) {
             return summaryLoad;
@@ -128,7 +157,7 @@
                                                                   Instruction.Type instType) {
         checkPermission(STATISTIC_READ);
 
-        Map<ConnectPoint, List<TypedFlowEntryWithLoad>> allLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR);
+        Map<ConnectPoint, List<TypedFlowEntryWithLoad>> allLoad = new TreeMap<>(CONNECT_POINT_COMPARATOR);
 
         if (device == null) {
             return allLoad;
@@ -162,7 +191,7 @@
                                                                    int topn) {
         checkPermission(STATISTIC_READ);
 
-        Map<ConnectPoint, List<TypedFlowEntryWithLoad>> allLoad = new TreeMap<>(Comparators.CONNECT_POINT_COMPARATOR);
+        Map<ConnectPoint, List<TypedFlowEntryWithLoad>> allLoad = new TreeMap<>(CONNECT_POINT_COMPARATOR);
 
         if (device == null) {
             return allLoad;
@@ -373,7 +402,7 @@
 
         // Sort with descending order of load
         List<TypedFlowEntryWithLoad> tfel =
-                fel.stream().sorted(Comparators.TYPEFLOWENTRY_WITHLOAD_COMPARATOR).
+                fel.stream().sorted(TYPEFLOWENTRY_WITHLOAD_COMPARATOR).
                         limit(topn).collect(Collectors.toList());
 
         return tfel;
diff --git a/core/security/BUCK b/core/security/BUCK
new file mode 100644
index 0000000..3618496
--- /dev/null
+++ b/core/security/BUCK
@@ -0,0 +1,29 @@
+SRC = 'src/main/java/org/onosproject/security/**/'
+TEST = 'src/test/java/org/onosproject/security/**/'
+CURRENT_NAME = 'onos-security'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//core/store/serializers:onos-core-serializers',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/store/dist/BUCK b/core/store/dist/BUCK
new file mode 100644
index 0000000..8653ad5
--- /dev/null
+++ b/core/store/dist/BUCK
@@ -0,0 +1,39 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-dist'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//core/common:onos-core-common',
+    '//utils/rest:onlab-rest',
+    '//core/store/serializers:onos-core-serializers',
+    '//lib:netty-transport',
+    '//lib:netty-codec',
+    '//lib:netty-buffer',
+    '//lib:netty-handler',
+    '//lib:netty-transport-native-epoll',
+    '//lib:commons-math3',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+     '//core/api:onos-api-tests',
+     '//core/common:onos-core-common-tests',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name= 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/store/persistence/BUCK b/core/store/persistence/BUCK
new file mode 100644
index 0000000..3caf6ea
--- /dev/null
+++ b/core/store/persistence/BUCK
@@ -0,0 +1,30 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-persistence'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//lib:catalyst-serializer',
+    '//lib:mapdb',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/store/primitives/BUCK b/core/store/primitives/BUCK
new file mode 100644
index 0000000..1e77e32
--- /dev/null
+++ b/core/store/primitives/BUCK
@@ -0,0 +1,49 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-primitives'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//core/common:onos-core-common',
+    '//incubator/api:onos-incubator-api',
+    '//core/store/serializers:onos-core-serializers',
+    '//lib:copycat-client',
+    '//lib:copycat-server',
+    '//lib:copycat-protocol',
+    '//lib:copycat-core',
+    '//lib:typesafe-config',
+    '//lib:copycat-api',
+    '//lib:copycat-state-machine',
+    '//lib:copycat-state-log',
+    '//lib:catalyst-transport',
+    '//lib:catalyst-buffer',
+    '//lib:catalyst-common',
+    '//lib:catalyst-local',
+    '//lib:catalyst-serializer',
+    '//lib:atomix',
+    '//lib:atomix-resource',
+    '//lib:atomix-variables',
+    '//lib:atomix-resource-manager',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+    '//core/api:onos-api-tests',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)
diff --git a/core/store/serializers/BUCK b/core/store/serializers/BUCK
new file mode 100644
index 0000000..050269d
--- /dev/null
+++ b/core/store/serializers/BUCK
@@ -0,0 +1,29 @@
+SRC = 'src/main/java/org/onosproject/**/'
+TEST = 'src/test/java/org/onosproject/**/'
+CURRENT_NAME = 'onos-core-serializers'
+CURRENT_TARGET = ':' + CURRENT_NAME
+
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//incubator/api:onos-incubator-api',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+]
+
+java_library(
+    name = CURRENT_NAME,
+    srcs = glob([SRC + '/*.java']),
+    deps = COMPILE_DEPS,
+    visibility = ['PUBLIC'],
+)
+
+java_test(
+    name = 'tests',
+    srcs = glob([TEST + '/*.java']),
+    deps = COMPILE_DEPS +
+           TEST_DEPS +
+           [CURRENT_TARGET],
+    source_under_test = [CURRENT_TARGET],
+)