weibit

Merge branch 'master' of ssh://gerrit.onlab.us:29418/onos-next

...@@ -62,6 +62,32 @@ ...@@ -62,6 +62,32 @@
62 <groupId>org.osgi</groupId> 62 <groupId>org.osgi</groupId>
63 <artifactId>org.osgi.core</artifactId> 63 <artifactId>org.osgi.core</artifactId>
64 </dependency> 64 </dependency>
65 +
66 + <dependency>
67 + <groupId>org.onlab.onos</groupId>
68 + <artifactId>onlab-thirdparty</artifactId>
69 + </dependency>
70 +
71 + <dependency>
72 + <groupId>org.onlab.onos</groupId>
73 + <artifactId>onlab-misc</artifactId>
74 + </dependency>
75 +
76 + <dependency>
77 + <groupId>org.onlab.onos</groupId>
78 + <artifactId>onlab-junit</artifactId>
79 + <scope>test</scope>
80 + </dependency>
81 +
82 + <dependency>
83 + <groupId>org.onlab.onos</groupId>
84 + <artifactId>onos-cli</artifactId>
85 + <version>${project.version}</version>
86 + </dependency>
87 + <dependency>
88 + <groupId>org.apache.karaf.shell</groupId>
89 + <artifactId>org.apache.karaf.shell.console</artifactId>
90 + </dependency>
65 </dependencies> 91 </dependencies>
66 92
67 <build> 93 <build>
...@@ -77,6 +103,7 @@ ...@@ -77,6 +103,7 @@
77 ${project.groupId}.${project.artifactId} 103 ${project.groupId}.${project.artifactId}
78 </Bundle-SymbolicName> 104 </Bundle-SymbolicName>
79 <Import-Package> 105 <Import-Package>
106 + org.slf4j,
80 org.osgi.framework, 107 org.osgi.framework,
81 javax.ws.rs,javax.ws.rs.core, 108 javax.ws.rs,javax.ws.rs.core,
82 com.sun.jersey.api.core, 109 com.sun.jersey.api.core,
......
...@@ -16,43 +16,47 @@ ...@@ -16,43 +16,47 @@
16 package org.onlab.onos.calendar; 16 package org.onlab.onos.calendar;
17 17
18 import java.net.URI; 18 import java.net.URI;
19 - 19 +import org.onlab.onos.net.ConnectPoint;
20 +import org.onlab.onos.net.DeviceId;
21 +import org.onlab.onos.net.intent.IntentService;
22 +import org.onlab.rest.BaseResource;
20 import javax.ws.rs.POST; 23 import javax.ws.rs.POST;
21 -import javax.ws.rs.Path; 24 +import javax.ws.rs.DELETE;
22 import javax.ws.rs.PathParam; 25 import javax.ws.rs.PathParam;
23 import javax.ws.rs.core.Response; 26 import javax.ws.rs.core.Response;
24 -
25 import org.onlab.onos.core.ApplicationId; 27 import org.onlab.onos.core.ApplicationId;
26 import org.onlab.onos.core.CoreService; 28 import org.onlab.onos.core.CoreService;
27 -import org.onlab.onos.net.ConnectPoint;
28 -import org.onlab.onos.net.DeviceId;
29 import org.onlab.onos.net.flow.DefaultTrafficSelector; 29 import org.onlab.onos.net.flow.DefaultTrafficSelector;
30 import org.onlab.onos.net.flow.TrafficSelector; 30 import org.onlab.onos.net.flow.TrafficSelector;
31 import org.onlab.onos.net.flow.TrafficTreatment; 31 import org.onlab.onos.net.flow.TrafficTreatment;
32 -import org.onlab.onos.net.intent.Intent; 32 +import org.onlab.onos.net.intent.PointToPointIntent;
33 -import org.onlab.onos.net.intent.IntentService;
34 -import org.onlab.onos.net.intent.PointToPointIntentWithBandwidthConstraint;
35 -import org.onlab.onos.net.resource.BandwidthResourceRequest;
36 import org.onlab.packet.Ethernet; 33 import org.onlab.packet.Ethernet;
37 -import org.onlab.rest.BaseResource;
38 -
39 import static org.onlab.onos.net.PortNumber.portNumber; 34 import static org.onlab.onos.net.PortNumber.portNumber;
40 import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; 35 import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
41 36
37 +import static org.slf4j.LoggerFactory.getLogger;
38 +import org.slf4j.Logger;
39 +
42 /** 40 /**
43 * Web resource for triggering calendared intents. 41 * Web resource for triggering calendared intents.
44 */ 42 */
45 -@Path("intent") 43 +@javax.ws.rs.Path("intent")
46 public class BandwidthCalendarResource extends BaseResource { 44 public class BandwidthCalendarResource extends BaseResource {
47 45
46 + private static final Logger log = getLogger(BandwidthCalendarResource.class);
47 +
48 + @javax.ws.rs.Path("/{src}/{dst}/{srcPort}/{dstPort}/{bandwidth}")
48 @POST 49 @POST
49 - @Path("{src}/{dst}/{srcPort}/{dstPort}/{bandwidth}")
50 public Response createIntent(@PathParam("src") String src, 50 public Response createIntent(@PathParam("src") String src,
51 @PathParam("dst") String dst, 51 @PathParam("dst") String dst,
52 @PathParam("srcPort") String srcPort, 52 @PathParam("srcPort") String srcPort,
53 @PathParam("dstPort") String dstPort, 53 @PathParam("dstPort") String dstPort,
54 @PathParam("bandwidth") String bandwidth) { 54 @PathParam("bandwidth") String bandwidth) {
55 - // TODO: implement calls to intent framework 55 +
56 + log.info("Receiving Create Intent request...");
57 + log.info("Path Constraints: Src = {} SrcPort = {} Dest = {} DestPort = {} BW = {}",
58 + src, srcPort, dst, dstPort, bandwidth);
59 +
56 IntentService service = get(IntentService.class); 60 IntentService service = get(IntentService.class);
57 61
58 ConnectPoint srcPoint = new ConnectPoint(deviceId(src), portNumber(srcPort)); 62 ConnectPoint srcPoint = new ConnectPoint(deviceId(src), portNumber(srcPort));
...@@ -61,13 +65,38 @@ public class BandwidthCalendarResource extends BaseResource { ...@@ -61,13 +65,38 @@ public class BandwidthCalendarResource extends BaseResource {
61 TrafficSelector selector = buildTrafficSelector(); 65 TrafficSelector selector = buildTrafficSelector();
62 TrafficTreatment treatment = builder().build(); 66 TrafficTreatment treatment = builder().build();
63 67
64 - Intent intent = new PointToPointIntentWithBandwidthConstraint( 68 + PointToPointIntent intentP2P =
65 - appId(), selector, treatment, 69 + new PointToPointIntent(appId(), selector, treatment,
66 - srcPoint, dstPoint, new BandwidthResourceRequest(Double.parseDouble(bandwidth))); 70 + srcPoint, dstPoint);
67 - service.submit(intent); 71 + service.submit(intentP2P);
72 + log.info("Submitted Calendar App intent: src = " + src + "dest = " + dst
73 + + "srcPort = " + srcPort + "destPort" + dstPort + "intentID = " + intentP2P.id().toString());
74 + String reply = intentP2P.id().toString() + "\n";
68 75
69 - return Response.ok("Yo! We got src=" + srcPoint + "; dst=" + dstPoint + 76 + return Response.ok(reply).build();
70 - "; bw=" + bandwidth + "; intent service " + service).build(); 77 + }
78 +
79 + @javax.ws.rs.Path("/cancellation/{intentId}")
80 + @DELETE
81 + public Response withdrawIntent(@PathParam("intentId") String intentId) {
82 +
83 + log.info("Receiving Teardown request...");
84 + log.info("Withdraw intentId = {} ", intentId);
85 +
86 + String reply = "ok\n";
87 + return Response.ok(reply).build();
88 + }
89 +
90 + @javax.ws.rs.Path("/modification/{intentId}/{bandwidth}")
91 + @POST
92 + public Response modifyBandwidth(@PathParam("intentId") String intentId,
93 + @PathParam("bandwidth") String bandwidth) {
94 +
95 + log.info("Receiving Modify request...");
96 + log.info("Modify bw for intentId = {} with new bandwidth = {}", intentId, bandwidth);
97 +
98 + String reply = "ok\n";
99 + return Response.ok(reply).build();
71 } 100 }
72 101
73 private TrafficSelector buildTrafficSelector() { 102 private TrafficSelector buildTrafficSelector() {
...@@ -86,5 +115,4 @@ public class BandwidthCalendarResource extends BaseResource { ...@@ -86,5 +115,4 @@ public class BandwidthCalendarResource extends BaseResource {
86 protected ApplicationId appId() { 115 protected ApplicationId appId() {
87 return get(CoreService.class).registerApplication("org.onlab.onos.calendar"); 116 return get(CoreService.class).registerApplication("org.onlab.onos.calendar");
88 } 117 }
89 -
90 } 118 }
......
...@@ -188,7 +188,7 @@ public final class DefaultTrafficSelector implements TrafficSelector { ...@@ -188,7 +188,7 @@ public final class DefaultTrafficSelector implements TrafficSelector {
188 } 188 }
189 189
190 @Override 190 @Override
191 - public Builder matchOpticalSignalType(Byte signalType) { 191 + public Builder matchOpticalSignalType(Short signalType) {
192 return add(Criteria.matchOpticalSignalType(signalType)); 192 return add(Criteria.matchOpticalSignalType(signalType));
193 193
194 } 194 }
......
...@@ -27,11 +27,14 @@ public final class FlowRuleBatchEvent extends AbstractEvent<FlowRuleBatchEvent.T ...@@ -27,11 +27,14 @@ public final class FlowRuleBatchEvent extends AbstractEvent<FlowRuleBatchEvent.T
27 */ 27 */
28 public enum Type { 28 public enum Type {
29 29
30 + // Request has been forwarded to MASTER Node
30 /** 31 /**
31 * Signifies that a batch operation has been initiated. 32 * Signifies that a batch operation has been initiated.
32 */ 33 */
33 BATCH_OPERATION_REQUESTED, 34 BATCH_OPERATION_REQUESTED,
34 35
36 + // MASTER Node has pushed the batch down to the Device
37 + // (e.g., Received barrier reply)
35 /** 38 /**
36 * Signifies that a batch operation has completed. 39 * Signifies that a batch operation has completed.
37 */ 40 */
......
...@@ -25,29 +25,29 @@ import com.google.common.collect.Lists; ...@@ -25,29 +25,29 @@ import com.google.common.collect.Lists;
25 public class FlowRuleBatchRequest { 25 public class FlowRuleBatchRequest {
26 26
27 private final int batchId; 27 private final int batchId;
28 - private final List<FlowEntry> toAdd; 28 + private final List<FlowRule> toAdd;
29 - private final List<FlowEntry> toRemove; 29 + private final List<FlowRule> toRemove;
30 30
31 - public FlowRuleBatchRequest(int batchId, List<? extends FlowEntry> toAdd, List<? extends FlowEntry> toRemove) { 31 + public FlowRuleBatchRequest(int batchId, List<? extends FlowRule> toAdd, List<? extends FlowRule> toRemove) {
32 this.batchId = batchId; 32 this.batchId = batchId;
33 this.toAdd = Collections.unmodifiableList(toAdd); 33 this.toAdd = Collections.unmodifiableList(toAdd);
34 this.toRemove = Collections.unmodifiableList(toRemove); 34 this.toRemove = Collections.unmodifiableList(toRemove);
35 } 35 }
36 36
37 - public List<FlowEntry> toAdd() { 37 + public List<FlowRule> toAdd() {
38 return toAdd; 38 return toAdd;
39 } 39 }
40 40
41 - public List<FlowEntry> toRemove() { 41 + public List<FlowRule> toRemove() {
42 return toRemove; 42 return toRemove;
43 } 43 }
44 44
45 public FlowRuleBatchOperation asBatchOperation() { 45 public FlowRuleBatchOperation asBatchOperation() {
46 List<FlowRuleBatchEntry> entries = Lists.newArrayList(); 46 List<FlowRuleBatchEntry> entries = Lists.newArrayList();
47 - for (FlowEntry e : toAdd) { 47 + for (FlowRule e : toAdd) {
48 entries.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, e)); 48 entries.add(new FlowRuleBatchEntry(FlowRuleOperation.ADD, e));
49 } 49 }
50 - for (FlowEntry e : toRemove) { 50 + for (FlowRule e : toRemove) {
51 entries.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, e)); 51 entries.add(new FlowRuleBatchEntry(FlowRuleOperation.REMOVE, e));
52 } 52 }
53 return new FlowRuleBatchOperation(entries); 53 return new FlowRuleBatchOperation(entries);
......
...@@ -147,7 +147,7 @@ public interface TrafficSelector { ...@@ -147,7 +147,7 @@ public interface TrafficSelector {
147 * @param signalType 147 * @param signalType
148 * @return a selection builder 148 * @return a selection builder
149 */ 149 */
150 - public Builder matchOpticalSignalType(Byte signalType); 150 + public Builder matchOpticalSignalType(Short signalType);
151 151
152 /** 152 /**
153 * Builds an immutable traffic selector. 153 * Builds an immutable traffic selector.
......
...@@ -161,11 +161,11 @@ public final class Criteria { ...@@ -161,11 +161,11 @@ public final class Criteria {
161 /** 161 /**
162 * Creates a match on lambda field using the specified value. 162 * Creates a match on lambda field using the specified value.
163 * 163 *
164 - * @param lambda 164 + * @param sigType
165 * @return match criterion 165 * @return match criterion
166 */ 166 */
167 - public static Criterion matchOpticalSignalType(Byte lambda) { 167 + public static Criterion matchOpticalSignalType(Short sigType) {
168 - return new OpticalSignalTypeCriterion(lambda, Type.OCH_SIGTYPE); 168 + return new OpticalSignalTypeCriterion(sigType, Type.OCH_SIGTYPE);
169 } 169 }
170 170
171 171
...@@ -587,10 +587,10 @@ public final class Criteria { ...@@ -587,10 +587,10 @@ public final class Criteria {
587 587
588 public static final class OpticalSignalTypeCriterion implements Criterion { 588 public static final class OpticalSignalTypeCriterion implements Criterion {
589 589
590 - private final byte signalType; 590 + private final Short signalType;
591 private final Type type; 591 private final Type type;
592 592
593 - public OpticalSignalTypeCriterion(byte signalType, Type type) { 593 + public OpticalSignalTypeCriterion(Short signalType, Type type) {
594 this.signalType = signalType; 594 this.signalType = signalType;
595 this.type = type; 595 this.type = type;
596 } 596 }
...@@ -600,7 +600,7 @@ public final class Criteria { ...@@ -600,7 +600,7 @@ public final class Criteria {
600 return this.type; 600 return this.type;
601 } 601 }
602 602
603 - public Byte signalType() { 603 + public Short signalType() {
604 return this.signalType; 604 return this.signalType;
605 } 605 }
606 606
......
...@@ -15,7 +15,11 @@ ...@@ -15,7 +15,11 @@
15 */ 15 */
16 package org.onlab.onos.net.intent; 16 package org.onlab.onos.net.intent;
17 17
18 +import java.util.List;
19 +
18 import com.google.common.base.MoreObjects; 20 import com.google.common.base.MoreObjects;
21 +import com.google.common.collect.ImmutableList;
22 +
19 import org.onlab.onos.core.ApplicationId; 23 import org.onlab.onos.core.ApplicationId;
20 import org.onlab.onos.net.Path; 24 import org.onlab.onos.net.Path;
21 import org.onlab.onos.net.flow.TrafficSelector; 25 import org.onlab.onos.net.flow.TrafficSelector;
...@@ -28,7 +32,7 @@ import org.onlab.onos.net.resource.LinkResourceRequest; ...@@ -28,7 +32,7 @@ import org.onlab.onos.net.resource.LinkResourceRequest;
28 public class PathIntent extends ConnectivityIntent { 32 public class PathIntent extends ConnectivityIntent {
29 33
30 private final Path path; 34 private final Path path;
31 - private final LinkResourceRequest[] resourceRequests; 35 + private final List<LinkResourceRequest> resourceRequests;
32 36
33 /** 37 /**
34 * Creates a new point-to-point intent with the supplied ingress/egress 38 * Creates a new point-to-point intent with the supplied ingress/egress
...@@ -45,7 +49,7 @@ public class PathIntent extends ConnectivityIntent { ...@@ -45,7 +49,7 @@ public class PathIntent extends ConnectivityIntent {
45 super(id(PathIntent.class, selector, treatment, path), appId, 49 super(id(PathIntent.class, selector, treatment, path), appId,
46 resources(path.links()), selector, treatment); 50 resources(path.links()), selector, treatment);
47 this.path = path; 51 this.path = path;
48 - this.resourceRequests = resourceRequests; 52 + this.resourceRequests = ImmutableList.copyOf(resourceRequests);
49 } 53 }
50 54
51 /** 55 /**
...@@ -54,7 +58,7 @@ public class PathIntent extends ConnectivityIntent { ...@@ -54,7 +58,7 @@ public class PathIntent extends ConnectivityIntent {
54 protected PathIntent() { 58 protected PathIntent() {
55 super(); 59 super();
56 this.path = null; 60 this.path = null;
57 - this.resourceRequests = new LinkResourceRequest[0]; 61 + this.resourceRequests = ImmutableList.of();
58 } 62 }
59 63
60 /** 64 /**
...@@ -71,8 +75,9 @@ public class PathIntent extends ConnectivityIntent { ...@@ -71,8 +75,9 @@ public class PathIntent extends ConnectivityIntent {
71 return true; 75 return true;
72 } 76 }
73 77
78 + // TODO: consider changing return type
74 public LinkResourceRequest[] resourceRequests() { 79 public LinkResourceRequest[] resourceRequests() {
75 - return resourceRequests; 80 + return resourceRequests.toArray(new LinkResourceRequest[resourceRequests.size()]);
76 } 81 }
77 82
78 @Override 83 @Override
......
...@@ -371,10 +371,11 @@ public class FlowRuleManager ...@@ -371,10 +371,11 @@ public class FlowRuleManager
371 final FlowRuleBatchRequest request = event.subject(); 371 final FlowRuleBatchRequest request = event.subject();
372 switch (event.type()) { 372 switch (event.type()) {
373 case BATCH_OPERATION_REQUESTED: 373 case BATCH_OPERATION_REQUESTED:
374 - for (FlowEntry entry : request.toAdd()) { 374 + // Request has been forwarded to MASTER Node, and was
375 + for (FlowRule entry : request.toAdd()) {
375 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry)); 376 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADD_REQUESTED, entry));
376 } 377 }
377 - for (FlowEntry entry : request.toRemove()) { 378 + for (FlowRule entry : request.toRemove()) {
378 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry)); 379 eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVE_REQUESTED, entry));
379 } 380 }
380 // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ? 381 // FIXME: what about op.equals(FlowRuleOperation.MODIFY) ?
...@@ -392,21 +393,15 @@ public class FlowRuleManager ...@@ -392,21 +393,15 @@ public class FlowRuleManager
392 Futures.getUnchecked(result))); 393 Futures.getUnchecked(result)));
393 } 394 }
394 }, futureListeners); 395 }, futureListeners);
395 -
396 break; 396 break;
397 +
397 case BATCH_OPERATION_COMPLETED: 398 case BATCH_OPERATION_COMPLETED:
398 - Set<FlowRule> failedItems = event.result().failedItems(); 399 + // MASTER Node has pushed the batch down to the Device
399 - for (FlowEntry entry : request.toAdd()) { 400 +
400 - if (!failedItems.contains(entry)) { 401 + // Note: RULE_ADDED will be posted
401 - eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_ADDED, entry)); 402 + // when Flow was actually confirmed by stats reply.
402 - }
403 - }
404 - for (FlowEntry entry : request.toRemove()) {
405 - if (!failedItems.contains(entry)) {
406 - eventDispatcher.post(new FlowRuleEvent(FlowRuleEvent.Type.RULE_REMOVED, entry));
407 - }
408 - }
409 break; 403 break;
404 +
410 default: 405 default:
411 break; 406 break;
412 } 407 }
......
...@@ -79,6 +79,7 @@ public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIn ...@@ -79,6 +79,7 @@ public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIn
79 private ApplicationId appId; 79 private ApplicationId appId;
80 80
81 //final short WAVELENGTH = 80; 81 //final short WAVELENGTH = 80;
82 + static final short SIGNAL_TYPE = (short) 1;
82 83
83 @Activate 84 @Activate
84 public void activate() { 85 public void activate() {
...@@ -151,7 +152,9 @@ public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIn ...@@ -151,7 +152,9 @@ public class OpticalPathIntentInstaller implements IntentInstaller<OpticalPathIn
151 152
152 prev = link.dst(); 153 prev = link.dst();
153 selectorBuilder.matchInport(link.dst().port()); 154 selectorBuilder.matchInport(link.dst().port());
155 + selectorBuilder.matchOpticalSignalType(SIGNAL_TYPE); //todo
154 selectorBuilder.matchLambda((short) la.toInt()); 156 selectorBuilder.matchLambda((short) la.toInt());
157 +
155 } 158 }
156 159
157 // build the last T port rule 160 // build the last T port rule
......
...@@ -148,7 +148,7 @@ public class FlowRuleManagerTest { ...@@ -148,7 +148,7 @@ public class FlowRuleManagerTest {
148 int i = 0; 148 int i = 0;
149 System.err.println("events :" + listener.events); 149 System.err.println("events :" + listener.events);
150 for (FlowRuleEvent e : listener.events) { 150 for (FlowRuleEvent e : listener.events) {
151 - assertTrue("unexpected event", e.type().equals(events[i])); 151 + assertEquals("unexpected event", events[i], e.type());
152 i++; 152 i++;
153 } 153 }
154 154
...@@ -178,15 +178,13 @@ public class FlowRuleManagerTest { ...@@ -178,15 +178,13 @@ public class FlowRuleManagerTest {
178 RULE_ADDED, RULE_ADDED); 178 RULE_ADDED, RULE_ADDED);
179 179
180 addFlowRule(1); 180 addFlowRule(1);
181 + System.err.println("events :" + listener.events);
181 assertEquals("should still be 2 rules", 2, flowCount()); 182 assertEquals("should still be 2 rules", 2, flowCount());
182 183
183 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1)); 184 providerService.pushFlowMetrics(DID, ImmutableList.of(fe1));
184 validateEvents(RULE_UPDATED); 185 validateEvents(RULE_UPDATED);
185 } 186 }
186 187
187 -
188 - // TODO: If preserving iteration order is a requirement, redo FlowRuleStore.
189 - //backing store is sensitive to the order of additions/removals
190 private boolean validateState(Map<FlowRule, FlowEntryState> expected) { 188 private boolean validateState(Map<FlowRule, FlowEntryState> expected) {
191 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected); 189 Map<FlowRule, FlowEntryState> expectedToCheck = new HashMap<>(expected);
192 Iterable<FlowEntry> rules = service.getFlowEntries(DID); 190 Iterable<FlowEntry> rules = service.getFlowEntries(DID);
...@@ -539,17 +537,17 @@ public class FlowRuleManagerTest { ...@@ -539,17 +537,17 @@ public class FlowRuleManagerTest {
539 537
540 @Override 538 @Override
541 public boolean cancel(boolean mayInterruptIfRunning) { 539 public boolean cancel(boolean mayInterruptIfRunning) {
542 - return true; 540 + return false;
543 } 541 }
544 542
545 @Override 543 @Override
546 public boolean isCancelled() { 544 public boolean isCancelled() {
547 - return true; 545 + return false;
548 } 546 }
549 547
550 @Override 548 @Override
551 public boolean isDone() { 549 public boolean isDone() {
552 - return false; 550 + return true;
553 } 551 }
554 552
555 @Override 553 @Override
...@@ -562,12 +560,14 @@ public class FlowRuleManagerTest { ...@@ -562,12 +560,14 @@ public class FlowRuleManagerTest {
562 public CompletedBatchOperation get(long timeout, TimeUnit unit) 560 public CompletedBatchOperation get(long timeout, TimeUnit unit)
563 throws InterruptedException, 561 throws InterruptedException,
564 ExecutionException, TimeoutException { 562 ExecutionException, TimeoutException {
565 - return null; 563 + return new CompletedBatchOperation(true, Collections.<FlowRule>emptySet());
566 } 564 }
567 565
568 @Override 566 @Override
569 public void addListener(Runnable task, Executor executor) { 567 public void addListener(Runnable task, Executor executor) {
570 - // TODO: add stuff. 568 + if (isDone()) {
569 + executor.execute(task);
570 + }
571 } 571 }
572 } 572 }
573 573
......
...@@ -447,7 +447,13 @@ implements MastershipStore { ...@@ -447,7 +447,13 @@ implements MastershipStore {
447 RoleValue oldValue = event.getOldValue(); 447 RoleValue oldValue = event.getOldValue();
448 RoleValue newValue = event.getValue(); 448 RoleValue newValue = event.getValue();
449 449
450 - if (Objects.equal(oldValue.get(MASTER), newValue.get(MASTER))) { 450 + NodeId oldMaster = null;
451 + if (oldValue != null) {
452 + oldMaster = oldValue.get(MASTER);
453 + }
454 + NodeId newMaster = newValue.get(MASTER);
455 +
456 + if (Objects.equal(oldMaster, newMaster)) {
451 notifyDelegate(new MastershipEvent( 457 notifyDelegate(new MastershipEvent(
452 MASTER_CHANGED, event.getKey(), event.getValue().roleInfo())); 458 MASTER_CHANGED, event.getKey(), event.getValue().roleInfo()));
453 } else { 459 } else {
......
...@@ -16,8 +16,12 @@ ...@@ -16,8 +16,12 @@
16 package org.onlab.onos.store.trivial.impl; 16 package org.onlab.onos.store.trivial.impl;
17 17
18 import com.google.common.base.Function; 18 import com.google.common.base.Function;
19 +import com.google.common.cache.Cache;
20 +import com.google.common.cache.CacheBuilder;
19 import com.google.common.collect.FluentIterable; 21 import com.google.common.collect.FluentIterable;
20 import com.google.common.util.concurrent.Futures; 22 import com.google.common.util.concurrent.Futures;
23 +import com.google.common.util.concurrent.SettableFuture;
24 +
21 import org.apache.felix.scr.annotations.Activate; 25 import org.apache.felix.scr.annotations.Activate;
22 import org.apache.felix.scr.annotations.Component; 26 import org.apache.felix.scr.annotations.Component;
23 import org.apache.felix.scr.annotations.Deactivate; 27 import org.apache.felix.scr.annotations.Deactivate;
...@@ -43,13 +47,15 @@ import org.onlab.onos.store.AbstractStore; ...@@ -43,13 +47,15 @@ import org.onlab.onos.store.AbstractStore;
43 import org.onlab.util.NewConcurrentHashMap; 47 import org.onlab.util.NewConcurrentHashMap;
44 import org.slf4j.Logger; 48 import org.slf4j.Logger;
45 49
46 -import java.util.Arrays; 50 +import java.util.ArrayList;
47 import java.util.Collections; 51 import java.util.Collections;
48 import java.util.List; 52 import java.util.List;
49 import java.util.concurrent.ConcurrentHashMap; 53 import java.util.concurrent.ConcurrentHashMap;
50 import java.util.concurrent.ConcurrentMap; 54 import java.util.concurrent.ConcurrentMap;
51 import java.util.concurrent.CopyOnWriteArrayList; 55 import java.util.concurrent.CopyOnWriteArrayList;
52 import java.util.concurrent.Future; 56 import java.util.concurrent.Future;
57 +import java.util.concurrent.TimeUnit;
58 +import java.util.concurrent.atomic.AtomicInteger;
53 59
54 import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; 60 import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked;
55 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED; 61 import static org.onlab.onos.net.flow.FlowRuleEvent.Type.RULE_REMOVED;
...@@ -72,6 +78,18 @@ public class SimpleFlowRuleStore ...@@ -72,6 +78,18 @@ public class SimpleFlowRuleStore
72 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>> 78 private final ConcurrentMap<DeviceId, ConcurrentMap<FlowId, List<StoredFlowEntry>>>
73 flowEntries = new ConcurrentHashMap<>(); 79 flowEntries = new ConcurrentHashMap<>();
74 80
81 + private final AtomicInteger localBatchIdGen = new AtomicInteger();
82 +
83 + // TODO: make this configurable
84 + private int pendingFutureTimeoutMinutes = 5;
85 +
86 + private Cache<Integer, SettableFuture<CompletedBatchOperation>> pendingFutures =
87 + CacheBuilder.newBuilder()
88 + .expireAfterWrite(pendingFutureTimeoutMinutes, TimeUnit.MINUTES)
89 + // TODO Explicitly fail the future if expired?
90 + //.removalListener(listener)
91 + .build();
92 +
75 @Activate 93 @Activate
76 public void activate() { 94 public void activate() {
77 log.info("Started"); 95 log.info("Started");
...@@ -173,10 +191,6 @@ public class SimpleFlowRuleStore ...@@ -173,10 +191,6 @@ public class SimpleFlowRuleStore
173 } 191 }
174 // new flow rule added 192 // new flow rule added
175 existing.add(f); 193 existing.add(f);
176 - notifyDelegate(FlowRuleBatchEvent.requested(
177 - new FlowRuleBatchRequest(1, /* FIXME generate something */
178 - Arrays.<FlowEntry>asList(f),
179 - Collections.<FlowEntry>emptyList())));
180 } 194 }
181 } 195 }
182 196
...@@ -190,11 +204,6 @@ public class SimpleFlowRuleStore ...@@ -190,11 +204,6 @@ public class SimpleFlowRuleStore
190 if (entry.equals(rule)) { 204 if (entry.equals(rule)) {
191 synchronized (entry) { 205 synchronized (entry) {
192 entry.setState(FlowEntryState.PENDING_REMOVE); 206 entry.setState(FlowEntryState.PENDING_REMOVE);
193 - // TODO: Should we notify only if it's "remote" event?
194 - notifyDelegate(FlowRuleBatchEvent.requested(
195 - new FlowRuleBatchRequest(1, /* FIXME generate something */
196 - Collections.<FlowEntry>emptyList(),
197 - Arrays.<FlowEntry>asList(entry))));
198 } 207 }
199 } 208 }
200 } 209 }
...@@ -251,20 +260,47 @@ public class SimpleFlowRuleStore ...@@ -251,20 +260,47 @@ public class SimpleFlowRuleStore
251 @Override 260 @Override
252 public Future<CompletedBatchOperation> storeBatch( 261 public Future<CompletedBatchOperation> storeBatch(
253 FlowRuleBatchOperation batchOperation) { 262 FlowRuleBatchOperation batchOperation) {
263 + List<FlowRule> toAdd = new ArrayList<>();
264 + List<FlowRule> toRemove = new ArrayList<>();
254 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) { 265 for (FlowRuleBatchEntry entry : batchOperation.getOperations()) {
266 + final FlowRule flowRule = entry.getTarget();
255 if (entry.getOperator().equals(FlowRuleOperation.ADD)) { 267 if (entry.getOperator().equals(FlowRuleOperation.ADD)) {
256 - storeFlowRule(entry.getTarget()); 268 + if (!getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
269 + storeFlowRule(flowRule);
270 + toAdd.add(flowRule);
271 + }
257 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) { 272 } else if (entry.getOperator().equals(FlowRuleOperation.REMOVE)) {
258 - deleteFlowRule(entry.getTarget()); 273 + if (getFlowEntries(flowRule.deviceId(), flowRule.id()).contains(flowRule)) {
274 + deleteFlowRule(flowRule);
275 + toRemove.add(flowRule);
276 + }
259 } else { 277 } else {
260 throw new UnsupportedOperationException("Unsupported operation type"); 278 throw new UnsupportedOperationException("Unsupported operation type");
261 } 279 }
262 } 280 }
263 - return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowEntry>emptySet())); 281 +
282 + if (toAdd.isEmpty() && toRemove.isEmpty()) {
283 + return Futures.immediateFuture(new CompletedBatchOperation(true, Collections.<FlowRule>emptySet()));
284 + }
285 +
286 + SettableFuture<CompletedBatchOperation> r = SettableFuture.create();
287 + final int batchId = localBatchIdGen.incrementAndGet();
288 +
289 + pendingFutures.put(batchId, r);
290 + notifyDelegate(FlowRuleBatchEvent.requested(new FlowRuleBatchRequest(batchId, toAdd, toRemove)));
291 +
292 + return r;
264 } 293 }
265 294
266 @Override 295 @Override
267 public void batchOperationComplete(FlowRuleBatchEvent event) { 296 public void batchOperationComplete(FlowRuleBatchEvent event) {
297 + final Integer batchId = event.subject().batchId();
298 + SettableFuture<CompletedBatchOperation> future
299 + = pendingFutures.getIfPresent(batchId);
300 + if (future != null) {
301 + future.set(event.result());
302 + pendingFutures.invalidate(batchId);
303 + }
268 notifyDelegate(event); 304 notifyDelegate(event);
269 } 305 }
270 } 306 }
......
...@@ -352,13 +352,6 @@ ...@@ -352,13 +352,6 @@
352 </dependencies> 352 </dependencies>
353 353
354 <build> 354 <build>
355 - <extensions>
356 - <extension>
357 - <groupId>kr.motd.maven</groupId>
358 - <artifactId>os-maven-plugin</artifactId>
359 - <version>1.2.3.Final</version>
360 - </extension>
361 - </extensions>
362 <pluginManagement> 355 <pluginManagement>
363 <plugins> 356 <plugins>
364 <plugin> 357 <plugin>
......
...@@ -289,7 +289,10 @@ public class FlowEntryBuilder { ...@@ -289,7 +289,10 @@ public class FlowEntryBuilder {
289 case OCH_SIGID: 289 case OCH_SIGID:
290 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber()); 290 builder.matchLambda(match.get(MatchField.OCH_SIGID).getChannelNumber());
291 break; 291 break;
292 - case OCH_SIGTYPE_BASIC: 292 + case OCH_SIGTYPE:
293 + builder.matchOpticalSignalType(match.get(MatchField
294 + .OCH_SIGTYPE).getValue());
295 + break;
293 case ARP_OP: 296 case ARP_OP:
294 case ARP_SHA: 297 case ARP_SHA:
295 case ARP_SPA: 298 case ARP_SPA:
......
...@@ -19,6 +19,7 @@ import static org.slf4j.LoggerFactory.getLogger; ...@@ -19,6 +19,7 @@ import static org.slf4j.LoggerFactory.getLogger;
19 19
20 import org.onlab.onos.net.flow.FlowRule; 20 import org.onlab.onos.net.flow.FlowRule;
21 import org.onlab.onos.net.flow.TrafficSelector; 21 import org.onlab.onos.net.flow.TrafficSelector;
22 +import org.onlab.onos.net.flow.criteria.Criteria;
22 import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion; 23 import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
23 import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion; 24 import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
24 import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion; 25 import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
...@@ -46,6 +47,7 @@ import org.projectfloodlight.openflow.types.Masked; ...@@ -46,6 +47,7 @@ import org.projectfloodlight.openflow.types.Masked;
46 import org.projectfloodlight.openflow.types.OFPort; 47 import org.projectfloodlight.openflow.types.OFPort;
47 import org.projectfloodlight.openflow.types.OFVlanVidMatch; 48 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
48 import org.projectfloodlight.openflow.types.TransportPort; 49 import org.projectfloodlight.openflow.types.TransportPort;
50 +import org.projectfloodlight.openflow.types.U8;
49 import org.projectfloodlight.openflow.types.VlanPcp; 51 import org.projectfloodlight.openflow.types.VlanPcp;
50 import org.projectfloodlight.openflow.types.VlanVid; 52 import org.projectfloodlight.openflow.types.VlanVid;
51 import org.slf4j.Logger; 53 import org.slf4j.Logger;
...@@ -197,6 +199,12 @@ public abstract class FlowModBuilder { ...@@ -197,6 +199,12 @@ public abstract class FlowModBuilder {
197 mBuilder.setExact(MatchField.OCH_SIGID, 199 mBuilder.setExact(MatchField.OCH_SIGID,
198 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1)); 200 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
199 break; 201 break;
202 + case OCH_SIGTYPE:
203 + Criteria.OpticalSignalTypeCriterion sc =
204 + (Criteria.OpticalSignalTypeCriterion) c;
205 + mBuilder.setExact(MatchField.OCH_SIGTYPE,
206 + U8.of(sc.signalType()));
207 + break;
200 case ARP_OP: 208 case ARP_OP:
201 case ARP_SHA: 209 case ARP_SHA:
202 case ARP_SPA: 210 case ARP_SPA:
......
...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState; ...@@ -30,7 +30,6 @@ import static com.google.common.base.Preconditions.checkState;
30 30
31 /** 31 /**
32 * A class representing an IP address. 32 * A class representing an IP address.
33 - * TODO: Add support for IPv6 as well.
34 */ 33 */
35 public final class IpAddress implements Comparable<IpAddress> { 34 public final class IpAddress implements Comparable<IpAddress> {
36 // IP Versions 35 // IP Versions
......
...@@ -17,8 +17,6 @@ package org.onlab.packet; ...@@ -17,8 +17,6 @@ package org.onlab.packet;
17 17
18 import java.util.Objects; 18 import java.util.Objects;
19 19
20 -// TODO: Add support for IPv6 as well.
21 -
22 /** 20 /**
23 * A class representing an IP prefix. A prefix consists of an IP address and 21 * A class representing an IP prefix. A prefix consists of an IP address and
24 * a subnet mask. 22 * a subnet mask.
...@@ -40,26 +38,39 @@ public final class IpPrefix { ...@@ -40,26 +38,39 @@ public final class IpPrefix {
40 * 38 *
41 * @param address the IP address 39 * @param address the IP address
42 * @param prefixLength the prefix length 40 * @param prefixLength the prefix length
41 + * @throws IllegalArgumentException if the prefix length value is invalid
43 */ 42 */
44 private IpPrefix(IpAddress address, int prefixLength) { 43 private IpPrefix(IpAddress address, int prefixLength) {
45 - checkPrefixLength(prefixLength); 44 + checkPrefixLength(address.version(), prefixLength);
46 this.address = IpAddress.makeMaskedAddress(address, prefixLength); 45 this.address = IpAddress.makeMaskedAddress(address, prefixLength);
47 this.prefixLength = (short) prefixLength; 46 this.prefixLength = (short) prefixLength;
48 } 47 }
49 48
50 /** 49 /**
51 - * Checks whether the prefix length is valid. 50 + * Returns the IP version of the prefix.
52 * 51 *
53 - * @param prefixLength the prefix length value to check 52 + * @return the IP version of the prefix
54 - * @throws IllegalArgumentException if the prefix length value is invalid
55 */ 53 */
56 - private static void checkPrefixLength(int prefixLength) { 54 + public IpAddress.Version version() {
57 - if ((prefixLength < 0) || (prefixLength > MAX_INET_MASK_LENGTH)) { 55 + return address.version();
58 - String msg = "Invalid prefix length " + prefixLength + ". " + 56 + }
59 - "The value must be in the interval [0, " + 57 +
60 - MAX_INET_MASK_LENGTH + "]"; 58 + /**
61 - throw new IllegalArgumentException(msg); 59 + * Returns the IP address value of the prefix.
60 + *
61 + * @return the IP address value of the prefix
62 + */
63 + public IpAddress address() {
64 + return address;
62 } 65 }
66 +
67 + /**
68 + * Returns the IP address prefix length.
69 + *
70 + * @return the IP address prefix length
71 + */
72 + public int prefixLength() {
73 + return prefixLength;
63 } 74 }
64 75
65 /** 76 /**
...@@ -68,6 +79,7 @@ public final class IpPrefix { ...@@ -68,6 +79,7 @@ public final class IpPrefix {
68 * @param address an integer representing the IPv4 address 79 * @param address an integer representing the IPv4 address
69 * @param prefixLength the prefix length 80 * @param prefixLength the prefix length
70 * @return an IP prefix 81 * @return an IP prefix
82 + * @throws IllegalArgumentException if the prefix length value is invalid
71 */ 83 */
72 public static IpPrefix valueOf(int address, int prefixLength) { 84 public static IpPrefix valueOf(int address, int prefixLength) {
73 return new IpPrefix(IpAddress.valueOf(address), prefixLength); 85 return new IpPrefix(IpAddress.valueOf(address), prefixLength);
...@@ -80,11 +92,11 @@ public final class IpPrefix { ...@@ -80,11 +92,11 @@ public final class IpPrefix {
80 * @param address the IP address value stored in network byte order 92 * @param address the IP address value stored in network byte order
81 * @param prefixLength the prefix length 93 * @param prefixLength the prefix length
82 * @return an IP prefix 94 * @return an IP prefix
95 + * @throws IllegalArgumentException if the prefix length value is invalid
83 */ 96 */
84 public static IpPrefix valueOf(IpAddress.Version version, byte[] address, 97 public static IpPrefix valueOf(IpAddress.Version version, byte[] address,
85 int prefixLength) { 98 int prefixLength) {
86 - return new IpPrefix(IpAddress.valueOf(version, address), 99 + return new IpPrefix(IpAddress.valueOf(version, address), prefixLength);
87 - prefixLength);
88 } 100 }
89 101
90 /** 102 /**
...@@ -93,6 +105,7 @@ public final class IpPrefix { ...@@ -93,6 +105,7 @@ public final class IpPrefix {
93 * @param address the IP address 105 * @param address the IP address
94 * @param prefixLength the prefix length 106 * @param prefixLength the prefix length
95 * @return an IP prefix 107 * @return an IP prefix
108 + * @throws IllegalArgumentException if the prefix length value is invalid
96 */ 109 */
97 public static IpPrefix valueOf(IpAddress address, int prefixLength) { 110 public static IpPrefix valueOf(IpAddress address, int prefixLength) {
98 return new IpPrefix(address, prefixLength); 111 return new IpPrefix(address, prefixLength);
...@@ -104,6 +117,7 @@ public final class IpPrefix { ...@@ -104,6 +117,7 @@ public final class IpPrefix {
104 * 117 *
105 * @param address an IP prefix in string form, e.g. "10.1.0.0/16" 118 * @param address an IP prefix in string form, e.g. "10.1.0.0/16"
106 * @return an IP prefix 119 * @return an IP prefix
120 + * @throws IllegalArgumentException if the arguments are invalid
107 */ 121 */
108 public static IpPrefix valueOf(String address) { 122 public static IpPrefix valueOf(String address) {
109 final String[] parts = address.split("/"); 123 final String[] parts = address.split("/");
...@@ -119,33 +133,6 @@ public final class IpPrefix { ...@@ -119,33 +133,6 @@ public final class IpPrefix {
119 } 133 }
120 134
121 /** 135 /**
122 - * Returns the IP version of the prefix.
123 - *
124 - * @return the IP version of the prefix
125 - */
126 - public IpAddress.Version version() {
127 - return address.version();
128 - }
129 -
130 - /**
131 - * Returns the IP address value of the prefix.
132 - *
133 - * @return the IP address value of the prefix
134 - */
135 - public IpAddress address() {
136 - return address;
137 - }
138 -
139 - /**
140 - * Returns the IP address prefix length.
141 - *
142 - * @return the IP address prefix length
143 - */
144 - public int prefixLength() {
145 - return prefixLength;
146 - }
147 -
148 - /**
149 * Determines whether a given IP prefix is contained within this prefix. 136 * Determines whether a given IP prefix is contained within this prefix.
150 * 137 *
151 * @param other the IP prefix to test 138 * @param other the IP prefix to test
...@@ -217,4 +204,35 @@ public final class IpPrefix { ...@@ -217,4 +204,35 @@ public final class IpPrefix {
217 builder.append(String.format("%d", prefixLength)); 204 builder.append(String.format("%d", prefixLength));
218 return builder.toString(); 205 return builder.toString();
219 } 206 }
207 +
208 + /**
209 + * Checks whether the prefix length is valid.
210 + *
211 + * @param version the IP address version
212 + * @param prefixLength the prefix length value to check
213 + * @throws IllegalArgumentException if the prefix length value is invalid
214 + */
215 + private static void checkPrefixLength(IpAddress.Version version,
216 + int prefixLength) {
217 + int maxPrefixLen = 0;
218 +
219 + switch (version) {
220 + case INET:
221 + maxPrefixLen = MAX_INET_MASK_LENGTH;
222 + break;
223 + case INET6:
224 + maxPrefixLen = MAX_INET6_MASK_LENGTH;
225 + break;
226 + default:
227 + String msg = "Invalid IP version " + version;
228 + throw new IllegalArgumentException(msg);
229 + }
230 +
231 + if ((prefixLength < 0) || (prefixLength > maxPrefixLen)) {
232 + String msg = "Invalid prefix length " + prefixLength + ". " +
233 + "The value must be in the interval [0, " +
234 + maxPrefixLen + "]";
235 + throw new IllegalArgumentException(msg);
236 + }
237 + }
220 } 238 }
......
...@@ -135,7 +135,7 @@ public class IpAddressTest { ...@@ -135,7 +135,7 @@ public class IpAddressTest {
135 * Tests returning an IPv4 address asn an integer. 135 * Tests returning an IPv4 address asn an integer.
136 */ 136 */
137 @Test 137 @Test
138 - public void testToint() { 138 + public void testToInt() {
139 IpAddress ipAddress; 139 IpAddress ipAddress;
140 140
141 ipAddress = IpAddress.valueOf("1.2.3.4"); 141 ipAddress = IpAddress.valueOf("1.2.3.4");
...@@ -149,10 +149,10 @@ public class IpAddressTest { ...@@ -149,10 +149,10 @@ public class IpAddressTest {
149 } 149 }
150 150
151 /** 151 /**
152 - * Tests valueOf() converter for an integer value. 152 + * Tests valueOf() converter for IPv4 integer value.
153 */ 153 */
154 @Test 154 @Test
155 - public void testValueOfForInteger() { 155 + public void testValueOfForIntegerIPv4() {
156 IpAddress ipAddress; 156 IpAddress ipAddress;
157 157
158 ipAddress = IpAddress.valueOf(0x01020304); 158 ipAddress = IpAddress.valueOf(0x01020304);
......
...@@ -15,123 +15,970 @@ ...@@ -15,123 +15,970 @@
15 */ 15 */
16 package org.onlab.packet; 16 package org.onlab.packet;
17 17
18 -import static org.junit.Assert.assertEquals; 18 +import com.google.common.testing.EqualsTester;
19 +import org.junit.Test;
20 +
21 +import static org.hamcrest.Matchers.equalTo;
22 +import static org.hamcrest.Matchers.is;
23 +import static org.junit.Assert.assertThat;
19 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue; 25 import static org.junit.Assert.assertTrue;
26 +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
21 27
22 -import java.util.Arrays; 28 +/**
29 + * Tests for class {@link IpPrefix}.
30 + */
31 +public class IpPrefixTest {
32 + /**
33 + * Tests the immutability of {@link IpPrefix}.
34 + */
35 + @Test
36 + public void testImmutable() {
37 + assertThatClassIsImmutable(IpPrefix.class);
38 + }
23 39
24 -import org.junit.Test; 40 + /**
25 -import org.onlab.packet.IpAddress.Version; 41 + * Tests the maximum mask length.
42 + */
43 + @Test
44 + public void testMaxMaskLength() {
45 + assertThat(IpPrefix.MAX_INET_MASK_LENGTH, is(32));
46 + assertThat(IpPrefix.MAX_INET6_MASK_LENGTH, is(128));
47 + }
26 48
27 -import com.google.common.testing.EqualsTester; 49 + /**
50 + * Tests returning the IP version of the prefix.
51 + */
52 + @Test
53 + public void testVersion() {
54 + IpPrefix ipPrefix;
28 55
29 -public class IpPrefixTest { 56 + // IPv4
57 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
58 + assertThat(ipPrefix.version(), is(IpAddress.Version.INET));
30 59
31 - private static final byte [] BYTES1 = new byte [] {0xa, 0x0, 0x0, 0xa}; 60 + // IPv6
32 - private static final byte [] BYTES2 = new byte [] {0xa, 0x0, 0x0, 0xb}; 61 + ipPrefix = IpPrefix.valueOf("::/0");
33 - private static final int INTVAL0 = 0x0a000000; 62 + assertThat(ipPrefix.version(), is(IpAddress.Version.INET6));
34 - private static final int INTVAL1 = 0x0a00000a; 63 + }
35 - private static final int INTVAL2 = 0x0a00000b; 64 +
36 - private static final String STRVAL = "10.0.0.12/16"; 65 + /**
37 - private static final int MASK_LENGTH = 16; 66 + * Tests returning the IP address value and IP address prefix length of
38 - 67 + * an IPv4 prefix.
39 - @Test 68 + */
40 - public void testEquality() { 69 + @Test
41 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 70 + public void testAddressAndPrefixLengthIPv4() {
42 - BYTES1, IpPrefix.MAX_INET_MASK_LENGTH); 71 + IpPrefix ipPrefix;
43 - IpPrefix ip2 = IpPrefix.valueOf(INTVAL1, IpPrefix.MAX_INET_MASK_LENGTH); 72 +
44 - IpPrefix ip3 = IpPrefix.valueOf(IpAddress.Version.INET, 73 + ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
45 - BYTES2, IpPrefix.MAX_INET_MASK_LENGTH); 74 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
46 - IpPrefix ip4 = IpPrefix.valueOf(INTVAL2, IpPrefix.MAX_INET_MASK_LENGTH); 75 + assertThat(ipPrefix.prefixLength(), is(24));
47 - IpPrefix ip5 = IpPrefix.valueOf(STRVAL); 76 +
48 - 77 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
49 - new EqualsTester().addEqualityGroup(ip1, ip2) 78 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.0")));
50 - .addEqualityGroup(ip3, ip4) 79 + assertThat(ipPrefix.prefixLength(), is(24));
51 - .addEqualityGroup(ip5) 80 +
52 - .testEquals(); 81 + ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
82 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.4")));
83 + assertThat(ipPrefix.prefixLength(), is(32));
84 +
85 + ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
86 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1.2.3.5")));
87 + assertThat(ipPrefix.prefixLength(), is(32));
88 +
89 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
90 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("0.0.0.0")));
91 + assertThat(ipPrefix.prefixLength(), is(0));
92 +
93 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
94 + assertThat(ipPrefix.address(),
95 + equalTo(IpAddress.valueOf("255.255.255.255")));
96 + assertThat(ipPrefix.prefixLength(), is(32));
97 + }
98 +
99 + /**
100 + * Tests returning the IP address value and IP address prefix length of
101 + * an IPv6 prefix.
102 + */
103 + @Test
104 + public void testAddressAndPrefixLengthIPv6() {
105 + IpPrefix ipPrefix;
106 +
107 + ipPrefix = IpPrefix.valueOf("1100::/8");
108 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
109 + assertThat(ipPrefix.prefixLength(), is(8));
110 +
111 + ipPrefix =
112 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
113 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("1100::")));
114 + assertThat(ipPrefix.prefixLength(), is(8));
115 +
116 + ipPrefix =
117 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8800/120");
118 + assertThat(ipPrefix.address(),
119 + equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8800")));
120 + assertThat(ipPrefix.prefixLength(), is(120));
121 +
122 + ipPrefix =
123 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/128");
124 + assertThat(ipPrefix.address(),
125 + equalTo(IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8885")));
126 + assertThat(ipPrefix.prefixLength(), is(128));
127 +
128 + ipPrefix = IpPrefix.valueOf("::/0");
129 + assertThat(ipPrefix.address(), equalTo(IpAddress.valueOf("::")));
130 + assertThat(ipPrefix.prefixLength(), is(0));
131 +
132 + ipPrefix =
133 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
134 + assertThat(ipPrefix.address(),
135 + equalTo(IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
136 + assertThat(ipPrefix.prefixLength(), is(128));
137 +
138 + ipPrefix =
139 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/64");
140 + assertThat(ipPrefix.address(),
141 + equalTo(IpAddress.valueOf("1111:2222:3333:4444::")));
142 + assertThat(ipPrefix.prefixLength(), is(64));
143 + }
144 +
145 + /**
146 + * Tests valueOf() converter for IPv4 integer value.
147 + */
148 + @Test
149 + public void testValueOfForIntegerIPv4() {
150 + IpPrefix ipPrefix;
151 +
152 + ipPrefix = IpPrefix.valueOf(0x01020304, 24);
153 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
154 +
155 + ipPrefix = IpPrefix.valueOf(0x01020304, 32);
156 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
157 +
158 + ipPrefix = IpPrefix.valueOf(0x01020305, 32);
159 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
160 +
161 + ipPrefix = IpPrefix.valueOf(0, 0);
162 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
163 +
164 + ipPrefix = IpPrefix.valueOf(0, 32);
165 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
166 +
167 + ipPrefix = IpPrefix.valueOf(0xffffffff, 0);
168 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
169 +
170 + ipPrefix = IpPrefix.valueOf(0xffffffff, 16);
171 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
172 +
173 + ipPrefix = IpPrefix.valueOf(0xffffffff, 32);
174 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
175 + }
176 +
177 + /**
178 + * Tests invalid valueOf() converter for IPv4 integer value and
179 + * negative prefix length.
180 + */
181 + @Test(expected = IllegalArgumentException.class)
182 + public void testInvalidValueOfIntegerNegativePrefixLengthIPv4() {
183 + IpPrefix ipPrefix;
184 +
185 + ipPrefix = IpPrefix.valueOf(0x01020304, -1);
186 + }
187 +
188 + /**
189 + * Tests invalid valueOf() converter for IPv4 integer value and
190 + * too long prefix length.
191 + */
192 + @Test(expected = IllegalArgumentException.class)
193 + public void testInvalidValueOfIntegerTooLongPrefixLengthIPv4() {
194 + IpPrefix ipPrefix;
195 +
196 + ipPrefix = IpPrefix.valueOf(0x01020304, 33);
197 + }
198 +
199 + /**
200 + * Tests valueOf() converter for IPv4 byte array.
201 + */
202 + @Test
203 + public void testValueOfByteArrayIPv4() {
204 + IpPrefix ipPrefix;
205 + byte[] value;
206 +
207 + value = new byte[] {1, 2, 3, 4};
208 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
209 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
210 +
211 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
212 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
213 +
214 + value = new byte[] {1, 2, 3, 5};
215 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
216 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
217 +
218 + value = new byte[] {0, 0, 0, 0};
219 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
220 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
221 +
222 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
223 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
224 +
225 + value = new byte[] {(byte) 0xff, (byte) 0xff,
226 + (byte) 0xff, (byte) 0xff};
227 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 0);
228 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
229 +
230 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 16);
231 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
232 +
233 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 32);
234 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
235 + }
236 +
237 + /**
238 + * Tests valueOf() converter for IPv6 byte array.
239 + */
240 + @Test
241 + public void testValueOfByteArrayIPv6() {
242 + IpPrefix ipPrefix;
243 + byte[] value;
244 +
245 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
246 + 0x33, 0x33, 0x44, 0x44,
247 + 0x55, 0x55, 0x66, 0x66,
248 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
249 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
250 + assertThat(ipPrefix.toString(),
251 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
252 +
253 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
254 + assertThat(ipPrefix.toString(),
255 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
256 +
257 + value = new byte[] {0x00, 0x00, 0x00, 0x00,
258 + 0x00, 0x00, 0x00, 0x00,
259 + 0x00, 0x00, 0x00, 0x00,
260 + 0x00, 0x00, 0x00, 0x00};
261 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
262 + assertThat(ipPrefix.toString(), is("::/0"));
263 +
264 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
265 + assertThat(ipPrefix.toString(), is("::/128"));
266 +
267 + value = new byte[] {(byte) 0xff, (byte) 0xff,
268 + (byte) 0xff, (byte) 0xff,
269 + (byte) 0xff, (byte) 0xff,
270 + (byte) 0xff, (byte) 0xff,
271 + (byte) 0xff, (byte) 0xff,
272 + (byte) 0xff, (byte) 0xff,
273 + (byte) 0xff, (byte) 0xff,
274 + (byte) 0xff, (byte) 0xff};
275 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 0);
276 + assertThat(ipPrefix.toString(), is("::/0"));
277 +
278 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 64);
279 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
280 +
281 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 128);
282 + assertThat(ipPrefix.toString(),
283 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
284 + }
285 +
286 + /**
287 + * Tests invalid valueOf() converter for a null array for IPv4.
288 + */
289 + @Test(expected = NullPointerException.class)
290 + public void testInvalidValueOfNullArrayIPv4() {
291 + IpPrefix ipPrefix;
292 + byte[] value;
293 +
294 + value = null;
295 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
296 + }
297 +
298 + /**
299 + * Tests invalid valueOf() converter for a null array for IPv6.
300 + */
301 + @Test(expected = NullPointerException.class)
302 + public void testInvalidValueOfNullArrayIPv6() {
303 + IpPrefix ipPrefix;
304 + byte[] value;
305 +
306 + value = null;
307 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
308 + }
309 +
310 + /**
311 + * Tests invalid valueOf() converter for a short array for IPv4.
312 + */
313 + @Test(expected = IllegalArgumentException.class)
314 + public void testInvalidValueOfShortArrayIPv4() {
315 + IpPrefix ipPrefix;
316 + byte[] value;
317 +
318 + value = new byte[] {1, 2, 3};
319 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 24);
320 + }
321 +
322 + /**
323 + * Tests invalid valueOf() converter for a short array for IPv6.
324 + */
325 + @Test(expected = IllegalArgumentException.class)
326 + public void testInvalidValueOfShortArrayIPv6() {
327 + IpPrefix ipPrefix;
328 + byte[] value;
329 +
330 + value = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9};
331 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 120);
332 + }
333 +
334 + /**
335 + * Tests invalid valueOf() converter for IPv4 byte array and
336 + * negative prefix length.
337 + */
338 + @Test(expected = IllegalArgumentException.class)
339 + public void testInvalidValueOfByteArrayNegativePrefixLengthIPv4() {
340 + IpPrefix ipPrefix;
341 + byte[] value;
342 +
343 + value = new byte[] {1, 2, 3, 4};
344 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, -1);
345 + }
346 +
347 + /**
348 + * Tests invalid valueOf() converter for IPv6 byte array and
349 + * negative prefix length.
350 + */
351 + @Test(expected = IllegalArgumentException.class)
352 + public void testInvalidValueOfByteArrayNegativePrefixLengthIPv6() {
353 + IpPrefix ipPrefix;
354 + byte[] value;
355 +
356 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
357 + 0x33, 0x33, 0x44, 0x44,
358 + 0x55, 0x55, 0x66, 0x66,
359 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
360 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, -1);
361 + }
362 +
363 + /**
364 + * Tests invalid valueOf() converter for IPv4 byte array and
365 + * too long prefix length.
366 + */
367 + @Test(expected = IllegalArgumentException.class)
368 + public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv4() {
369 + IpPrefix ipPrefix;
370 + byte[] value;
371 +
372 + value = new byte[] {1, 2, 3, 4};
373 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET, value, 33);
374 + }
375 +
376 + /**
377 + * Tests invalid valueOf() converter for IPv6 byte array and
378 + * too long prefix length.
379 + */
380 + @Test(expected = IllegalArgumentException.class)
381 + public void testInvalidValueOfByteArrayTooLongPrefixLengthIPv6() {
382 + IpPrefix ipPrefix;
383 + byte[] value;
384 +
385 + value = new byte[] {0x11, 0x11, 0x22, 0x22,
386 + 0x33, 0x33, 0x44, 0x44,
387 + 0x55, 0x55, 0x66, 0x66,
388 + 0x77, 0x77, (byte) 0x88, (byte) 0x88};
389 + ipPrefix = IpPrefix.valueOf(IpAddress.Version.INET6, value, 129);
390 + }
391 +
392 + /**
393 + * Tests valueOf() converter for IPv4 address.
394 + */
395 + @Test
396 + public void testValueOfAddressIPv4() {
397 + IpAddress ipAddress;
398 + IpPrefix ipPrefix;
399 +
400 + ipAddress = IpAddress.valueOf("1.2.3.4");
401 + ipPrefix = IpPrefix.valueOf(ipAddress, 24);
402 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
403 +
404 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
405 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
406 +
407 + ipAddress = IpAddress.valueOf("1.2.3.5");
408 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
409 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
410 +
411 + ipAddress = IpAddress.valueOf("0.0.0.0");
412 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
413 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
53 414
54 - // string conversions 415 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
55 - IpPrefix ip6 = IpPrefix.valueOf(IpAddress.Version.INET, 416 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
56 - BYTES1, MASK_LENGTH); 417 +
57 - IpPrefix ip7 = IpPrefix.valueOf("10.0.0.10/16"); 418 + ipAddress = IpAddress.valueOf("255.255.255.255");
58 - IpPrefix ip8 = IpPrefix.valueOf(IpAddress.Version.INET, 419 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
59 - new byte [] {0xa, 0x0, 0x0, 0xc}, 16); 420 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
60 - assertEquals("incorrect address conversion", ip6, ip7); 421 +
61 - assertEquals("incorrect address conversion", ip5, ip8); 422 + ipPrefix = IpPrefix.valueOf(ipAddress, 16);
423 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
424 +
425 + ipPrefix = IpPrefix.valueOf(ipAddress, 32);
426 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
62 } 427 }
63 428
429 + /**
430 + * Tests valueOf() converter for IPv6 address.
431 + */
64 @Test 432 @Test
65 - public void basics() { 433 + public void testValueOfAddressIPv6() {
66 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 434 + IpAddress ipAddress;
67 - BYTES1, MASK_LENGTH); 435 + IpPrefix ipPrefix;
68 - final byte [] bytes = new byte [] {0xa, 0x0, 0x0, 0x0}; 436 +
437 + ipAddress =
438 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
439 + ipPrefix = IpPrefix.valueOf(ipAddress, 120);
440 + assertThat(ipPrefix.toString(),
441 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
442 +
443 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
444 + assertThat(ipPrefix.toString(),
445 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
446 +
447 + ipAddress = IpAddress.valueOf("::");
448 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
449 + assertThat(ipPrefix.toString(), is("::/0"));
450 +
451 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
452 + assertThat(ipPrefix.toString(), is("::/128"));
69 453
70 - // check fields 454 + ipAddress =
71 - assertEquals("incorrect IP Version", Version.INET, ip1.version()); 455 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
72 - assertEquals("incorrect netmask", 16, ip1.prefixLength()); 456 + ipPrefix = IpPrefix.valueOf(ipAddress, 0);
73 - assertTrue("faulty toOctets()", 457 + assertThat(ipPrefix.toString(), is("::/0"));
74 - Arrays.equals(bytes, ip1.address().toOctets())); 458 +
75 - assertEquals("faulty toInt()", INTVAL0, ip1.address().toInt()); 459 + ipPrefix = IpPrefix.valueOf(ipAddress, 64);
76 - assertEquals("faulty toString()", "10.0.0.0/16", ip1.toString()); 460 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
461 +
462 + ipPrefix = IpPrefix.valueOf(ipAddress, 128);
463 + assertThat(ipPrefix.toString(),
464 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
465 + }
466 +
467 + /**
468 + * Tests invalid valueOf() converter for a null IP address.
469 + */
470 + @Test(expected = NullPointerException.class)
471 + public void testInvalidValueOfNullAddress() {
472 + IpAddress ipAddress;
473 + IpPrefix ipPrefix;
474 +
475 + ipAddress = null;
476 + ipPrefix = IpPrefix.valueOf(ipAddress, 24);
77 } 477 }
78 478
479 + /**
480 + * Tests invalid valueOf() converter for IPv4 address and
481 + * negative prefix length.
482 + */
483 + @Test(expected = IllegalArgumentException.class)
484 + public void testInvalidValueOfAddressNegativePrefixLengthIPv4() {
485 + IpAddress ipAddress;
486 + IpPrefix ipPrefix;
487 +
488 + ipAddress = IpAddress.valueOf("1.2.3.4");
489 + ipPrefix = IpPrefix.valueOf(ipAddress, -1);
490 + }
491 +
492 + /**
493 + * Tests invalid valueOf() converter for IPv6 address and
494 + * negative prefix length.
495 + */
496 + @Test(expected = IllegalArgumentException.class)
497 + public void testInvalidValueOfAddressNegativePrefixLengthIPv6() {
498 + IpAddress ipAddress;
499 + IpPrefix ipPrefix;
500 +
501 + ipAddress =
502 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
503 + ipPrefix = IpPrefix.valueOf(ipAddress, -1);
504 + }
505 +
506 + /**
507 + * Tests invalid valueOf() converter for IPv4 address and
508 + * too long prefix length.
509 + */
510 + @Test(expected = IllegalArgumentException.class)
511 + public void testInvalidValueOfAddressTooLongPrefixLengthIPv4() {
512 + IpAddress ipAddress;
513 + IpPrefix ipPrefix;
514 +
515 + ipAddress = IpAddress.valueOf("1.2.3.4");
516 + ipPrefix = IpPrefix.valueOf(ipAddress, 33);
517 + }
518 +
519 + /**
520 + * Tests invalid valueOf() converter for IPv6 address and
521 + * too long prefix length.
522 + */
523 + @Test(expected = IllegalArgumentException.class)
524 + public void testInvalidValueOfAddressTooLongPrefixLengthIPv6() {
525 + IpAddress ipAddress;
526 + IpPrefix ipPrefix;
527 +
528 + ipAddress =
529 + IpAddress.valueOf("1111:2222:3333:4444:5555:6666:7777:8888");
530 + ipPrefix = IpPrefix.valueOf(ipAddress, 129);
531 + }
532 +
533 + /**
534 + * Tests valueOf() converter for IPv4 string.
535 + */
79 @Test 536 @Test
80 - public void netmasks() { 537 + public void testValueOfStringIPv4() {
81 - // masked 538 + IpPrefix ipPrefix;
82 - IpPrefix ip1 = IpPrefix.valueOf(IpAddress.Version.INET, 539 +
83 - BYTES1, MASK_LENGTH); 540 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
84 - IpPrefix ip2 = IpPrefix.valueOf("10.0.0.10/16"); 541 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
85 - IpPrefix ip3 = IpPrefix.valueOf("10.0.0.0/16"); 542 +
86 - assertEquals("incorrect binary masked address", 543 + ipPrefix = IpPrefix.valueOf("1.2.3.4/32");
87 - ip1.toString(), "10.0.0.0/16"); 544 + assertThat(ipPrefix.toString(), is("1.2.3.4/32"));
88 - assertEquals("incorrect string masked address", 545 +
89 - ip2.toString(), "10.0.0.0/16"); 546 + ipPrefix = IpPrefix.valueOf("1.2.3.5/32");
90 - assertEquals("incorrect network address", 547 + assertThat(ipPrefix.toString(), is("1.2.3.5/32"));
91 - ip2.toString(), "10.0.0.0/16"); 548 +
549 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
550 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
551 +
552 + ipPrefix = IpPrefix.valueOf("0.0.0.0/32");
553 + assertThat(ipPrefix.toString(), is("0.0.0.0/32"));
554 +
555 + ipPrefix = IpPrefix.valueOf("255.255.255.255/0");
556 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
557 +
558 + ipPrefix = IpPrefix.valueOf("255.255.255.255/16");
559 + assertThat(ipPrefix.toString(), is("255.255.0.0/16"));
560 +
561 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
562 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
92 } 563 }
93 564
565 + /**
566 + * Tests valueOf() converter for IPv6 string.
567 + */
94 @Test 568 @Test
95 - public void testContainsIpPrefix() { 569 + public void testValueOfStringIPv6() {
96 - IpPrefix slash31 = IpPrefix.valueOf(IpAddress.Version.INET, 570 + IpPrefix ipPrefix;
97 - BYTES1, 31); 571 +
98 - IpPrefix slash32 = IpPrefix.valueOf(IpAddress.Version.INET, 572 + ipPrefix =
99 - BYTES1, 32); 573 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/120");
100 - IpPrefix differentSlash32 = IpPrefix.valueOf(IpAddress.Version.INET, 574 + assertThat(ipPrefix.toString(),
101 - BYTES2, 32); 575 + is("1111:2222:3333:4444:5555:6666:7777:8800/120"));
576 +
577 + ipPrefix =
578 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/128");
579 + assertThat(ipPrefix.toString(),
580 + is("1111:2222:3333:4444:5555:6666:7777:8888/128"));
581 +
582 + ipPrefix = IpPrefix.valueOf("::/0");
583 + assertThat(ipPrefix.toString(), is("::/0"));
584 +
585 + ipPrefix = IpPrefix.valueOf("::/128");
586 + assertThat(ipPrefix.toString(), is("::/128"));
587 +
588 + ipPrefix =
589 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/0");
590 + assertThat(ipPrefix.toString(), is("::/0"));
591 +
592 + ipPrefix =
593 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/64");
594 + assertThat(ipPrefix.toString(), is("ffff:ffff:ffff:ffff::/64"));
102 595
103 - assertTrue(slash31.contains(differentSlash32)); 596 + ipPrefix =
104 - assertFalse(differentSlash32.contains(slash31)); 597 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
598 + assertThat(ipPrefix.toString(),
599 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
600 + }
601 +
602 + /**
603 + * Tests invalid valueOf() converter for a null string.
604 + */
605 + @Test(expected = NullPointerException.class)
606 + public void testInvalidValueOfNullString() {
607 + IpPrefix ipPrefix;
608 + String fromString;
105 609
106 - assertTrue(slash31.contains(slash32)); 610 + fromString = null;
107 - assertFalse(slash32.contains(differentSlash32)); 611 + ipPrefix = IpPrefix.valueOf(fromString);
108 - assertFalse(differentSlash32.contains(slash32)); 612 + }
109 613
110 - IpPrefix zero = IpPrefix.valueOf("0.0.0.0/0"); 614 + /**
111 - assertTrue(zero.contains(differentSlash32)); 615 + * Tests invalid valueOf() converter for an empty string.
112 - assertFalse(differentSlash32.contains(zero)); 616 + */
617 + @Test(expected = IllegalArgumentException.class)
618 + public void testInvalidValueOfEmptyString() {
619 + IpPrefix ipPrefix;
620 + String fromString;
113 621
114 - IpPrefix slash8 = IpPrefix.valueOf("10.0.0.0/8"); 622 + fromString = "";
115 - assertTrue(slash8.contains(slash31)); 623 + ipPrefix = IpPrefix.valueOf(fromString);
116 - assertFalse(slash31.contains(slash8));
117 } 624 }
118 625
626 + /**
627 + * Tests invalid valueOf() converter for an incorrect string.
628 + */
629 + @Test(expected = IllegalArgumentException.class)
630 + public void testInvalidValueOfIncorrectString() {
631 + IpPrefix ipPrefix;
632 + String fromString;
633 +
634 + fromString = "NoSuchIpPrefix";
635 + ipPrefix = IpPrefix.valueOf(fromString);
636 + }
637 +
638 + /**
639 + * Tests invalid valueOf() converter for IPv4 string and
640 + * negative prefix length.
641 + */
642 + @Test(expected = IllegalArgumentException.class)
643 + public void testInvalidValueOfStringNegativePrefixLengthIPv4() {
644 + IpPrefix ipPrefix;
645 +
646 + ipPrefix = IpPrefix.valueOf("1.2.3.4/-1");
647 + }
648 +
649 + /**
650 + * Tests invalid valueOf() converter for IPv6 string and
651 + * negative prefix length.
652 + */
653 + @Test(expected = IllegalArgumentException.class)
654 + public void testInvalidValueOfStringNegativePrefixLengthIPv6() {
655 + IpPrefix ipPrefix;
656 +
657 + ipPrefix =
658 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/-1");
659 + }
660 +
661 + /**
662 + * Tests invalid valueOf() converter for IPv4 string and
663 + * too long prefix length.
664 + */
665 + @Test(expected = IllegalArgumentException.class)
666 + public void testInvalidValueOfStringTooLongPrefixLengthIPv4() {
667 + IpPrefix ipPrefix;
668 +
669 + ipPrefix = IpPrefix.valueOf("1.2.3.4/33");
670 + }
671 +
672 + /**
673 + * Tests invalid valueOf() converter for IPv6 string and
674 + * too long prefix length.
675 + */
676 + @Test(expected = IllegalArgumentException.class)
677 + public void testInvalidValueOfStringTooLongPrefixLengthIPv6() {
678 + IpPrefix ipPrefix;
679 +
680 + ipPrefix =
681 + IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8888/129");
682 + }
683 +
684 + /**
685 + * Tests IP prefix contains another IP prefix for IPv4.
686 + */
687 + @Test
688 + public void testContainsIpPrefixIPv4() {
689 + IpPrefix ipPrefix;
690 +
691 + ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
692 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
693 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
694 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
695 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
696 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
697 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
698 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
699 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
700 +
701 + ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
702 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
703 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
704 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
705 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
706 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
707 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
708 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
709 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
710 +
711 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
712 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
713 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
714 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
715 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
716 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
717 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
718 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
719 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
720 +
721 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
722 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/24")));
723 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/32")));
724 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.4/32")));
725 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.2.0.0/16")));
726 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("1.3.0.0/24")));
727 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/16")));
728 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("0.0.0.0/0")));
729 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("255.255.255.255/32")));
730 + }
731 +
732 + /**
733 + * Tests IP prefix contains another IP prefix for IPv6.
734 + */
735 + @Test
736 + public void testContainsIpPrefixIPv6() {
737 + IpPrefix ipPrefix;
738 +
739 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
740 + assertTrue(ipPrefix.contains(
741 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
742 + assertTrue(ipPrefix.contains(
743 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
744 + assertTrue(ipPrefix.contains(
745 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
746 + assertFalse(ipPrefix.contains(
747 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
748 + assertFalse(ipPrefix.contains(
749 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
750 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
751 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
752 + assertFalse(ipPrefix.contains(
753 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
754 +
755 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
756 + assertFalse(ipPrefix.contains(
757 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
758 + assertTrue(ipPrefix.contains(
759 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
760 + assertFalse(ipPrefix.contains(
761 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
762 + assertFalse(ipPrefix.contains(
763 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
764 + assertFalse(ipPrefix.contains(
765 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
766 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
767 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
768 + assertFalse(ipPrefix.contains(
769 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
770 +
771 + ipPrefix = IpPrefix.valueOf("::/0");
772 + assertTrue(ipPrefix.contains(
773 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
774 + assertTrue(ipPrefix.contains(
775 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
776 + assertTrue(ipPrefix.contains(
777 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
778 + assertTrue(ipPrefix.contains(
779 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
780 + assertTrue(ipPrefix.contains(
781 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
782 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/64")));
783 + assertTrue(ipPrefix.contains(IpPrefix.valueOf("::/0")));
784 + assertTrue(ipPrefix.contains(
785 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
786 +
787 + ipPrefix =
788 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
789 + assertFalse(ipPrefix.contains(
790 + IpPrefix.valueOf("1111:2222:3333:4444::/120")));
791 + assertFalse(ipPrefix.contains(
792 + IpPrefix.valueOf("1111:2222:3333:4444::/128")));
793 + assertFalse(ipPrefix.contains(
794 + IpPrefix.valueOf("1111:2222:3333:4444::1/128")));
795 + assertFalse(ipPrefix.contains(
796 + IpPrefix.valueOf("1111:2222:3333:4444::/64")));
797 + assertFalse(ipPrefix.contains(
798 + IpPrefix.valueOf("1111:2222:3333:4445::/120")));
799 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/64")));
800 + assertFalse(ipPrefix.contains(IpPrefix.valueOf("::/0")));
801 + assertTrue(ipPrefix.contains(
802 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128")));
803 + }
804 +
805 + /**
806 + * Tests IP prefix contains IP address for IPv4.
807 + */
808 + @Test
809 + public void testContainsIpAddressIPv4() {
810 + IpPrefix ipPrefix;
811 +
812 + ipPrefix = IpPrefix.valueOf("1.2.0.0/24");
813 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
814 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
815 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
816 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
817 + assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
818 +
819 + ipPrefix = IpPrefix.valueOf("1.2.0.0/32");
820 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
821 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
822 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
823 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
824 + assertFalse(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
825 +
826 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
827 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
828 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
829 + assertTrue(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
830 + assertTrue(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
831 + assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
832 +
833 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
834 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.0")));
835 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.2.0.4")));
836 + assertFalse(ipPrefix.contains(IpAddress.valueOf("1.3.0.0")));
837 + assertFalse(ipPrefix.contains(IpAddress.valueOf("0.0.0.0")));
838 + assertTrue(ipPrefix.contains(IpAddress.valueOf("255.255.255.255")));
839 + }
840 +
841 + /**
842 + * Tests IP prefix contains IP address for IPv6.
843 + */
844 + @Test
845 + public void testContainsIpAddressIPv6() {
846 + IpPrefix ipPrefix;
847 +
848 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/120");
849 + assertTrue(ipPrefix.contains(
850 + IpAddress.valueOf("1111:2222:3333:4444::")));
851 + assertTrue(ipPrefix.contains(
852 + IpAddress.valueOf("1111:2222:3333:4444::1")));
853 + assertFalse(ipPrefix.contains(
854 + IpAddress.valueOf("1111:2222:3333:4445::")));
855 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
856 + assertFalse(ipPrefix.contains(
857 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
858 +
859 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444::/128");
860 + assertTrue(ipPrefix.contains(
861 + IpAddress.valueOf("1111:2222:3333:4444::")));
862 + assertFalse(ipPrefix.contains(
863 + IpAddress.valueOf("1111:2222:3333:4444::1")));
864 + assertFalse(ipPrefix.contains(
865 + IpAddress.valueOf("1111:2222:3333:4445::")));
866 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
867 + assertFalse(ipPrefix.contains(
868 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
869 +
870 + ipPrefix = IpPrefix.valueOf("::/0");
871 + assertTrue(ipPrefix.contains(
872 + IpAddress.valueOf("1111:2222:3333:4444::")));
873 + assertTrue(ipPrefix.contains(
874 + IpAddress.valueOf("1111:2222:3333:4444::1")));
875 + assertTrue(ipPrefix.contains(
876 + IpAddress.valueOf("1111:2222:3333:4445::")));
877 + assertTrue(ipPrefix.contains(IpAddress.valueOf("::")));
878 + assertTrue(ipPrefix.contains(
879 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
880 +
881 + ipPrefix =
882 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
883 + assertFalse(ipPrefix.contains(
884 + IpAddress.valueOf("1111:2222:3333:4444::")));
885 + assertFalse(ipPrefix.contains(
886 + IpAddress.valueOf("1111:2222:3333:4444::1")));
887 + assertFalse(ipPrefix.contains(
888 + IpAddress.valueOf("1111:2222:3333:4445::")));
889 + assertFalse(ipPrefix.contains(IpAddress.valueOf("::")));
890 + assertTrue(ipPrefix.contains(
891 + IpAddress.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")));
892 + }
893 +
894 + /**
895 + * Tests equality of {@link IpPrefix} for IPv4.
896 + */
897 + @Test
898 + public void testEqualityIPv4() {
899 + new EqualsTester()
900 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/24"),
901 + IpPrefix.valueOf("1.2.0.0/24"),
902 + IpPrefix.valueOf("1.2.0.4/24"))
903 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/16"),
904 + IpPrefix.valueOf("1.2.0.0/16"))
905 + .addEqualityGroup(IpPrefix.valueOf("1.2.0.0/32"),
906 + IpPrefix.valueOf("1.2.0.0/32"))
907 + .addEqualityGroup(IpPrefix.valueOf("1.3.0.0/24"),
908 + IpPrefix.valueOf("1.3.0.0/24"))
909 + .addEqualityGroup(IpPrefix.valueOf("0.0.0.0/0"),
910 + IpPrefix.valueOf("0.0.0.0/0"))
911 + .addEqualityGroup(IpPrefix.valueOf("255.255.255.255/32"),
912 + IpPrefix.valueOf("255.255.255.255/32"))
913 + .testEquals();
914 + }
915 +
916 + /**
917 + * Tests equality of {@link IpPrefix} for IPv6.
918 + */
919 + @Test
920 + public void testEqualityIPv6() {
921 + new EqualsTester()
922 + .addEqualityGroup(
923 + IpPrefix.valueOf("1111:2222:3333:4444::/120"),
924 + IpPrefix.valueOf("1111:2222:3333:4444::1/120"),
925 + IpPrefix.valueOf("1111:2222:3333:4444::/120"))
926 + .addEqualityGroup(
927 + IpPrefix.valueOf("1111:2222:3333:4444::/64"),
928 + IpPrefix.valueOf("1111:2222:3333:4444::/64"))
929 + .addEqualityGroup(
930 + IpPrefix.valueOf("1111:2222:3333:4444::/128"),
931 + IpPrefix.valueOf("1111:2222:3333:4444::/128"))
932 + .addEqualityGroup(
933 + IpPrefix.valueOf("1111:2222:3333:4445::/64"),
934 + IpPrefix.valueOf("1111:2222:3333:4445::/64"))
935 + .addEqualityGroup(
936 + IpPrefix.valueOf("::/0"),
937 + IpPrefix.valueOf("::/0"))
938 + .addEqualityGroup(
939 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"),
940 + IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"))
941 + .testEquals();
942 + }
943 +
944 + /**
945 + * Tests object string representation for IPv4.
946 + */
119 @Test 947 @Test
120 - public void testContainsIpAddress() { 948 + public void testToStringIPv4() {
121 - IpPrefix slash31 = IpPrefix.valueOf(IpAddress.Version.INET, 949 + IpPrefix ipPrefix;
122 - BYTES1, 31);
123 - IpAddress addr32 = IpAddress.valueOf(IpAddress.Version.INET, BYTES1);
124 950
125 - assertTrue(slash31.contains(addr32)); 951 + ipPrefix = IpPrefix.valueOf("1.2.3.0/24");
952 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
953 +
954 + ipPrefix = IpPrefix.valueOf("1.2.3.4/24");
955 + assertThat(ipPrefix.toString(), is("1.2.3.0/24"));
956 +
957 + ipPrefix = IpPrefix.valueOf("0.0.0.0/0");
958 + assertThat(ipPrefix.toString(), is("0.0.0.0/0"));
959 +
960 + ipPrefix = IpPrefix.valueOf("255.255.255.255/32");
961 + assertThat(ipPrefix.toString(), is("255.255.255.255/32"));
962 + }
963 +
964 + /**
965 + * Tests object string representation for IPv6.
966 + */
967 + @Test
968 + public void testToStringIPv6() {
969 + IpPrefix ipPrefix;
126 970
127 - IpPrefix intf = IpPrefix.valueOf("192.168.10.101/24"); 971 + ipPrefix = IpPrefix.valueOf("1100::/8");
128 - IpAddress addr = IpAddress.valueOf("192.168.10.1"); 972 + assertThat(ipPrefix.toString(), is("1100::/8"));
129 973
130 - assertTrue(intf.contains(addr)); 974 + ipPrefix = IpPrefix.valueOf("1111:2222:3333:4444:5555:6666:7777:8885/8");
975 + assertThat(ipPrefix.toString(), is("1100::/8"));
131 976
132 - IpPrefix intf1 = IpPrefix.valueOf("10.0.0.101/24"); 977 + ipPrefix = IpPrefix.valueOf("::/0");
133 - IpAddress addr1 = IpAddress.valueOf("10.0.0.4"); 978 + assertThat(ipPrefix.toString(), is("::/0"));
134 979
135 - assertTrue(intf1.contains(addr1)); 980 + ipPrefix = IpPrefix.valueOf("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128");
981 + assertThat(ipPrefix.toString(),
982 + is("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128"));
136 } 983 }
137 } 984 }
......