【ONOS-1223】【ONOS-1870】the implements of label resource APIs.it include
commands
used to test
if there is any bug,LabelResourceManager,LabelResourceStore using
copycat,and junit test code.
the distribution strategy is that the master of devices handle all the
requests if applied label belongs to it.except for query request.
label store uses copycat instead of hazelcast to keep strong consistency

Change-Id: I77bde6a96f33098063573d37ed1ba787ae21973f
diff --git a/cli/src/main/java/org/onosproject/cli/net/ApplyGlobalLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/ApplyGlobalLabelResourceCommand.java
new file mode 100644
index 0000000..6cf924a
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/ApplyGlobalLabelResourceCommand.java
@@ -0,0 +1,39 @@
+package org.onosproject.cli.net;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.resource.DefaultLabelResource;
+import org.onosproject.net.resource.LabelResource;
+import org.onosproject.net.resource.LabelResourceService;
+
+@Command(scope = "onos", name = "apply-global-label-resource-pool",
+      description = "Apply global labels from global resource pool")
+public class ApplyGlobalLabelResourceCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "applyNum",
+            description = "Applying number means how many labels applications want to use.",
+            required = true, multiValued = false)
+    String applyNum = null;
+
+    private static final String FMT = "deviceid=%s, labelresourceid=%s";
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        Collection<LabelResource> result =
+                lrs.applyFromGlobalPool(Long.parseLong(applyNum));
+        if (result.size() > 0) {
+            for (Iterator<LabelResource> iterator = result.iterator(); iterator
+                    .hasNext();) {
+                DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator
+                        .next();
+                print(FMT, defaultLabelResource.deviceId().toString(),
+                      defaultLabelResource.labelResourceId().toString());
+            }
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/ApplyLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/ApplyLabelResourceCommand.java
new file mode 100644
index 0000000..06cd8e6
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/ApplyLabelResourceCommand.java
@@ -0,0 +1,44 @@
+package org.onosproject.cli.net;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.DefaultLabelResource;
+import org.onosproject.net.resource.LabelResource;
+import org.onosproject.net.resource.LabelResourceService;
+
+@Command(scope = "onos", name = "apply-label-resource-pool",
+      description = "Apply label resource from device pool by specific device id")
+public class ApplyLabelResourceCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "deviceId",
+            description = "Device identity",
+            required = true, multiValued = false)
+    String deviceId = null;
+    @Argument(index = 1, name = "applyNum",
+            description = "Applying number means how many labels applications want to use.",
+            required = true, multiValued = false)
+    String applyNum = null;
+
+    private static final String FMT = "deviceid=%s, labelresourceid=%s";
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        Collection<LabelResource> result = lrs.applyFromDevicePool(DeviceId
+                .deviceId(deviceId), Long.parseLong(applyNum));
+        if (result.size() > 0) {
+            for (Iterator<LabelResource> iterator = result.iterator(); iterator
+                    .hasNext();) {
+                DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator
+                        .next();
+                print(FMT, defaultLabelResource.deviceId().toString(),
+                      defaultLabelResource.labelResourceId().toString());
+            }
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/CreateGlobalLabelResourcePoolCommand.java b/cli/src/main/java/org/onosproject/cli/net/CreateGlobalLabelResourcePoolCommand.java
new file mode 100644
index 0000000..0be80cf
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/CreateGlobalLabelResourcePoolCommand.java
@@ -0,0 +1,32 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.resource.LabelResourceAdminService;
+import org.onosproject.net.resource.LabelResourceId;
+
+/**
+ * create label resource pool by specific device id.
+ */
+@Command(scope = "onos", name = "create-global-label-resource-pool",
+description = "Creates global label resource pool.")
+public class CreateGlobalLabelResourcePoolCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "beginLabel",
+            description = "The first label of global label resource pool.",
+            required = true, multiValued = false)
+    String beginLabel = null;
+    @Argument(index = 1, name = "endLabel",
+            description = "The last label of global label resource pool.",
+            required = true, multiValued = false)
+    String endLabel = null;
+
+    @Override
+    protected void execute() {
+        LabelResourceAdminService lrs = get(LabelResourceAdminService.class);
+        lrs.createGlobalPool(LabelResourceId.labelResourceId(Long
+                .parseLong(beginLabel)), LabelResourceId.labelResourceId(Long
+                .parseLong(endLabel)));
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/CreateLabelResourcePoolCommand.java b/cli/src/main/java/org/onosproject/cli/net/CreateLabelResourcePoolCommand.java
new file mode 100644
index 0000000..5a7d6b4
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/CreateLabelResourcePoolCommand.java
@@ -0,0 +1,33 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.LabelResourceAdminService;
+import org.onosproject.net.resource.LabelResourceId;
+
+/**
+ * create label resource pool by specific device id.
+ */
+@Command(scope = "onos", name = "create-label-resource-pool",
+     description = "Creates label resource pool by a specific device id")
+public class CreateLabelResourcePoolCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "deviceId", description = "Device identity", required = true, multiValued = false)
+    String deviceId = null;
+    @Argument(index = 1, name = "beginLabel",
+            description = "The first label of global label resource pool.", required = true, multiValued = false)
+    String beginLabel = null;
+    @Argument(index = 2, name = "endLabel",
+            description = "The last label of global label resource pool.", required = true, multiValued = false)
+    String endLabel = null;
+
+    @Override
+    protected void execute() {
+        LabelResourceAdminService lrs = get(LabelResourceAdminService.class);
+        lrs.createDevicePool(DeviceId.deviceId(deviceId), LabelResourceId
+                .labelResourceId(Long.parseLong(beginLabel)), LabelResourceId
+                .labelResourceId(Long.parseLong(endLabel)));
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/DestroyGlobalLabelResourcePoolCommand.java b/cli/src/main/java/org/onosproject/cli/net/DestroyGlobalLabelResourcePoolCommand.java
new file mode 100644
index 0000000..8921044
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/DestroyGlobalLabelResourcePoolCommand.java
@@ -0,0 +1,16 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.resource.LabelResourceAdminService;
+
+@Command(scope = "onos", name = "destroy-global-label-resource-pool",
+description = "Destroys global label resource pool")
+public class DestroyGlobalLabelResourcePoolCommand extends AbstractShellCommand {
+    @Override
+    protected void execute() {
+        LabelResourceAdminService lrs = get(LabelResourceAdminService.class);
+        lrs.destroyGlobalPool();
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/DestroyLabelResourcePoolCommand.java b/cli/src/main/java/org/onosproject/cli/net/DestroyLabelResourcePoolCommand.java
new file mode 100644
index 0000000..7965dc6
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/DestroyLabelResourcePoolCommand.java
@@ -0,0 +1,21 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.LabelResourceAdminService;
+
+@Command(scope = "onos", name = "destroy-label-resource-pool",
+    description = "Destroys label resource pool by a specific device id")
+public class DestroyLabelResourcePoolCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "deviceId", description = "Device identity", required = true, multiValued = false)
+    String deviceId = null;
+
+    @Override
+    protected void execute() {
+        LabelResourceAdminService lrs = get(LabelResourceAdminService.class);
+        lrs.destroyDevicePool(DeviceId.deviceId(deviceId));
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/GetGlobalLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/GetGlobalLabelResourceCommand.java
new file mode 100644
index 0000000..5ff98ba
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/GetGlobalLabelResourceCommand.java
@@ -0,0 +1,27 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.resource.LabelResourcePool;
+import org.onosproject.net.resource.LabelResourceService;
+
+@Command(scope = "onos", name = "get-global-label-resource-pool",
+      description = "Gets global label resource pool information.")
+public class GetGlobalLabelResourceCommand extends AbstractShellCommand {
+    private static final String FMT = "deviceid=%s, beginLabel=%s,"
+            + "endLabel=%s, totalNum=%s, usedNum=%s, currentUsedMaxLabelId=%s,"
+            + "releaseLabelIds=%s";
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        LabelResourcePool pool = lrs.getGlobalLabelResourcePool();
+        if (pool != null) {
+            print(FMT, pool.deviceId().toString(), pool.beginLabel(),
+                  pool.endLabel(), pool.totalNum(), pool.usedNum(),
+                  pool.currentUsedMaxLabelId(), pool.releaseLabelId()
+                          .toString());
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/GetLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/GetLabelResourceCommand.java
new file mode 100644
index 0000000..cd10add
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/GetLabelResourceCommand.java
@@ -0,0 +1,35 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.LabelResourcePool;
+import org.onosproject.net.resource.LabelResourceService;
+
+@Command(scope = "onos", name = "get-label-resource-pool",
+      description = "Gets label resource pool information by a specific device id")
+public class GetLabelResourceCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "deviceId",
+            description = "Device identity", required = true, multiValued = false)
+    String deviceId = null;
+    private static final String FMT = "deviceid=%s, beginLabel=%s,"
+            + "endLabel=%s, totalNum=%s, usedNum=%s, currentUsedMaxLabelId=%s,"
+            + "releaseLabelIds=%s";
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        LabelResourcePool pool = lrs.getDeviceLabelResourcePool(DeviceId
+                .deviceId(deviceId));
+        if (pool != null) {
+            print(FMT, pool.deviceId().toString(), pool.beginLabel(),
+                  pool.endLabel(), pool.totalNum(), pool.usedNum(),
+                  pool.currentUsedMaxLabelId(), pool.releaseLabelId()
+                          .toString());
+        } else {
+            print(FMT, deviceId, null, null, null, null, null, null);
+        }
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/ReleaseGlobalLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/ReleaseGlobalLabelResourceCommand.java
new file mode 100644
index 0000000..9596061
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/ReleaseGlobalLabelResourceCommand.java
@@ -0,0 +1,33 @@
+package org.onosproject.cli.net;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.resource.LabelResourceId;
+import org.onosproject.net.resource.LabelResourceService;
+
+@Command(scope = "onos", name = "release-global-label-resource-pool",
+description = "Releases labels to global label resource pool.")
+public class ReleaseGlobalLabelResourceCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "releaseLabelIds",
+            description = "Represents for the label ids that are released. They are splited by dot symbol",
+            required = true, multiValued = false)
+    String releaseLabelIds = null;
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        Set<LabelResourceId> release = new HashSet<LabelResourceId>();
+        String[] labelIds = releaseLabelIds.split(",");
+        LabelResourceId resource = null;
+        for (int i = 0; i < labelIds.length; i++) {
+            resource = LabelResourceId.labelResourceId(Long.parseLong(labelIds[i]));
+            release.add(resource);
+        }
+        lrs.releaseToGlobalPool(release);
+    }
+
+}
diff --git a/cli/src/main/java/org/onosproject/cli/net/ReleaseLabelResourceCommand.java b/cli/src/main/java/org/onosproject/cli/net/ReleaseLabelResourceCommand.java
new file mode 100644
index 0000000..55c44c4
--- /dev/null
+++ b/cli/src/main/java/org/onosproject/cli/net/ReleaseLabelResourceCommand.java
@@ -0,0 +1,44 @@
+package org.onosproject.cli.net;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.resource.DefaultLabelResource;
+import org.onosproject.net.resource.LabelResource;
+import org.onosproject.net.resource.LabelResourceId;
+import org.onosproject.net.resource.LabelResourceService;
+
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
+@Command(scope = "onos", name = "release-label-resource-pool",
+description = "Releases label ids to label resource pool by a specific device id")
+public class ReleaseLabelResourceCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "deviceId",
+            description = "Device identity",
+            required = true, multiValued = false)
+    String deviceId = null;
+    @Argument(index = 1, name = "releaseLabelIds",
+            description = "Represents for the label ids that are released. They are splited by dot symbol",
+            required = true, multiValued = false)
+    String releaseLabelIds = null;
+
+    @Override
+    protected void execute() {
+        LabelResourceService lrs = get(LabelResourceService.class);
+        Multimap<DeviceId, LabelResource> map = ArrayListMultimap
+                .create();
+        String[] labelIds = releaseLabelIds.split(",");
+        DefaultLabelResource resource = null;
+        for (int i = 0; i < labelIds.length; i++) {
+            resource = new DefaultLabelResource(
+                                                DeviceId.deviceId(deviceId),
+                                                LabelResourceId.labelResourceId(Long
+                                                        .parseLong(labelIds[i])));
+            map.put(DeviceId.deviceId(deviceId), resource);
+        }
+        lrs.releaseToDevicePool(map);
+    }
+
+}
diff --git a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 8a303dc..d530ca8 100644
--- a/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/cli/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -38,7 +38,6 @@
                 <ref component-id="cfgCommandCompleter"/>
                 <ref component-id="componentNameCompleter"/>
                 <ref component-id="componentPropertyNameCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -67,7 +66,6 @@
             <action class="org.onosproject.cli.net.DriversListCommand"/>
             <completers>
                 <ref component-id="driverNameCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -78,14 +76,12 @@
             <action class="org.onosproject.cli.net.DevicePortsListCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.DeviceRemoveCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -94,14 +90,12 @@
                 <ref component-id="deviceIdCompleter"/>
                 <ref component-id="nodeIdCompleter"/>
                 <ref component-id="roleCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.AnnotateDeviceCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -109,7 +103,6 @@
             <action class="org.onosproject.cli.net.LinksListCommand"/>
             <completers>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -121,7 +114,6 @@
             <completers>
                 <ref component-id="deviceIdCompleter"/>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -133,7 +125,6 @@
             <completers>
                 <ref component-id="appIdWithIntentNameCompleter"/>
                 <ref component-id="intentIdCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -141,7 +132,6 @@
             <completers>
                 <ref component-id="hostIdCompleter"/>
                 <ref component-id="hostIdCompleter"/>
