package org.opendaylight.openflowplugin.api;
import java.math.BigInteger;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
/**
* OFP related constants
public static final Long OFPG_ALL = 0xfffffffcL;
/** Refers to all queues configured at the specified port. */
public static final Long OFPQ_ALL = ANY;
+ /** default cookie */
public static final BigInteger DEFAULT_COOKIE = BigInteger.ZERO;
public static final BigInteger DEFAULT_COOKIE_MASK = BigInteger.ZERO;
+ public static final FlowCookie DEFAULT_FLOW_COOKIE = new FlowCookie(DEFAULT_COOKIE);
/** indicates that no buffering should be applied and the whole packet is to be
* sent to the controller. */
public static final Long OFP_NO_BUFFER = 0xffffffffL;
package org.opendaylight.openflowplugin.impl.registry.flow;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
import com.google.common.primitives.Longs;
import java.math.BigInteger;
import java.util.Objects;
+import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowHash;
import org.opendaylight.openflowplugin.impl.util.HashUtil;
public FlowHashDto(final long flowHash, final Flow flow) {
this.flowHash = flowHash;
this.intHashCode = Longs.hashCode(flowHash);
- tableId = flow.getTableId();
- priority = flow.getPriority();
- cookie = flow.getCookie().getValue();
+ tableId = Preconditions.checkNotNull(flow.getTableId(), "flow tableId must not be null");
+ priority = Preconditions.checkNotNull(flow.getPriority(), "flow priority must not be null");
+ cookie = MoreObjects.firstNonNull(flow.getCookie(), OFConstants.DEFAULT_FLOW_COOKIE).getValue();
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
-import junit.framework.Assert;
+import org.junit.Assert;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
}
@Test
- @Ignore
public void testGetHash2() throws Exception {
MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
.setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
Assert.assertNotSame(flow1Hash, flow2Hash);
}
+ @Test
+ public void testGetHashNPE() throws Exception {
+ MatchBuilder match1Builder = new MatchBuilder().setLayer3Match(new Ipv4MatchBuilder()
+ .setIpv4Destination(new Ipv4Prefix("10.0.1.157/32")).build());
+ FlowBuilder flow1Builder = new FlowBuilder()
+ .setCookie(new FlowCookie(BigInteger.valueOf(483)))
+ .setMatch(match1Builder.build())
+ .setPriority(2)
+ .setTableId((short) 0);
+
+ FlowBuilder fb1 = new FlowBuilder(flow1Builder.build());
+ fb1.setTableId(null);
+ try {
+ FlowHashFactory.create(fb1.build(), deviceContext);
+ Assert.fail("hash creation should have failed because of NPE");
+ } catch (Exception e) {
+ // expected
+ Assert.assertEquals("flow tableId must not be null", e.getMessage());
+ }
+
+ FlowBuilder fb2 = new FlowBuilder(flow1Builder.build());
+ fb2.setPriority(null);
+ try {
+ FlowHashFactory.create(fb2.build(), deviceContext);
+ Assert.fail("hash creation should have failed because of NPE");
+ } catch (Exception e) {
+ // expected
+ Assert.assertEquals("flow priority must not be null", e.getMessage());
+ }
+
+ FlowBuilder fb3 = new FlowBuilder(flow1Builder.build());
+ fb3.setCookie(null);
+ FlowHash flowHash = FlowHashFactory.create(fb3.build(), deviceContext);
+ Assert.assertNotNull(flowHash.getCookie());
+ Assert.assertEquals(OFConstants.DEFAULT_COOKIE, flowHash.getCookie());
+ }
+
@Test
public void testGetHash() throws Exception {
FlowsStatisticsUpdate flowStats = FLOWS_STATISTICS_UPDATE_BUILDER.build();