Committed by
Gerrit Code Review
【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
Showing
17 changed files
with
1299 additions
and
22 deletions
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import java.util.Collection; | ||
4 | +import java.util.Iterator; | ||
5 | + | ||
6 | +import org.apache.karaf.shell.commands.Argument; | ||
7 | +import org.apache.karaf.shell.commands.Command; | ||
8 | +import org.onosproject.cli.AbstractShellCommand; | ||
9 | +import org.onosproject.net.resource.DefaultLabelResource; | ||
10 | +import org.onosproject.net.resource.LabelResource; | ||
11 | +import org.onosproject.net.resource.LabelResourceService; | ||
12 | + | ||
13 | +@Command(scope = "onos", name = "apply-global-label-resource-pool", | ||
14 | + description = "Apply global labels from global resource pool") | ||
15 | +public class ApplyGlobalLabelResourceCommand extends AbstractShellCommand { | ||
16 | + @Argument(index = 0, name = "applyNum", | ||
17 | + description = "Applying number means how many labels applications want to use.", | ||
18 | + required = true, multiValued = false) | ||
19 | + String applyNum = null; | ||
20 | + | ||
21 | + private static final String FMT = "deviceid=%s, labelresourceid=%s"; | ||
22 | + | ||
23 | + @Override | ||
24 | + protected void execute() { | ||
25 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
26 | + Collection<LabelResource> result = | ||
27 | + lrs.applyFromGlobalPool(Long.parseLong(applyNum)); | ||
28 | + if (result.size() > 0) { | ||
29 | + for (Iterator<LabelResource> iterator = result.iterator(); iterator | ||
30 | + .hasNext();) { | ||
31 | + DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator | ||
32 | + .next(); | ||
33 | + print(FMT, defaultLabelResource.deviceId().toString(), | ||
34 | + defaultLabelResource.labelResourceId().toString()); | ||
35 | + } | ||
36 | + } | ||
37 | + } | ||
38 | + | ||
39 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import java.util.Collection; | ||
4 | +import java.util.Iterator; | ||
5 | + | ||
6 | +import org.apache.karaf.shell.commands.Argument; | ||
7 | +import org.apache.karaf.shell.commands.Command; | ||
8 | +import org.onosproject.cli.AbstractShellCommand; | ||
9 | +import org.onosproject.net.DeviceId; | ||
10 | +import org.onosproject.net.resource.DefaultLabelResource; | ||
11 | +import org.onosproject.net.resource.LabelResource; | ||
12 | +import org.onosproject.net.resource.LabelResourceService; | ||
13 | + | ||
14 | +@Command(scope = "onos", name = "apply-label-resource-pool", | ||
15 | + description = "Apply label resource from device pool by specific device id") | ||
16 | +public class ApplyLabelResourceCommand extends AbstractShellCommand { | ||
17 | + @Argument(index = 0, name = "deviceId", | ||
18 | + description = "Device identity", | ||
19 | + required = true, multiValued = false) | ||
20 | + String deviceId = null; | ||
21 | + @Argument(index = 1, name = "applyNum", | ||
22 | + description = "Applying number means how many labels applications want to use.", | ||
23 | + required = true, multiValued = false) | ||
24 | + String applyNum = null; | ||
25 | + | ||
26 | + private static final String FMT = "deviceid=%s, labelresourceid=%s"; | ||
27 | + | ||
28 | + @Override | ||
29 | + protected void execute() { | ||
30 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
31 | + Collection<LabelResource> result = lrs.applyFromDevicePool(DeviceId | ||
32 | + .deviceId(deviceId), Long.parseLong(applyNum)); | ||
33 | + if (result.size() > 0) { | ||
34 | + for (Iterator<LabelResource> iterator = result.iterator(); iterator | ||
35 | + .hasNext();) { | ||
36 | + DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator | ||
37 | + .next(); | ||
38 | + print(FMT, defaultLabelResource.deviceId().toString(), | ||
39 | + defaultLabelResource.labelResourceId().toString()); | ||
40 | + } | ||
41 | + } | ||
42 | + } | ||
43 | + | ||
44 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Argument; | ||
4 | +import org.apache.karaf.shell.commands.Command; | ||
5 | +import org.onosproject.cli.AbstractShellCommand; | ||
6 | +import org.onosproject.net.resource.LabelResourceAdminService; | ||
7 | +import org.onosproject.net.resource.LabelResourceId; | ||
8 | + | ||
9 | +/** | ||
10 | + * create label resource pool by specific device id. | ||
11 | + */ | ||
12 | +@Command(scope = "onos", name = "create-global-label-resource-pool", | ||
13 | +description = "Creates global label resource pool.") | ||
14 | +public class CreateGlobalLabelResourcePoolCommand extends AbstractShellCommand { | ||
15 | + @Argument(index = 0, name = "beginLabel", | ||
16 | + description = "The first label of global label resource pool.", | ||
17 | + required = true, multiValued = false) | ||
18 | + String beginLabel = null; | ||
19 | + @Argument(index = 1, name = "endLabel", | ||
20 | + description = "The last label of global label resource pool.", | ||
21 | + required = true, multiValued = false) | ||
22 | + String endLabel = null; | ||
23 | + | ||
24 | + @Override | ||
25 | + protected void execute() { | ||
26 | + LabelResourceAdminService lrs = get(LabelResourceAdminService.class); | ||
27 | + lrs.createGlobalPool(LabelResourceId.labelResourceId(Long | ||
28 | + .parseLong(beginLabel)), LabelResourceId.labelResourceId(Long | ||
29 | + .parseLong(endLabel))); | ||
30 | + } | ||
31 | + | ||
32 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Argument; | ||
4 | +import org.apache.karaf.shell.commands.Command; | ||
5 | +import org.onosproject.cli.AbstractShellCommand; | ||
6 | +import org.onosproject.net.DeviceId; | ||
7 | +import org.onosproject.net.resource.LabelResourceAdminService; | ||
8 | +import org.onosproject.net.resource.LabelResourceId; | ||
9 | + | ||
10 | +/** | ||
11 | + * create label resource pool by specific device id. | ||
12 | + */ | ||
13 | +@Command(scope = "onos", name = "create-label-resource-pool", | ||
14 | + description = "Creates label resource pool by a specific device id") | ||
15 | +public class CreateLabelResourcePoolCommand extends AbstractShellCommand { | ||
16 | + @Argument(index = 0, name = "deviceId", description = "Device identity", required = true, multiValued = false) | ||
17 | + String deviceId = null; | ||
18 | + @Argument(index = 1, name = "beginLabel", | ||
19 | + description = "The first label of global label resource pool.", required = true, multiValued = false) | ||
20 | + String beginLabel = null; | ||
21 | + @Argument(index = 2, name = "endLabel", | ||
22 | + description = "The last label of global label resource pool.", required = true, multiValued = false) | ||
23 | + String endLabel = null; | ||
24 | + | ||
25 | + @Override | ||
26 | + protected void execute() { | ||
27 | + LabelResourceAdminService lrs = get(LabelResourceAdminService.class); | ||
28 | + lrs.createDevicePool(DeviceId.deviceId(deviceId), LabelResourceId | ||
29 | + .labelResourceId(Long.parseLong(beginLabel)), LabelResourceId | ||
30 | + .labelResourceId(Long.parseLong(endLabel))); | ||
31 | + } | ||
32 | + | ||
33 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Command; | ||
4 | +import org.onosproject.cli.AbstractShellCommand; | ||
5 | +import org.onosproject.net.resource.LabelResourceAdminService; | ||
6 | + | ||
7 | +@Command(scope = "onos", name = "destroy-global-label-resource-pool", | ||
8 | +description = "Destroys global label resource pool") | ||
9 | +public class DestroyGlobalLabelResourcePoolCommand extends AbstractShellCommand { | ||
10 | + @Override | ||
11 | + protected void execute() { | ||
12 | + LabelResourceAdminService lrs = get(LabelResourceAdminService.class); | ||
13 | + lrs.destroyGlobalPool(); | ||
14 | + } | ||
15 | + | ||
16 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Argument; | ||
4 | +import org.apache.karaf.shell.commands.Command; | ||
5 | +import org.onosproject.cli.AbstractShellCommand; | ||
6 | +import org.onosproject.net.DeviceId; | ||
7 | +import org.onosproject.net.resource.LabelResourceAdminService; | ||
8 | + | ||
9 | +@Command(scope = "onos", name = "destroy-label-resource-pool", | ||
10 | + description = "Destroys label resource pool by a specific device id") | ||
11 | +public class DestroyLabelResourcePoolCommand extends AbstractShellCommand { | ||
12 | + @Argument(index = 0, name = "deviceId", description = "Device identity", required = true, multiValued = false) | ||
13 | + String deviceId = null; | ||
14 | + | ||
15 | + @Override | ||
16 | + protected void execute() { | ||
17 | + LabelResourceAdminService lrs = get(LabelResourceAdminService.class); | ||
18 | + lrs.destroyDevicePool(DeviceId.deviceId(deviceId)); | ||
19 | + } | ||
20 | + | ||
21 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Command; | ||
4 | +import org.onosproject.cli.AbstractShellCommand; | ||
5 | +import org.onosproject.net.resource.LabelResourcePool; | ||
6 | +import org.onosproject.net.resource.LabelResourceService; | ||
7 | + | ||
8 | +@Command(scope = "onos", name = "get-global-label-resource-pool", | ||
9 | + description = "Gets global label resource pool information.") | ||
10 | +public class GetGlobalLabelResourceCommand extends AbstractShellCommand { | ||
11 | + private static final String FMT = "deviceid=%s, beginLabel=%s," | ||
12 | + + "endLabel=%s, totalNum=%s, usedNum=%s, currentUsedMaxLabelId=%s," | ||
13 | + + "releaseLabelIds=%s"; | ||
14 | + | ||
15 | + @Override | ||
16 | + protected void execute() { | ||
17 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
18 | + LabelResourcePool pool = lrs.getGlobalLabelResourcePool(); | ||
19 | + if (pool != null) { | ||
20 | + print(FMT, pool.deviceId().toString(), pool.beginLabel(), | ||
21 | + pool.endLabel(), pool.totalNum(), pool.usedNum(), | ||
22 | + pool.currentUsedMaxLabelId(), pool.releaseLabelId() | ||
23 | + .toString()); | ||
24 | + } | ||
25 | + } | ||
26 | + | ||
27 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Argument; | ||
4 | +import org.apache.karaf.shell.commands.Command; | ||
5 | +import org.onosproject.cli.AbstractShellCommand; | ||
6 | +import org.onosproject.net.DeviceId; | ||
7 | +import org.onosproject.net.resource.LabelResourcePool; | ||
8 | +import org.onosproject.net.resource.LabelResourceService; | ||
9 | + | ||
10 | +@Command(scope = "onos", name = "get-label-resource-pool", | ||
11 | + description = "Gets label resource pool information by a specific device id") | ||
12 | +public class GetLabelResourceCommand extends AbstractShellCommand { | ||
13 | + @Argument(index = 0, name = "deviceId", | ||
14 | + description = "Device identity", required = true, multiValued = false) | ||
15 | + String deviceId = null; | ||
16 | + private static final String FMT = "deviceid=%s, beginLabel=%s," | ||
17 | + + "endLabel=%s, totalNum=%s, usedNum=%s, currentUsedMaxLabelId=%s," | ||
18 | + + "releaseLabelIds=%s"; | ||
19 | + | ||
20 | + @Override | ||
21 | + protected void execute() { | ||
22 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
23 | + LabelResourcePool pool = lrs.getDeviceLabelResourcePool(DeviceId | ||
24 | + .deviceId(deviceId)); | ||
25 | + if (pool != null) { | ||
26 | + print(FMT, pool.deviceId().toString(), pool.beginLabel(), | ||
27 | + pool.endLabel(), pool.totalNum(), pool.usedNum(), | ||
28 | + pool.currentUsedMaxLabelId(), pool.releaseLabelId() | ||
29 | + .toString()); | ||
30 | + } else { | ||
31 | + print(FMT, deviceId, null, null, null, null, null, null); | ||
32 | + } | ||
33 | + } | ||
34 | + | ||
35 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import java.util.HashSet; | ||
4 | +import java.util.Set; | ||
5 | + | ||
6 | +import org.apache.karaf.shell.commands.Argument; | ||
7 | +import org.apache.karaf.shell.commands.Command; | ||
8 | +import org.onosproject.cli.AbstractShellCommand; | ||
9 | +import org.onosproject.net.resource.LabelResourceId; | ||
10 | +import org.onosproject.net.resource.LabelResourceService; | ||
11 | + | ||
12 | +@Command(scope = "onos", name = "release-global-label-resource-pool", | ||
13 | +description = "Releases labels to global label resource pool.") | ||
14 | +public class ReleaseGlobalLabelResourceCommand extends AbstractShellCommand { | ||
15 | + @Argument(index = 0, name = "releaseLabelIds", | ||
16 | + description = "Represents for the label ids that are released. They are splited by dot symbol", | ||
17 | + required = true, multiValued = false) | ||
18 | + String releaseLabelIds = null; | ||
19 | + | ||
20 | + @Override | ||
21 | + protected void execute() { | ||
22 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
23 | + Set<LabelResourceId> release = new HashSet<LabelResourceId>(); | ||
24 | + String[] labelIds = releaseLabelIds.split(","); | ||
25 | + LabelResourceId resource = null; | ||
26 | + for (int i = 0; i < labelIds.length; i++) { | ||
27 | + resource = LabelResourceId.labelResourceId(Long.parseLong(labelIds[i])); | ||
28 | + release.add(resource); | ||
29 | + } | ||
30 | + lrs.releaseToGlobalPool(release); | ||
31 | + } | ||
32 | + | ||
33 | +} |
1 | +package org.onosproject.cli.net; | ||
2 | + | ||
3 | +import org.apache.karaf.shell.commands.Argument; | ||
4 | +import org.apache.karaf.shell.commands.Command; | ||
5 | +import org.onosproject.cli.AbstractShellCommand; | ||
6 | +import org.onosproject.net.DeviceId; | ||
7 | +import org.onosproject.net.resource.DefaultLabelResource; | ||
8 | +import org.onosproject.net.resource.LabelResource; | ||
9 | +import org.onosproject.net.resource.LabelResourceId; | ||
10 | +import org.onosproject.net.resource.LabelResourceService; | ||
11 | + | ||
12 | +import com.google.common.collect.ArrayListMultimap; | ||
13 | +import com.google.common.collect.Multimap; | ||
14 | + | ||
15 | +@Command(scope = "onos", name = "release-label-resource-pool", | ||
16 | +description = "Releases label ids to label resource pool by a specific device id") | ||
17 | +public class ReleaseLabelResourceCommand extends AbstractShellCommand { | ||
18 | + @Argument(index = 0, name = "deviceId", | ||
19 | + description = "Device identity", | ||
20 | + required = true, multiValued = false) | ||
21 | + String deviceId = null; | ||
22 | + @Argument(index = 1, name = "releaseLabelIds", | ||
23 | + description = "Represents for the label ids that are released. They are splited by dot symbol", | ||
24 | + required = true, multiValued = false) | ||
25 | + String releaseLabelIds = null; | ||
26 | + | ||
27 | + @Override | ||
28 | + protected void execute() { | ||
29 | + LabelResourceService lrs = get(LabelResourceService.class); | ||
30 | + Multimap<DeviceId, LabelResource> map = ArrayListMultimap | ||
31 | + .create(); | ||
32 | + String[] labelIds = releaseLabelIds.split(","); | ||
33 | + DefaultLabelResource resource = null; | ||
34 | + for (int i = 0; i < labelIds.length; i++) { | ||
35 | + resource = new DefaultLabelResource( | ||
36 | + DeviceId.deviceId(deviceId), | ||
37 | + LabelResourceId.labelResourceId(Long | ||
38 | + .parseLong(labelIds[i]))); | ||
39 | + map.put(DeviceId.deviceId(deviceId), resource); | ||
40 | + } | ||
41 | + lrs.releaseToDevicePool(map); | ||
42 | + } | ||
43 | + | ||
44 | +} |
... | @@ -38,7 +38,6 @@ | ... | @@ -38,7 +38,6 @@ |
38 | <ref component-id="cfgCommandCompleter"/> | 38 | <ref component-id="cfgCommandCompleter"/> |
39 | <ref component-id="componentNameCompleter"/> | 39 | <ref component-id="componentNameCompleter"/> |
40 | <ref component-id="componentPropertyNameCompleter"/> | 40 | <ref component-id="componentPropertyNameCompleter"/> |
41 | - <null/> | ||
42 | </completers> | 41 | </completers> |
43 | </command> | 42 | </command> |
44 | 43 | ||
... | @@ -67,7 +66,6 @@ | ... | @@ -67,7 +66,6 @@ |
67 | <action class="org.onosproject.cli.net.DriversListCommand"/> | 66 | <action class="org.onosproject.cli.net.DriversListCommand"/> |
68 | <completers> | 67 | <completers> |
69 | <ref component-id="driverNameCompleter"/> | 68 | <ref component-id="driverNameCompleter"/> |
70 | - <null/> | ||
71 | </completers> | 69 | </completers> |
72 | </command> | 70 | </command> |
73 | 71 | ||
... | @@ -78,14 +76,12 @@ | ... | @@ -78,14 +76,12 @@ |
78 | <action class="org.onosproject.cli.net.DevicePortsListCommand"/> | 76 | <action class="org.onosproject.cli.net.DevicePortsListCommand"/> |
79 | <completers> | 77 | <completers> |
80 | <ref component-id="deviceIdCompleter"/> | 78 | <ref component-id="deviceIdCompleter"/> |
81 | - <null/> | ||
82 | </completers> | 79 | </completers> |
83 | </command> | 80 | </command> |
84 | <command> | 81 | <command> |
85 | <action class="org.onosproject.cli.net.DeviceRemoveCommand"/> | 82 | <action class="org.onosproject.cli.net.DeviceRemoveCommand"/> |
86 | <completers> | 83 | <completers> |
87 | <ref component-id="deviceIdCompleter"/> | 84 | <ref component-id="deviceIdCompleter"/> |
88 | - <null/> | ||
89 | </completers> | 85 | </completers> |
90 | </command> | 86 | </command> |
91 | <command> | 87 | <command> |
... | @@ -94,14 +90,12 @@ | ... | @@ -94,14 +90,12 @@ |
94 | <ref component-id="deviceIdCompleter"/> | 90 | <ref component-id="deviceIdCompleter"/> |
95 | <ref component-id="nodeIdCompleter"/> | 91 | <ref component-id="nodeIdCompleter"/> |
96 | <ref component-id="roleCompleter"/> | 92 | <ref component-id="roleCompleter"/> |
97 | - <null/> | ||
98 | </completers> | 93 | </completers> |
99 | </command> | 94 | </command> |
100 | <command> | 95 | <command> |
101 | <action class="org.onosproject.cli.net.AnnotateDeviceCommand"/> | 96 | <action class="org.onosproject.cli.net.AnnotateDeviceCommand"/> |
102 | <completers> | 97 | <completers> |
103 | <ref component-id="deviceIdCompleter"/> | 98 | <ref component-id="deviceIdCompleter"/> |
104 | - <null/> | ||
105 | </completers> | 99 | </completers> |
106 | </command> | 100 | </command> |
107 | 101 | ||
... | @@ -109,7 +103,6 @@ | ... | @@ -109,7 +103,6 @@ |
109 | <action class="org.onosproject.cli.net.LinksListCommand"/> | 103 | <action class="org.onosproject.cli.net.LinksListCommand"/> |
110 | <completers> | 104 | <completers> |
111 | <ref component-id="deviceIdCompleter"/> | 105 | <ref component-id="deviceIdCompleter"/> |
112 | - <null/> | ||
113 | </completers> | 106 | </completers> |
114 | </command> | 107 | </command> |
115 | 108 | ||
... | @@ -121,7 +114,6 @@ | ... | @@ -121,7 +114,6 @@ |
121 | <completers> | 114 | <completers> |
122 | <ref component-id="deviceIdCompleter"/> | 115 | <ref component-id="deviceIdCompleter"/> |
123 | <ref component-id="deviceIdCompleter"/> | 116 | <ref component-id="deviceIdCompleter"/> |
124 | - <null/> | ||
125 | </completers> | 117 | </completers> |
126 | </command> | 118 | </command> |
127 | 119 | ||
... | @@ -133,7 +125,6 @@ | ... | @@ -133,7 +125,6 @@ |
133 | <completers> | 125 | <completers> |
134 | <ref component-id="appIdWithIntentNameCompleter"/> | 126 | <ref component-id="appIdWithIntentNameCompleter"/> |
135 | <ref component-id="intentIdCompleter"/> | 127 | <ref component-id="intentIdCompleter"/> |
136 | - <null/> | ||
137 | </completers> | 128 | </completers> |
138 | </command> | 129 | </command> |
139 | <command> | 130 | <command> |
... | @@ -141,7 +132,6 @@ | ... | @@ -141,7 +132,6 @@ |
141 | <completers> | 132 | <completers> |
142 | <ref component-id="hostIdCompleter"/> | 133 | <ref component-id="hostIdCompleter"/> |
143 | <ref component-id="hostIdCompleter"/> | 134 | <ref component-id="hostIdCompleter"/> |
144 | - <null/> | ||
145 | </completers> | 135 | </completers> |
146 | <optional-completers> | 136 | <optional-completers> |
147 | <entry key="-t" value-ref="ethTypeCompleter"/> | 137 | <entry key="-t" value-ref="ethTypeCompleter"/> |
... | @@ -157,7 +147,6 @@ | ... | @@ -157,7 +147,6 @@ |
157 | <completers> | 147 | <completers> |
158 | <ref component-id="connectPointCompleter"/> | 148 | <ref component-id="connectPointCompleter"/> |
159 | <ref component-id="connectPointCompleter"/> | 149 | <ref component-id="connectPointCompleter"/> |
160 | - <null/> | ||
161 | </completers> | 150 | </completers> |
162 | <optional-completers> | 151 | <optional-completers> |
163 | <entry key="-t" value-ref="ethTypeCompleter"/> | 152 | <entry key="-t" value-ref="ethTypeCompleter"/> |
... | @@ -173,7 +162,6 @@ | ... | @@ -173,7 +162,6 @@ |
173 | <completers> | 162 | <completers> |
174 | <ref component-id="connectPointCompleter"/> | 163 | <ref component-id="connectPointCompleter"/> |
175 | <ref component-id="connectPointCompleter"/> | 164 | <ref component-id="connectPointCompleter"/> |
176 | - <null/> | ||
177 | </completers> | 165 | </completers> |
178 | <optional-completers> | 166 | <optional-completers> |
179 | <entry key="-a" value-ref="allAppNameCompleter"/> | 167 | <entry key="-a" value-ref="allAppNameCompleter"/> |
... | @@ -219,7 +207,6 @@ | ... | @@ -219,7 +207,6 @@ |
219 | <ref component-id="connectPointCompleter"/> | 207 | <ref component-id="connectPointCompleter"/> |
220 | <ref component-id="connectPointCompleter"/> | 208 | <ref component-id="connectPointCompleter"/> |
221 | <ref component-id="nullCompleter"/> | 209 | <ref component-id="nullCompleter"/> |
222 | - <null/> | ||
223 | </completers> | 210 | </completers> |
224 | </command> | 211 | </command> |
225 | <command> | 212 | <command> |
... | @@ -228,14 +215,12 @@ | ... | @@ -228,14 +215,12 @@ |
228 | <ref component-id="connectPointCompleter"/> | 215 | <ref component-id="connectPointCompleter"/> |
229 | <ref component-id="connectPointCompleter"/> | 216 | <ref component-id="connectPointCompleter"/> |
230 | <ref component-id="nullCompleter"/> | 217 | <ref component-id="nullCompleter"/> |
231 | - <null/> | ||
232 | </completers> | 218 | </completers> |
233 | </command> | 219 | </command> |
234 | <command> | 220 | <command> |
235 | <action class="org.onosproject.cli.net.RandomIntentCommand"/> | 221 | <action class="org.onosproject.cli.net.RandomIntentCommand"/> |
236 | <completers> | 222 | <completers> |
237 | <ref component-id="nullCompleter"/> | 223 | <ref component-id="nullCompleter"/> |
238 | - <null/> | ||
239 | </completers> | 224 | </completers> |
240 | </command> | 225 | </command> |
241 | <command> | 226 | <command> |
... | @@ -243,7 +228,6 @@ | ... | @@ -243,7 +228,6 @@ |
243 | <completers> | 228 | <completers> |
244 | <ref component-id="connectPointCompleter"/> | 229 | <ref component-id="connectPointCompleter"/> |
245 | <ref component-id="connectPointCompleter"/> | 230 | <ref component-id="connectPointCompleter"/> |
246 | - <null/> | ||
247 | </completers> | 231 | </completers> |
248 | </command> | 232 | </command> |
249 | <command> | 233 | <command> |
... | @@ -251,7 +235,6 @@ | ... | @@ -251,7 +235,6 @@ |
251 | <completers> | 235 | <completers> |
252 | <ref component-id="connectPointCompleter"/> | 236 | <ref component-id="connectPointCompleter"/> |
253 | <ref component-id="connectPointCompleter"/> | 237 | <ref component-id="connectPointCompleter"/> |
254 | - <null/> | ||
255 | </completers> | 238 | </completers> |
256 | </command> | 239 | </command> |
257 | <command> | 240 | <command> |
... | @@ -281,14 +264,12 @@ | ... | @@ -281,14 +264,12 @@ |
281 | <action class="org.onosproject.cli.net.ClusterDevicesCommand"/> | 264 | <action class="org.onosproject.cli.net.ClusterDevicesCommand"/> |
282 | <completers> | 265 | <completers> |
283 | <ref component-id="clusterIdCompleter"/> | 266 | <ref component-id="clusterIdCompleter"/> |
284 | - <null/> | ||
285 | </completers> | 267 | </completers> |
286 | </command> | 268 | </command> |
287 | <command> | 269 | <command> |
288 | <action class="org.onosproject.cli.net.ClusterLinksCommand"/> | 270 | <action class="org.onosproject.cli.net.ClusterLinksCommand"/> |
289 | <completers> | 271 | <completers> |
290 | <ref component-id="clusterIdCompleter"/> | 272 | <ref component-id="clusterIdCompleter"/> |
291 | - <null/> | ||
292 | </completers> | 273 | </completers> |
293 | </command> | 274 | </command> |
294 | 275 | ||
... | @@ -299,7 +280,6 @@ | ... | @@ -299,7 +280,6 @@ |
299 | <action class="org.onosproject.cli.net.HostRemoveCommand"/> | 280 | <action class="org.onosproject.cli.net.HostRemoveCommand"/> |
300 | <completers> | 281 | <completers> |
301 | <ref component-id="hostIdCompleter"/> | 282 | <ref component-id="hostIdCompleter"/> |
302 | - <null/> | ||
303 | </completers> | 283 | </completers> |
304 | </command> | 284 | </command> |
305 | <command> | 285 | <command> |
... | @@ -319,7 +299,6 @@ | ... | @@ -319,7 +299,6 @@ |
319 | <completers> | 299 | <completers> |
320 | <ref component-id="flowRuleStatusCompleter"/> | 300 | <ref component-id="flowRuleStatusCompleter"/> |
321 | <ref component-id="deviceIdCompleter"/> | 301 | <ref component-id="deviceIdCompleter"/> |
322 | - <null/> | ||
323 | </completers> | 302 | </completers> |
324 | </command> | 303 | </command> |
325 | 304 | ||
... | @@ -338,7 +317,6 @@ | ... | @@ -338,7 +317,6 @@ |
338 | <completers> | 317 | <completers> |
339 | <ref component-id="connectPointCompleter"/> | 318 | <ref component-id="connectPointCompleter"/> |
340 | <ref component-id="connectPointCompleter"/> | 319 | <ref component-id="connectPointCompleter"/> |
341 | - <null/> | ||
342 | </completers> | 320 | </completers> |
343 | <optional-completers> | 321 | <optional-completers> |
344 | <entry key="-t" value-ref="ethTypeCompleter"/> | 322 | <entry key="-t" value-ref="ethTypeCompleter"/> |
... | @@ -349,6 +327,37 @@ | ... | @@ -349,6 +327,37 @@ |
349 | <entry key="-a" value-ref="allAppNameCompleter"/> | 327 | <entry key="-a" value-ref="allAppNameCompleter"/> |
350 | </optional-completers> | 328 | </optional-completers> |
351 | </command> | 329 | </command> |
330 | + | ||
331 | + <command> | ||
332 | + <action class="org.onosproject.cli.net.GetGlobalLabelResourceCommand"/> | ||
333 | + </command> | ||
334 | + <command> | ||
335 | + <action class="org.onosproject.cli.net.GetLabelResourceCommand"/> | ||
336 | + </command> | ||
337 | + <command> | ||
338 | + <action class="org.onosproject.cli.net.CreateGlobalLabelResourcePoolCommand"/> | ||
339 | + </command> | ||
340 | + <command> | ||
341 | + <action class="org.onosproject.cli.net.CreateLabelResourcePoolCommand"/> | ||
342 | + </command> | ||
343 | + <command> | ||
344 | + <action class="org.onosproject.cli.net.DestroyGlobalLabelResourcePoolCommand"/> | ||
345 | + </command> | ||
346 | + <command> | ||
347 | + <action class="org.onosproject.cli.net.DestroyGlobalLabelResourcePoolCommand"/> | ||
348 | + </command> | ||
349 | + <command> | ||
350 | + <action class="org.onosproject.cli.net.ReleaseGlobalLabelResourceCommand"/> | ||
351 | + </command> | ||
352 | + <command> | ||
353 | + <action class="org.onosproject.cli.net.ReleaseLabelResourceCommand"/> | ||
354 | + </command> | ||
355 | + <command> | ||
356 | + <action class="org.onosproject.cli.net.ApplyGlobalLabelResourceCommand"/> | ||
357 | + </command> | ||
358 | + <command> | ||
359 | + <action class="org.onosproject.cli.net.ApplyLabelResourceCommand"/> | ||
360 | + </command> | ||
352 | </command-bundle> | 361 | </command-bundle> |
353 | 362 | ||
354 | <bean id="appCommandCompleter" class="org.onosproject.cli.app.ApplicationCommandCompleter"/> | 363 | <bean id="appCommandCompleter" class="org.onosproject.cli.app.ApplicationCommandCompleter"/> | ... | ... |
1 | +package org.onosproject.net.resource; | ||
2 | + | ||
3 | +import org.junit.Test; | ||
4 | +import org.onosproject.event.AbstractEventTest; | ||
5 | + | ||
6 | +import com.google.common.testing.EqualsTester; | ||
7 | + | ||
8 | +/** | ||
9 | + * Tests of default label resource. | ||
10 | + */ | ||
11 | +public class DefaultLabelResourceTest extends AbstractEventTest { | ||
12 | + | ||
13 | + @Test | ||
14 | + public void testEquality() { | ||
15 | + String deviceId1 = "of:001"; | ||
16 | + String deviceId2 = "of:002"; | ||
17 | + long labelResourceId1 = 100; | ||
18 | + long labelResourceId2 = 200; | ||
19 | + DefaultLabelResource h1 = new DefaultLabelResource(deviceId1, | ||
20 | + labelResourceId1); | ||
21 | + DefaultLabelResource h2 = new DefaultLabelResource(deviceId1, | ||
22 | + labelResourceId1); | ||
23 | + DefaultLabelResource h3 = new DefaultLabelResource(deviceId2, | ||
24 | + labelResourceId2); | ||
25 | + DefaultLabelResource h4 = new DefaultLabelResource(deviceId2, | ||
26 | + labelResourceId2); | ||
27 | + | ||
28 | + new EqualsTester().addEqualityGroup(h1, h2).addEqualityGroup(h3, h4) | ||
29 | + .testEquals(); | ||
30 | + } | ||
31 | +} |
1 | +package org.onosproject.net.resource; | ||
2 | + | ||
3 | +import org.junit.Test; | ||
4 | +import org.onosproject.event.AbstractEventTest; | ||
5 | + | ||
6 | +import com.google.common.testing.EqualsTester; | ||
7 | + | ||
8 | +/** | ||
9 | + * Tests of the label resource pool. | ||
10 | + */ | ||
11 | +public class LabelResourcePoolTest extends AbstractEventTest { | ||
12 | + | ||
13 | + @Test | ||
14 | + public void testEquality() { | ||
15 | + LabelResourcePool h1 = new LabelResourcePool("of:001", 0, 100); | ||
16 | + LabelResourcePool h2 = new LabelResourcePool("of:001", 0, 100); | ||
17 | + LabelResourcePool h3 = new LabelResourcePool("of:002", 0, 100); | ||
18 | + LabelResourcePool h4 = new LabelResourcePool("of:002", 0, 100); | ||
19 | + new EqualsTester().addEqualityGroup(h1, h2).addEqualityGroup(h3, h4) | ||
20 | + .testEquals(); | ||
21 | + } | ||
22 | + | ||
23 | +} |
1 | +package org.onosproject.net.resource; | ||
2 | + | ||
3 | +import java.util.Collections; | ||
4 | + | ||
5 | +import org.junit.Test; | ||
6 | +import org.onosproject.event.AbstractEventTest; | ||
7 | +import org.onosproject.net.DeviceId; | ||
8 | + | ||
9 | +import com.google.common.collect.ImmutableSet; | ||
10 | +import com.google.common.testing.EqualsTester; | ||
11 | + | ||
12 | +/** | ||
13 | + * Tests of the label resource request. | ||
14 | + */ | ||
15 | +public class LabelResourceRequestTest extends AbstractEventTest { | ||
16 | + | ||
17 | + @Test | ||
18 | + public void testEquality() { | ||
19 | + DeviceId deviceId1 = DeviceId.deviceId("of:0001"); | ||
20 | + DeviceId deviceId2 = DeviceId.deviceId("of:0002"); | ||
21 | + long apply = 2; | ||
22 | + ImmutableSet<LabelResource> releaseCollection = ImmutableSet | ||
23 | + .copyOf(Collections.emptySet()); | ||
24 | + LabelResourceRequest h1 = new LabelResourceRequest( | ||
25 | + deviceId1, | ||
26 | + LabelResourceRequest.Type.APPLY, | ||
27 | + apply, null); | ||
28 | + LabelResourceRequest h2 = new LabelResourceRequest( | ||
29 | + deviceId1, | ||
30 | + LabelResourceRequest.Type.APPLY, | ||
31 | + apply, null); | ||
32 | + LabelResourceRequest h3 = new LabelResourceRequest( | ||
33 | + deviceId2, | ||
34 | + LabelResourceRequest.Type.RELEASE, | ||
35 | + 0, releaseCollection); | ||
36 | + LabelResourceRequest h4 = new LabelResourceRequest( | ||
37 | + deviceId2, | ||
38 | + LabelResourceRequest.Type.RELEASE, | ||
39 | + 0, releaseCollection); | ||
40 | + | ||
41 | + new EqualsTester().addEqualityGroup(h1, h2).addEqualityGroup(h3, h4) | ||
42 | + .testEquals(); | ||
43 | + } | ||
44 | +} |
1 | +package org.onosproject.net.resource.impl; | ||
2 | + | ||
3 | +import static org.slf4j.LoggerFactory.getLogger; | ||
4 | + | ||
5 | +import java.util.Collection; | ||
6 | +import java.util.Set; | ||
7 | + | ||
8 | +import org.apache.felix.scr.annotations.Activate; | ||
9 | +import org.apache.felix.scr.annotations.Component; | ||
10 | +import org.apache.felix.scr.annotations.Deactivate; | ||
11 | +import org.apache.felix.scr.annotations.Reference; | ||
12 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
13 | +import org.apache.felix.scr.annotations.Service; | ||
14 | +import org.onosproject.event.AbstractListenerRegistry; | ||
15 | +import org.onosproject.event.EventDeliveryService; | ||
16 | +import org.onosproject.net.Device; | ||
17 | +import org.onosproject.net.DeviceId; | ||
18 | +import org.onosproject.net.device.DeviceEvent; | ||
19 | +import org.onosproject.net.device.DeviceEvent.Type; | ||
20 | +import org.onosproject.net.device.DeviceListener; | ||
21 | +import org.onosproject.net.device.DeviceService; | ||
22 | +import org.onosproject.net.provider.AbstractProviderRegistry; | ||
23 | +import org.onosproject.net.provider.AbstractProviderService; | ||
24 | +import org.onosproject.net.resource.LabelResource; | ||
25 | +import org.onosproject.net.resource.LabelResourceAdminService; | ||
26 | +import org.onosproject.net.resource.LabelResourceDelegate; | ||
27 | +import org.onosproject.net.resource.LabelResourceEvent; | ||
28 | +import org.onosproject.net.resource.LabelResourceId; | ||
29 | +import org.onosproject.net.resource.LabelResourceListener; | ||
30 | +import org.onosproject.net.resource.LabelResourcePool; | ||
31 | +import org.onosproject.net.resource.LabelResourceProvider; | ||
32 | +import org.onosproject.net.resource.LabelResourceProviderRegistry; | ||
33 | +import org.onosproject.net.resource.LabelResourceProviderService; | ||
34 | +import org.onosproject.net.resource.LabelResourceService; | ||
35 | +import org.onosproject.net.resource.LabelResourceStore; | ||
36 | +import org.slf4j.Logger; | ||
37 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
38 | +import static com.google.common.base.Preconditions.checkArgument; | ||
39 | + | ||
40 | +import com.google.common.collect.Multimap; | ||
41 | + | ||
42 | +/** | ||
43 | + * provides implementation of the label resource NB & SB APIs. | ||
44 | + * | ||
45 | + */ | ||
46 | +@Component(immediate = true) | ||
47 | +@Service | ||
48 | +public class LabelResourceManager | ||
49 | + extends | ||
50 | + AbstractProviderRegistry<LabelResourceProvider, LabelResourceProviderService> | ||
51 | + implements LabelResourceService, LabelResourceAdminService, | ||
52 | + LabelResourceProviderRegistry { | ||
53 | + private final Logger log = getLogger(getClass()); | ||
54 | + private final LabelResourceDelegate delegate = new InternalLabelResourceDelegate(); | ||
55 | + | ||
56 | + private final AbstractListenerRegistry<LabelResourceEvent, LabelResourceListener> listenerRegistry | ||
57 | + = new AbstractListenerRegistry<>(); | ||
58 | + | ||
59 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
60 | + protected LabelResourceStore store; | ||
61 | + | ||
62 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
63 | + protected EventDeliveryService eventDispatcher; | ||
64 | + | ||
65 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
66 | + protected DeviceService deviceService; | ||
67 | + | ||
68 | + private DeviceListener deviceListener = new InternalDeviceListener(); | ||
69 | + | ||
70 | + @Activate | ||
71 | + public void activate() { | ||
72 | + store.setDelegate(delegate); | ||
73 | + eventDispatcher.addSink(LabelResourceEvent.class, listenerRegistry); | ||
74 | + deviceService.addListener(deviceListener); | ||
75 | + log.info("Started"); | ||
76 | + | ||
77 | + } | ||
78 | + | ||
79 | + @Deactivate | ||
80 | + public void deactivate() { | ||
81 | + deviceService.removeListener(deviceListener); | ||
82 | + store.unsetDelegate(delegate); | ||
83 | + eventDispatcher.removeSink(LabelResourceEvent.class); | ||
84 | + log.info("Stopped"); | ||
85 | + } | ||
86 | + | ||
87 | + @Override | ||
88 | + public boolean createDevicePool(DeviceId deviceId, | ||
89 | + LabelResourceId beginLabel, | ||
90 | + LabelResourceId endLabel) { | ||
91 | + checkNotNull(deviceId, "deviceId is not null"); | ||
92 | + checkNotNull(beginLabel, "beginLabel is not null"); | ||
93 | + checkNotNull(endLabel, "beginLabel is not null"); | ||
94 | + checkArgument(beginLabel.labelId() < 0 || endLabel.labelId() < 0, | ||
95 | + "The value of beginLabel and the value of endLabel must be both positive number."); | ||
96 | + checkArgument(beginLabel.labelId() > endLabel.labelId(), | ||
97 | + "The value of endLabel must be greater than the value of endLabel."); | ||
98 | + return store.createDevicePool(deviceId, beginLabel, endLabel); | ||
99 | + } | ||
100 | + | ||
101 | + @Override | ||
102 | + public boolean createGlobalPool(LabelResourceId beginLabel, | ||
103 | + LabelResourceId endLabel) { | ||
104 | + checkNotNull(beginLabel, "beginLabel is not null"); | ||
105 | + checkNotNull(endLabel, "beginLabel is not null"); | ||
106 | + checkArgument(beginLabel.labelId() < 0 || endLabel.labelId() < 0, | ||
107 | + "The value of beginLabel and the value of endLabel must be both positive number."); | ||
108 | + checkArgument(beginLabel.labelId() > endLabel.labelId(), | ||
109 | + "The value of endLabel must be greater than the value of endLabel."); | ||
110 | + return store.createGlobalPool(beginLabel, endLabel); | ||
111 | + } | ||
112 | + | ||
113 | + @Override | ||
114 | + public boolean destroyDevicePool(DeviceId deviceId) { | ||
115 | + checkNotNull(deviceId, "deviceId is not null"); | ||
116 | + return store.destroyDevicePool(deviceId); | ||
117 | + } | ||
118 | + | ||
119 | + @Override | ||
120 | + public boolean destroyGlobalPool() { | ||
121 | + return store.destroyGlobalPool(); | ||
122 | + } | ||
123 | + | ||
124 | + @Override | ||
125 | + public Collection<LabelResource> applyFromDevicePool(DeviceId deviceId, | ||
126 | + long applyNum) { | ||
127 | + checkNotNull(deviceId, "deviceId is not null"); | ||
128 | + checkNotNull(applyNum, "applyNum is not null"); | ||
129 | + return store.applyFromDevicePool(deviceId, applyNum); | ||
130 | + } | ||
131 | + | ||
132 | + @Override | ||
133 | + public Collection<LabelResource> applyFromGlobalPool(long applyNum) { | ||
134 | + checkNotNull(applyNum, "applyNum is not null"); | ||
135 | + return store.applyFromGlobalPool(applyNum); | ||
136 | + } | ||
137 | + | ||
138 | + @Override | ||
139 | + public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) { | ||
140 | + checkNotNull(release, "release is not null"); | ||
141 | + return store.releaseToDevicePool(release); | ||
142 | + } | ||
143 | + | ||
144 | + @Override | ||
145 | + public boolean releaseToGlobalPool(Set<LabelResourceId> release) { | ||
146 | + checkNotNull(release, "release is not null"); | ||
147 | + return store.releaseToGlobalPool(release); | ||
148 | + } | ||
149 | + | ||
150 | + @Override | ||
151 | + public boolean isDevicePoolFull(DeviceId deviceId) { | ||
152 | + checkNotNull(deviceId, "deviceId is not null"); | ||
153 | + return store.isDevicePoolFull(deviceId); | ||
154 | + } | ||
155 | + | ||
156 | + @Override | ||
157 | + public boolean isGlobalPoolFull() { | ||
158 | + return store.isGlobalPoolFull(); | ||
159 | + } | ||
160 | + | ||
161 | + @Override | ||
162 | + public long getFreeNumOfDevicePool(DeviceId deviceId) { | ||
163 | + checkNotNull(deviceId, "deviceId is not null"); | ||
164 | + return store.getFreeNumOfDevicePool(deviceId); | ||
165 | + } | ||
166 | + | ||
167 | + @Override | ||
168 | + public long getFreeNumOfGlobalPool() { | ||
169 | + return store.getFreeNumOfGlobalPool(); | ||
170 | + } | ||
171 | + | ||
172 | + @Override | ||
173 | + public LabelResourcePool getDeviceLabelResourcePool(DeviceId deviceId) { | ||
174 | + checkNotNull(deviceId, "deviceId is not null"); | ||
175 | + return store.getDeviceLabelResourcePool(deviceId); | ||
176 | + } | ||
177 | + | ||
178 | + @Override | ||
179 | + public LabelResourcePool getGlobalLabelResourcePool() { | ||
180 | + return store.getGlobalLabelResourcePool(); | ||
181 | + } | ||
182 | + | ||
183 | + @Override | ||
184 | + public void addListener(LabelResourceListener listener) { | ||
185 | + listenerRegistry.addListener(listener); | ||
186 | + } | ||
187 | + | ||
188 | + @Override | ||
189 | + public void removeListener(LabelResourceListener listener) { | ||
190 | + listenerRegistry.removeListener(listener); | ||
191 | + | ||
192 | + } | ||
193 | + | ||
194 | + private void post(LabelResourceEvent event) { | ||
195 | + if (event != null) { | ||
196 | + eventDispatcher.post(event); | ||
197 | + } | ||
198 | + } | ||
199 | + | ||
200 | + private class InternalLabelResourceDelegate | ||
201 | + implements LabelResourceDelegate { | ||
202 | + | ||
203 | + @Override | ||
204 | + public void notify(LabelResourceEvent event) { | ||
205 | + post(event); | ||
206 | + } | ||
207 | + | ||
208 | + } | ||
209 | + | ||
210 | + private class InternalDeviceListener implements DeviceListener { | ||
211 | + | ||
212 | + @Override | ||
213 | + public void event(DeviceEvent event) { | ||
214 | + Device device = event.subject(); | ||
215 | + if (Type.DEVICE_REMOVED.equals(event.type())) { | ||
216 | + destroyDevicePool(device.id()); | ||
217 | + } | ||
218 | + } | ||
219 | + } | ||
220 | + | ||
221 | + private class InternalLabelResourceProviderService | ||
222 | + extends AbstractProviderService<LabelResourceProvider> | ||
223 | + implements LabelResourceProviderService { | ||
224 | + | ||
225 | + protected InternalLabelResourceProviderService(LabelResourceProvider provider) { | ||
226 | + super(provider); | ||
227 | + } | ||
228 | + | ||
229 | + @Override | ||
230 | + public void deviceLabelResourcePoolDetected(DeviceId deviceId, | ||
231 | + LabelResourceId beginLabel, | ||
232 | + LabelResourceId endLabel) { | ||
233 | + checkNotNull(deviceId, "deviceId is not null"); | ||
234 | + checkNotNull(beginLabel, "beginLabel is not null"); | ||
235 | + checkNotNull(endLabel, "endLabel is not null"); | ||
236 | + createDevicePool(deviceId, beginLabel, endLabel); | ||
237 | + } | ||
238 | + | ||
239 | + @Override | ||
240 | + public void deviceLabelResourcePoolDestroyed(DeviceId deviceId) { | ||
241 | + checkNotNull(deviceId, "deviceId is not null"); | ||
242 | + destroyDevicePool(deviceId); | ||
243 | + } | ||
244 | + | ||
245 | + } | ||
246 | + | ||
247 | + @Override | ||
248 | + protected LabelResourceProviderService createProviderService(LabelResourceProvider provider) { | ||
249 | + return new InternalLabelResourceProviderService(provider); | ||
250 | + } | ||
251 | +} |
core/store/dist/src/main/java/org/onosproject/store/resource/impl/DistributedLabelResourceStore.java
0 → 100644
1 | +package org.onosproject.store.resource.impl; | ||
2 | + | ||
3 | +import static org.onlab.util.Tools.groupedThreads; | ||
4 | +import static org.slf4j.LoggerFactory.getLogger; | ||
5 | + | ||
6 | +import java.util.Collection; | ||
7 | +import java.util.Collections; | ||
8 | +import java.util.HashSet; | ||
9 | +import java.util.Iterator; | ||
10 | +import java.util.Map; | ||
11 | +import java.util.Set; | ||
12 | +import java.util.concurrent.ExecutionException; | ||
13 | +import java.util.concurrent.ExecutorService; | ||
14 | +import java.util.concurrent.Executors; | ||
15 | +import java.util.concurrent.Future; | ||
16 | +import java.util.concurrent.TimeUnit; | ||
17 | +import java.util.concurrent.TimeoutException; | ||
18 | +import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
19 | + | ||
20 | +import org.apache.felix.scr.annotations.Activate; | ||
21 | +import org.apache.felix.scr.annotations.Component; | ||
22 | +import org.apache.felix.scr.annotations.Deactivate; | ||
23 | +import org.apache.felix.scr.annotations.Reference; | ||
24 | +import org.apache.felix.scr.annotations.ReferenceCardinality; | ||
25 | +import org.apache.felix.scr.annotations.Service; | ||
26 | +import org.onlab.util.KryoNamespace; | ||
27 | +import org.onosproject.cluster.ClusterService; | ||
28 | +import org.onosproject.net.Device; | ||
29 | +import org.onosproject.net.DeviceId; | ||
30 | +import org.onosproject.net.device.DeviceService; | ||
31 | +import org.onosproject.net.resource.DefaultLabelResource; | ||
32 | +import org.onosproject.net.resource.LabelResource; | ||
33 | +import org.onosproject.net.resource.LabelResourceDelegate; | ||
34 | +import org.onosproject.net.resource.LabelResourceEvent; | ||
35 | +import org.onosproject.net.resource.LabelResourceEvent.Type; | ||
36 | +import org.onosproject.net.resource.LabelResourceId; | ||
37 | +import org.onosproject.net.resource.LabelResourcePool; | ||
38 | +import org.onosproject.net.resource.LabelResourceRequest; | ||
39 | +import org.onosproject.net.resource.LabelResourceStore; | ||
40 | +import org.onosproject.store.AbstractStore; | ||
41 | +import org.onosproject.store.cluster.messaging.ClusterCommunicationService; | ||
42 | +import org.onosproject.store.cluster.messaging.ClusterMessage; | ||
43 | +import org.onosproject.store.cluster.messaging.ClusterMessageHandler; | ||
44 | +import org.onosproject.store.flow.ReplicaInfo; | ||
45 | +import org.onosproject.store.flow.ReplicaInfoService; | ||
46 | +import org.onosproject.store.serializers.KryoNamespaces; | ||
47 | +import org.onosproject.store.serializers.KryoSerializer; | ||
48 | +import org.onosproject.store.serializers.impl.DistributedStoreSerializers; | ||
49 | +import org.onosproject.store.service.ConsistentMap; | ||
50 | +import org.onosproject.store.service.Serializer; | ||
51 | +import org.onosproject.store.service.StorageService; | ||
52 | +import org.slf4j.Logger; | ||
53 | + | ||
54 | +import com.google.common.collect.ImmutableSet; | ||
55 | +import com.google.common.collect.Multimap; | ||
56 | + | ||
57 | +/** | ||
58 | + * Manages label resources using copycat. | ||
59 | + */ | ||
60 | +@Component(immediate = true, enabled = true) | ||
61 | +@Service | ||
62 | +public class DistributedLabelResourceStore | ||
63 | + extends AbstractStore<LabelResourceEvent, LabelResourceDelegate> | ||
64 | + implements LabelResourceStore { | ||
65 | + private final Logger log = getLogger(getClass()); | ||
66 | + | ||
67 | + private static final String POOL_MAP_NAME = "labelresourcepool"; | ||
68 | + | ||
69 | + private static final String GLOBAL_RESOURCE_POOL_DEVICE_ID = "global_resource_pool_device_id"; | ||
70 | + // primary data: | ||
71 | + // read/write needs to be locked | ||
72 | + private final ReentrantReadWriteLock resourcePoolLock = new ReentrantReadWriteLock(); | ||
73 | + | ||
74 | + private ConsistentMap<DeviceId, LabelResourcePool> resourcePool = null; | ||
75 | + | ||
76 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
77 | + protected StorageService storageService; | ||
78 | + | ||
79 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
80 | + protected ReplicaInfoService replicaInfoManager; | ||
81 | + | ||
82 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
83 | + protected ClusterCommunicationService clusterCommunicator; | ||
84 | + | ||
85 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
86 | + protected ClusterService clusterService; | ||
87 | + | ||
88 | + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
89 | + protected DeviceService deviceService; | ||
90 | + | ||
91 | + private ExecutorService messageHandlingExecutor; | ||
92 | + private static final int MESSAGE_HANDLER_THREAD_POOL_SIZE = 8; | ||
93 | + private static final long PEER_REQUEST_TIMEOUT_MS = 5000; | ||
94 | + | ||
95 | + protected static final KryoSerializer SERIALIZER = new KryoSerializer() { | ||
96 | + @Override | ||
97 | + protected void setupKryoPool() { | ||
98 | + serializerPool = KryoNamespace.newBuilder() | ||
99 | + .register(DistributedStoreSerializers.STORE_COMMON) | ||
100 | + .nextId(DistributedStoreSerializers.STORE_CUSTOM_BEGIN) | ||
101 | + .register(LabelResourceEvent.class) | ||
102 | + .register(LabelResourcePool.class).register(DeviceId.class) | ||
103 | + .register(LabelResourceRequest.class) | ||
104 | + .register(LabelResourceRequest.Type.class) | ||
105 | + .register(LabelResourceEvent.Type.class) | ||
106 | + .register(DefaultLabelResource.class) | ||
107 | + .register(LabelResourceId.class).build(); | ||
108 | + } | ||
109 | + }; | ||
110 | + | ||
111 | + @Activate | ||
112 | + public void activate() { | ||
113 | + | ||
114 | + resourcePool = storageService | ||
115 | + .<DeviceId, LabelResourcePool>consistentMapBuilder() | ||
116 | + .withName(POOL_MAP_NAME).withSerializer(new Serializer() { | ||
117 | + KryoNamespace kryo = new KryoNamespace.Builder() | ||
118 | + .register(KryoNamespaces.API).build(); | ||
119 | + | ||
120 | + @Override | ||
121 | + public <T> byte[] encode(T object) { | ||
122 | + return kryo.serialize(object); | ||
123 | + } | ||
124 | + | ||
125 | + @Override | ||
126 | + public <T> T decode(byte[] bytes) { | ||
127 | + return kryo.deserialize(bytes); | ||
128 | + } | ||
129 | + }).withPartitionsDisabled().build(); | ||
130 | + messageHandlingExecutor = Executors | ||
131 | + .newFixedThreadPool(MESSAGE_HANDLER_THREAD_POOL_SIZE, | ||
132 | + groupedThreads("onos/store/flow", | ||
133 | + "message-handlers")); | ||
134 | + clusterCommunicator | ||
135 | + .addSubscriber(LabelResourceMessageSubjects.LABEL_POOL_CREATED, | ||
136 | + new ClusterMessageHandler() { | ||
137 | + | ||
138 | + @Override | ||
139 | + public void handle(ClusterMessage message) { | ||
140 | + LabelResourcePool operation = SERIALIZER | ||
141 | + .decode(message.payload()); | ||
142 | + log.trace("received get flow entry request for {}", | ||
143 | + operation); | ||
144 | + boolean b = internalCreate(operation); | ||
145 | + message.respond(SERIALIZER.encode(b)); | ||
146 | + } | ||
147 | + }, messageHandlingExecutor); | ||
148 | + clusterCommunicator | ||
149 | + .addSubscriber(LabelResourceMessageSubjects.LABEL_POOL_DESTROYED, | ||
150 | + new ClusterMessageHandler() { | ||
151 | + | ||
152 | + @Override | ||
153 | + public void handle(ClusterMessage message) { | ||
154 | + DeviceId deviceId = SERIALIZER | ||
155 | + .decode(message.payload()); | ||
156 | + log.trace("received get flow entry request for {}", | ||
157 | + deviceId); | ||
158 | + boolean b = internalDestroy(deviceId); | ||
159 | + message.respond(SERIALIZER.encode(b)); | ||
160 | + } | ||
161 | + }, messageHandlingExecutor); | ||
162 | + clusterCommunicator | ||
163 | + .addSubscriber(LabelResourceMessageSubjects.LABEL_POOL_APPLY, | ||
164 | + new ClusterMessageHandler() { | ||
165 | + | ||
166 | + @Override | ||
167 | + public void handle(ClusterMessage message) { | ||
168 | + LabelResourceRequest request = SERIALIZER | ||
169 | + .decode(message.payload()); | ||
170 | + log.trace("received get flow entry request for {}", | ||
171 | + request); | ||
172 | + final Collection<LabelResource> resource = internalApply(request); | ||
173 | + message.respond(SERIALIZER | ||
174 | + .encode(resource)); | ||
175 | + } | ||
176 | + }, messageHandlingExecutor); | ||
177 | + clusterCommunicator | ||
178 | + .addSubscriber(LabelResourceMessageSubjects.LABEL_POOL_RELEASE, | ||
179 | + new ClusterMessageHandler() { | ||
180 | + | ||
181 | + @Override | ||
182 | + public void handle(ClusterMessage message) { | ||
183 | + LabelResourceRequest request = SERIALIZER | ||
184 | + .decode(message.payload()); | ||
185 | + log.trace("received get flow entry request for {}", | ||
186 | + request); | ||
187 | + final boolean isSuccess = internalRelease(request); | ||
188 | + message.respond(SERIALIZER | ||
189 | + .encode(isSuccess)); | ||
190 | + } | ||
191 | + }, messageHandlingExecutor); | ||
192 | + log.info("Started"); | ||
193 | + } | ||
194 | + | ||
195 | + @Deactivate | ||
196 | + public void deactivate() { | ||
197 | + clusterCommunicator | ||
198 | + .removeSubscriber(LabelResourceMessageSubjects.LABEL_POOL_CREATED); | ||
199 | + clusterCommunicator | ||
200 | + .removeSubscriber(LabelResourceMessageSubjects.LABEL_POOL_APPLY); | ||
201 | + clusterCommunicator | ||
202 | + .removeSubscriber(LabelResourceMessageSubjects.LABEL_POOL_DESTROYED); | ||
203 | + clusterCommunicator | ||
204 | + .removeSubscriber(LabelResourceMessageSubjects.LABEL_POOL_RELEASE); | ||
205 | + messageHandlingExecutor.shutdown(); | ||
206 | + log.info("Stopped"); | ||
207 | + } | ||
208 | + | ||
209 | + @Override | ||
210 | + public boolean createDevicePool(DeviceId deviceId, | ||
211 | + LabelResourceId beginLabel, | ||
212 | + LabelResourceId endLabel) { | ||
213 | + LabelResourcePool pool = new LabelResourcePool(deviceId.toString(), | ||
214 | + beginLabel.labelId(), | ||
215 | + endLabel.labelId()); | ||
216 | + return this.create(pool); | ||
217 | + } | ||
218 | + | ||
219 | + @Override | ||
220 | + public boolean createGlobalPool(LabelResourceId beginLabel, | ||
221 | + LabelResourceId endLabel) { | ||
222 | + LabelResourcePool pool = new LabelResourcePool( | ||
223 | + GLOBAL_RESOURCE_POOL_DEVICE_ID, | ||
224 | + beginLabel.labelId(), | ||
225 | + endLabel.labelId()); | ||
226 | + return this.internalCreate(pool); | ||
227 | + } | ||
228 | + | ||
229 | + private boolean create(LabelResourcePool pool) { | ||
230 | + Device device = (Device) deviceService.getDevice(pool.deviceId()); | ||
231 | + if (device == null) { | ||
232 | + return false; | ||
233 | + } | ||
234 | + | ||
235 | + ReplicaInfo replicaInfo = replicaInfoManager.getReplicaInfoFor(pool | ||
236 | + .deviceId()); | ||
237 | + | ||
238 | + if (!replicaInfo.master().isPresent()) { | ||
239 | + log.warn("Failed to getFlowEntries: No master for {}", pool); | ||
240 | + return false; | ||
241 | + } | ||
242 | + | ||
243 | + if (replicaInfo.master().get() | ||
244 | + .equals(clusterService.getLocalNode().id())) { | ||
245 | + return internalCreate(pool); | ||
246 | + } | ||
247 | + | ||
248 | + log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", | ||
249 | + replicaInfo.master().orNull(), pool.deviceId()); | ||
250 | + | ||
251 | + return complete(clusterCommunicator | ||
252 | + .sendAndReceive(pool, | ||
253 | + LabelResourceMessageSubjects.LABEL_POOL_CREATED, | ||
254 | + SERIALIZER::encode, SERIALIZER::decode, | ||
255 | + replicaInfo.master().get())); | ||
256 | + } | ||
257 | + | ||
258 | + private boolean internalCreate(LabelResourcePool pool) { | ||
259 | + resourcePoolLock.writeLock().lock(); | ||
260 | + LabelResourcePool poolOld = resourcePool.get(pool.deviceId()).value(); | ||
261 | + if (poolOld == null) { | ||
262 | + resourcePool.put(pool.deviceId(), pool); | ||
263 | + resourcePoolLock.writeLock().unlock(); | ||
264 | + LabelResourceEvent event = new LabelResourceEvent( | ||
265 | + Type.POOL_CREATED, | ||
266 | + pool); | ||
267 | + notifyDelegate(event); | ||
268 | + return true; | ||
269 | + } | ||
270 | + resourcePoolLock.writeLock().unlock(); | ||
271 | + return false; | ||
272 | + } | ||
273 | + | ||
274 | + @Override | ||
275 | + public boolean destroyDevicePool(DeviceId deviceId) { | ||
276 | + Device device = (Device) deviceService.getDevice(deviceId); | ||
277 | + if (device == null) { | ||
278 | + return false; | ||
279 | + } | ||
280 | + ReplicaInfo replicaInfo = replicaInfoManager | ||
281 | + .getReplicaInfoFor(deviceId); | ||
282 | + | ||
283 | + if (!replicaInfo.master().isPresent()) { | ||
284 | + log.warn("Failed to getFlowEntries: No master for {}", deviceId); | ||
285 | + return false; | ||
286 | + } | ||
287 | + | ||
288 | + if (replicaInfo.master().get() | ||
289 | + .equals(clusterService.getLocalNode().id())) { | ||
290 | + return internalDestroy(deviceId); | ||
291 | + } | ||
292 | + | ||
293 | + log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", | ||
294 | + replicaInfo.master().orNull(), deviceId); | ||
295 | + | ||
296 | + return complete(clusterCommunicator | ||
297 | + .sendAndReceive(deviceId, | ||
298 | + LabelResourceMessageSubjects.LABEL_POOL_DESTROYED, | ||
299 | + SERIALIZER::encode, SERIALIZER::decode, | ||
300 | + replicaInfo.master().get())); | ||
301 | + } | ||
302 | + | ||
303 | + private boolean internalDestroy(DeviceId deviceId) { | ||
304 | + LabelResourcePool poolOld = resourcePool.get(deviceId).value(); | ||
305 | + if (poolOld != null) { | ||
306 | + resourcePool.remove(deviceId); | ||
307 | + LabelResourceEvent event = new LabelResourceEvent( | ||
308 | + Type.POOL_CREATED, | ||
309 | + poolOld); | ||
310 | + notifyDelegate(event); | ||
311 | + } | ||
312 | + log.info("success to destroy the label resource pool of device id {}", | ||
313 | + deviceId); | ||
314 | + return true; | ||
315 | + } | ||
316 | + | ||
317 | + @Override | ||
318 | + public Collection<LabelResource> applyFromDevicePool(DeviceId deviceId, | ||
319 | + long applyNum) { | ||
320 | + Device device = (Device) deviceService.getDevice(deviceId); | ||
321 | + if (device == null) { | ||
322 | + return Collections.emptyList(); | ||
323 | + } | ||
324 | + LabelResourceRequest request = new LabelResourceRequest( | ||
325 | + deviceId, | ||
326 | + LabelResourceRequest.Type.APPLY, | ||
327 | + applyNum, null); | ||
328 | + ReplicaInfo replicaInfo = replicaInfoManager | ||
329 | + .getReplicaInfoFor(deviceId); | ||
330 | + | ||
331 | + if (!replicaInfo.master().isPresent()) { | ||
332 | + log.warn("Failed to getFlowEntries: No master for {}", deviceId); | ||
333 | + return Collections.emptyList(); | ||
334 | + } | ||
335 | + | ||
336 | + if (replicaInfo.master().get() | ||
337 | + .equals(clusterService.getLocalNode().id())) { | ||
338 | + return internalApply(request); | ||
339 | + } | ||
340 | + | ||
341 | + log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", | ||
342 | + replicaInfo.master().orNull(), deviceId); | ||
343 | + | ||
344 | + return complete(clusterCommunicator | ||
345 | + .sendAndReceive(request, | ||
346 | + LabelResourceMessageSubjects.LABEL_POOL_APPLY, | ||
347 | + SERIALIZER::encode, SERIALIZER::decode, | ||
348 | + replicaInfo.master().get())); | ||
349 | + } | ||
350 | + | ||
351 | + private Collection<LabelResource> internalApply(LabelResourceRequest request) { | ||
352 | + resourcePoolLock.writeLock().lock(); | ||
353 | + DeviceId deviceId = request.deviceId(); | ||
354 | + long applyNum = request.applyNum(); | ||
355 | + LabelResourcePool pool = resourcePool.get(deviceId).value(); | ||
356 | + Collection<LabelResource> result = new HashSet<LabelResource>(); | ||
357 | + long freeNum = this.getFreeNumOfDevicePool(deviceId); | ||
358 | + if (applyNum > freeNum) { | ||
359 | + log.info("the free number of the label resource pool of deviceId {} is not enough."); | ||
360 | + resourcePoolLock.writeLock().unlock(); | ||
361 | + return Collections.emptyList(); | ||
362 | + } | ||
363 | + Set<LabelResource> releaseLabels = new HashSet<LabelResource>( | ||
364 | + pool.releaseLabelId()); | ||
365 | + long tmp = releaseLabels.size() > applyNum ? applyNum : releaseLabels | ||
366 | + .size(); | ||
367 | + LabelResource resource = null; | ||
368 | + for (int i = 0; i < tmp; i++) { | ||
369 | + Iterator<LabelResource> it = releaseLabels.iterator(); | ||
370 | + if (it.hasNext()) { | ||
371 | + resource = it.next(); | ||
372 | + releaseLabels.remove(resource); | ||
373 | + } | ||
374 | + result.add(resource); | ||
375 | + } | ||
376 | + for (long j = pool.currentUsedMaxLabelId().labelId(); j < pool | ||
377 | + .currentUsedMaxLabelId().labelId() + applyNum - tmp; j++) { | ||
378 | + resource = new DefaultLabelResource(deviceId, | ||
379 | + LabelResourceId | ||
380 | + .labelResourceId(j)); | ||
381 | + result.add(resource); | ||
382 | + } | ||
383 | + long beginLabel = pool.beginLabel().labelId(); | ||
384 | + long endLabel = pool.endLabel().labelId(); | ||
385 | + long totalNum = pool.totalNum(); | ||
386 | + long current = pool.currentUsedMaxLabelId().labelId() + applyNum - tmp; | ||
387 | + long usedNum = pool.usedNum() + applyNum; | ||
388 | + ImmutableSet<LabelResource> freeLabel = ImmutableSet | ||
389 | + .copyOf(releaseLabels); | ||
390 | + LabelResourcePool newPool = new LabelResourcePool(deviceId.toString(), | ||
391 | + beginLabel, endLabel, | ||
392 | + totalNum, usedNum, | ||
393 | + current, freeLabel); | ||
394 | + resourcePool.put(deviceId, newPool); | ||
395 | + log.info("success to apply label resource"); | ||
396 | + resourcePoolLock.writeLock().unlock(); | ||
397 | + return result; | ||
398 | + } | ||
399 | + | ||
400 | + @Override | ||
401 | + public boolean releaseToDevicePool(Multimap<DeviceId, LabelResource> release) { | ||
402 | + Map<DeviceId, Collection<LabelResource>> maps = release.asMap(); | ||
403 | + Set<DeviceId> deviceIdSet = maps.keySet(); | ||
404 | + LabelResourceRequest request = null; | ||
405 | + for (Iterator<DeviceId> it = deviceIdSet.iterator(); it.hasNext();) { | ||
406 | + DeviceId deviceId = (DeviceId) it.next(); | ||
407 | + Device device = (Device) deviceService.getDevice(deviceId); | ||
408 | + if (device == null) { | ||
409 | + continue; | ||
410 | + } | ||
411 | + ImmutableSet<LabelResource> collection = ImmutableSet.copyOf(maps | ||
412 | + .get(deviceId)); | ||
413 | + request = new LabelResourceRequest( | ||
414 | + deviceId, | ||
415 | + LabelResourceRequest.Type.RELEASE, | ||
416 | + 0, collection); | ||
417 | + ReplicaInfo replicaInfo = replicaInfoManager | ||
418 | + .getReplicaInfoFor(deviceId); | ||
419 | + | ||
420 | + if (!replicaInfo.master().isPresent()) { | ||
421 | + log.warn("Failed to getFlowEntries: No master for {}", deviceId); | ||
422 | + return false; | ||
423 | + } | ||
424 | + | ||
425 | + if (replicaInfo.master().get() | ||
426 | + .equals(clusterService.getLocalNode().id())) { | ||
427 | + return internalRelease(request); | ||
428 | + } | ||
429 | + | ||
430 | + log.trace("Forwarding getFlowEntries to {}, which is the primary (master) for device {}", | ||
431 | + replicaInfo.master().orNull(), deviceId); | ||
432 | + | ||
433 | + return complete(clusterCommunicator | ||
434 | + .sendAndReceive(request, | ||
435 | + LabelResourceMessageSubjects.LABEL_POOL_RELEASE, | ||
436 | + SERIALIZER::encode, SERIALIZER::decode, | ||
437 | + replicaInfo.master().get())); | ||
438 | + } | ||
439 | + return false; | ||
440 | + } | ||
441 | + | ||
442 | + private boolean internalRelease(LabelResourceRequest request) { | ||
443 | + resourcePoolLock.writeLock().lock(); | ||
444 | + DeviceId deviceId = request.deviceId(); | ||
445 | + Collection<LabelResource> release = request.releaseCollection(); | ||
446 | + LabelResourcePool pool = resourcePool.get(deviceId).value(); | ||
447 | + if (pool == null) { | ||
448 | + resourcePoolLock.writeLock().unlock(); | ||
449 | + log.info("the label resource pool of device id {} does not exist"); | ||
450 | + return false; | ||
451 | + } | ||
452 | + Set<LabelResource> storeSet = new HashSet<LabelResource>( | ||
453 | + pool.releaseLabelId()); | ||
454 | + LabelResource labelResource = null; | ||
455 | + long realReleasedNum = 0; | ||
456 | + for (Iterator<LabelResource> it = release.iterator(); it.hasNext();) { | ||
457 | + labelResource = it.next(); | ||
458 | + if (labelResource.labelResourceId().labelId() < pool.beginLabel() | ||
459 | + .labelId() | ||
460 | + || labelResource.labelResourceId().labelId() > pool | ||
461 | + .endLabel().labelId()) { | ||
462 | + continue; | ||
463 | + } | ||
464 | + if (pool.currentUsedMaxLabelId().labelId() > labelResource | ||
465 | + .labelResourceId().labelId() | ||
466 | + || !storeSet.contains(labelResource)) { | ||
467 | + storeSet.add(labelResource); | ||
468 | + realReleasedNum++; | ||
469 | + } | ||
470 | + } | ||
471 | + long beginNum = pool.beginLabel().labelId(); | ||
472 | + long endNum = pool.endLabel().labelId(); | ||
473 | + long totalNum = pool.totalNum(); | ||
474 | + long usedNum = pool.usedNum() - realReleasedNum; | ||
475 | + long current = pool.currentUsedMaxLabelId().labelId(); | ||
476 | + ImmutableSet<LabelResource> s = ImmutableSet.copyOf(storeSet); | ||
477 | + LabelResourcePool newPool = new LabelResourcePool(deviceId.toString(), | ||
478 | + beginNum, endNum, | ||
479 | + totalNum, usedNum, | ||
480 | + current, s); | ||
481 | + resourcePool.put(deviceId, newPool); | ||
482 | + log.info("success to release label resource"); | ||
483 | + resourcePoolLock.writeLock().unlock(); | ||
484 | + return true; | ||
485 | + } | ||
486 | + | ||
487 | + @Override | ||
488 | + public boolean isDevicePoolFull(DeviceId deviceId) { | ||
489 | + LabelResourcePool pool = resourcePool.get(deviceId).value(); | ||
490 | + if (pool == null) { | ||
491 | + return true; | ||
492 | + } | ||
493 | + return pool.currentUsedMaxLabelId() == pool.endLabel() | ||
494 | + && pool.releaseLabelId().size() == 0 ? true : false; | ||
495 | + } | ||
496 | + | ||
497 | + @Override | ||
498 | + public long getFreeNumOfDevicePool(DeviceId deviceId) { | ||
499 | + LabelResourcePool pool = resourcePool.get(deviceId).value(); | ||
500 | + if (pool == null) { | ||
501 | + return 0; | ||
502 | + } | ||
503 | + return pool.endLabel().labelId() | ||
504 | + - pool.currentUsedMaxLabelId().labelId() | ||
505 | + + pool.releaseLabelId().size(); | ||
506 | + } | ||
507 | + | ||
508 | + @Override | ||
509 | + public LabelResourcePool getDeviceLabelResourcePool(DeviceId deviceId) { | ||
510 | + return resourcePool.get(deviceId).value(); | ||
511 | + } | ||
512 | + | ||
513 | + @Override | ||
514 | + public boolean destroyGlobalPool() { | ||
515 | + return this.internalDestroy(DeviceId | ||
516 | + .deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID)); | ||
517 | + } | ||
518 | + | ||
519 | + @Override | ||
520 | + public Collection<LabelResource> applyFromGlobalPool(long applyNum) { | ||
521 | + LabelResourceRequest request = new LabelResourceRequest( | ||
522 | + DeviceId.deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID), | ||
523 | + LabelResourceRequest.Type.APPLY, | ||
524 | + applyNum, null); | ||
525 | + return this.internalApply(request); | ||
526 | + } | ||
527 | + | ||
528 | + @Override | ||
529 | + public boolean releaseToGlobalPool(Set<LabelResourceId> release) { | ||
530 | + Set<LabelResource> set = new HashSet<LabelResource>(); | ||
531 | + DefaultLabelResource resource = null; | ||
532 | + for (LabelResourceId labelResource : release) { | ||
533 | + resource = new DefaultLabelResource( | ||
534 | + DeviceId.deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID), | ||
535 | + labelResource); | ||
536 | + set.add(resource); | ||
537 | + } | ||
538 | + LabelResourceRequest request = new LabelResourceRequest( | ||
539 | + DeviceId.deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID), | ||
540 | + LabelResourceRequest.Type.APPLY, | ||
541 | + 0, | ||
542 | + ImmutableSet | ||
543 | + .copyOf(set)); | ||
544 | + return this.internalRelease(request); | ||
545 | + } | ||
546 | + | ||
547 | + @Override | ||
548 | + public boolean isGlobalPoolFull() { | ||
549 | + return this.isDevicePoolFull(DeviceId | ||
550 | + .deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID)); | ||
551 | + } | ||
552 | + | ||
553 | + @Override | ||
554 | + public long getFreeNumOfGlobalPool() { | ||
555 | + return this.getFreeNumOfDevicePool(DeviceId | ||
556 | + .deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID)); | ||
557 | + } | ||
558 | + | ||
559 | + @Override | ||
560 | + public LabelResourcePool getGlobalLabelResourcePool() { | ||
561 | + return this.getDeviceLabelResourcePool(DeviceId | ||
562 | + .deviceId(GLOBAL_RESOURCE_POOL_DEVICE_ID)); | ||
563 | + } | ||
564 | + | ||
565 | + private <T> T complete(Future<T> future) { | ||
566 | + try { | ||
567 | + return future.get(PEER_REQUEST_TIMEOUT_MS, | ||
568 | + TimeUnit.MILLISECONDS); | ||
569 | + } catch (InterruptedException e) { | ||
570 | + Thread.currentThread().interrupt(); | ||
571 | + log.error("Interrupted while waiting for operation to complete.", e); | ||
572 | + return null; | ||
573 | + } catch (TimeoutException | ExecutionException e) { | ||
574 | + log.error("Failed remote operation", e); | ||
575 | + return null; | ||
576 | + } | ||
577 | + } | ||
578 | +} |
core/store/dist/src/main/java/org/onosproject/store/resource/impl/LabelResourceMessageSubjects.java
0 → 100644
1 | +package org.onosproject.store.resource.impl; | ||
2 | + | ||
3 | +import org.onosproject.store.cluster.messaging.MessageSubject; | ||
4 | + | ||
5 | +public final class LabelResourceMessageSubjects { | ||
6 | + | ||
7 | + private LabelResourceMessageSubjects() { | ||
8 | + } | ||
9 | + public static final MessageSubject LABEL_POOL_CREATED | ||
10 | + = new MessageSubject("label-resource-pool-created"); | ||
11 | + public static final MessageSubject LABEL_POOL_DESTROYED | ||
12 | + = new MessageSubject("label-resource-pool-destroyed"); | ||
13 | + public static final MessageSubject LABEL_POOL_APPLY | ||
14 | + = new MessageSubject("label-resource-pool-apply"); | ||
15 | + public static final MessageSubject LABEL_POOL_RELEASE | ||
16 | + = new MessageSubject("label-resource-pool-release"); | ||
17 | +} |
-
Please register or login to post a comment