-                <null/>
             </completers>
             <optional-completers>
                 <entry key="-t" value-ref="ethTypeCompleter"/>
@@ -157,7 +147,6 @@
             <completers>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
-                <null/>
             </completers>
             <optional-completers>
                 <entry key="-t" value-ref="ethTypeCompleter"/>
@@ -173,7 +162,6 @@
             <completers>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
-                <null/>
             </completers>
             <optional-completers>
                 <entry key="-a" value-ref="allAppNameCompleter"/>
@@ -219,7 +207,6 @@
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="nullCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -228,14 +215,12 @@
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="nullCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.RandomIntentCommand"/>
             <completers>
                 <ref component-id="nullCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -243,7 +228,6 @@
             <completers>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -251,7 +235,6 @@
             <completers>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -281,14 +264,12 @@
             <action class="org.onosproject.cli.net.ClusterDevicesCommand"/>
             <completers>
                 <ref component-id="clusterIdCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
             <action class="org.onosproject.cli.net.ClusterLinksCommand"/>
             <completers>
                 <ref component-id="clusterIdCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -299,7 +280,6 @@
             <action class="org.onosproject.cli.net.HostRemoveCommand"/>
             <completers>
                 <ref component-id="hostIdCompleter"/>
-                <null/>
             </completers>
         </command>
         <command>
@@ -319,7 +299,6 @@
             <completers>
                 <ref component-id="flowRuleStatusCompleter"/>
                 <ref component-id="deviceIdCompleter"/>
-                <null/>
             </completers>
         </command>
 
@@ -338,7 +317,6 @@
             <completers>
                 <ref component-id="connectPointCompleter"/>
                 <ref component-id="connectPointCompleter"/>
-                <null/>
             </completers>
             <optional-completers>
                 <entry key="-t" value-ref="ethTypeCompleter"/>
@@ -349,6 +327,37 @@
                 <entry key="-a" value-ref="allAppNameCompleter"/>
             </optional-completers>
         </command>
+        
+        <command>
+            <action class="org.onosproject.cli.net.GetGlobalLabelResourceCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.GetLabelResourceCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.CreateGlobalLabelResourcePoolCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.CreateLabelResourcePoolCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.DestroyGlobalLabelResourcePoolCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.DestroyGlobalLabelResourcePoolCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.ReleaseGlobalLabelResourceCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.ReleaseLabelResourceCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.ApplyGlobalLabelResourceCommand"/>
+        </command>
+        <command>
+            <action class="org.onosproject.cli.net.ApplyLabelResourceCommand"/>
+        </command>
     </command-bundle>
 
     <bean id="appCommandCompleter" class="org.onosproject.cli.app.ApplicationCommandCompleter"/>