package org.opendaylight.controller.sal.action;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.opendaylight.controller.sal.core.Property;
+
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlElement
protected ActionType type;
private transient boolean isValid = true;
+ private ConcurrentMap<String, Property> props;
/* Dummy constructor for JAXB */
public Action() {
}
}
+ /**
+ * Gets the list of metadata currently registered with this match
+ *
+ * @return List of metadata currently registered
+ */
+ public List <Property> getMetadatas() {
+ if (this.props != null) {
+ // Return all the values in the map
+ Collection res = this.props.values();
+ if (res == null) {
+ return Collections.emptyList();
+ }
+ return new ArrayList<Property>(res);
+ }
+ return Collections.emptyList();
+ }
+
+ /**
+ * Gets the metadata registered with a name if present
+ *
+ * @param name the name of the property to be extracted
+ *
+ * @return List of metadata currently registered
+ */
+ public Property getMetadata(String name) {
+ if (name == null) {
+ return null;
+ }
+ if (this.props != null) {
+ // Return the Property associated to the name
+ return this.props.get(name);
+ }
+ return null;
+ }
+
+ /**
+ * Sets the metadata associated to a name. If the name or prop is NULL,
+ * an exception NullPointerException will be raised.
+ *
+ * @param name the name of the property to be set
+ * @param prop, property to be set
+ */
+ public void setMetadata(String name, Property prop) {
+ if (this.props == null) {
+ props = new ConcurrentHashMap<String, Property>();
+ }
+
+ if (this.props != null) {
+ this.props.put(name, prop);
+ }
+ }
+
+ /**
+ * Remove the metadata associated to a name. If the name is NULL,
+ * nothing will be removed.
+ *
+ * @param name the name of the property to be set
+ * @param prop, property to be set
+ *
+ * @return List of metadata currently registered
+ */
+ public void removeMetadata(String name) {
+ if (this.props == null) {
+ return;
+ }
+
+ if (this.props != null) {
+ this.props.remove(name);
+ }
+ // It's intentional to keep the this.props still allocated
+ // till the parent data structure will be alive, so to avoid
+ // unnecessary allocation/deallocation, even if it's holding
+ // nothing
+ }
+
/**
* Returns the type of this action
*
-
/*
* Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
*
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ConcurrentHashMap;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
+import org.opendaylight.controller.sal.core.Property;
import org.opendaylight.controller.sal.utils.EtherTypes;
import org.opendaylight.controller.sal.utils.IPProtocols;
import org.opendaylight.controller.sal.utils.NetUtils;
reversableMatches = Collections.unmodifiableMap(map);
}
private Map<MatchType, MatchField> fields;
- private int matches; // concise way to tell which fields the match is set for (may remove if not needed)
+ private int matches; // concise way to tell which fields the match
+ // is set for (may remove if not needed)
+ private ConcurrentMap<String, Property> props;
public Match() {
fields = new HashMap<MatchType, MatchField>();
matches = match.matches;
}
+ /**
+ * Gets the list of metadata currently registered with this match
+ *
+ * @return List of metadata currently registered
+ */
+ public List <Property> getMetadatas() {
+ if (this.props != null) {
+ // Return all the values in the map
+ Collection res = this.props.values();
+ if (res == null) {
+ return Collections.emptyList();
+ }
+ return new ArrayList<Property>(res);
+ }
+ return Collections.emptyList();
+ }
+
+ /**
+ * Gets the metadata registered with a name if present
+ *
+ * @param name the name of the property to be extracted
+ *
+ * @return List of metadata currently registered
+ */
+ public Property getMetadata(String name) {
+ if (name == null) {
+ return null;
+ }
+ if (this.props != null) {
+ // Return the Property associated to the name
+ return this.props.get(name);
+ }
+ return null;
+ }
+
+ /**
+ * Sets the metadata associated to a name. If the name or prop is NULL,
+ * an exception NullPointerException will be raised.
+ *
+ * @param name the name of the property to be set
+ * @param prop, property to be set
+ */
+ public void setMetadata(String name, Property prop) {
+ if (this.props == null) {
+ props = new ConcurrentHashMap<String, Property>();
+ }
+
+ if (this.props != null) {
+ this.props.put(name, prop);
+ }
+ }
+
+ /**
+ * Remove the metadata associated to a name. If the name is NULL,
+ * nothing will be removed.
+ *
+ * @param name the name of the property to be set
+ * @param prop, property to be set
+ *
+ * @return List of metadata currently registered
+ */
+ public void removeMetadata(String name) {
+ if (this.props == null) {
+ return;
+ }
+
+ if (this.props != null) {
+ this.props.remove(name);
+ }
+ // It's intentional to keep the this.props still allocated
+ // till the parent data structure will be alive, so to avoid
+ // unnecessary allocation/deallocation, even if it's holding
+ // nothing
+ }
+
/**
* Generic setter for frame/packet/message's header fields against which to match
* Note: For MAC addresses, please pass the cloned value to this function
import org.junit.Assert;
import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.core.Property;
+import org.opendaylight.controller.sal.core.Tables;
+import org.opendaylight.controller.sal.core.Tier;
import org.opendaylight.controller.sal.utils.EtherTypes;
import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
import org.slf4j.Logger;
.createNodeConnector((short) 5, node))));
Assert.assertFalse(actions.contains(new Controller()));
}
+
+ @Test
+ public void testMetadata() {
+ Property tier1 = new Tier(1);
+ Property tier2 = new Tier(2);
+ Property table1 = new Tables((byte)0x7f);
+ Action a1 = new PopVlan();
+ List<Property> resprops = null;
+ resprops = a1.getMetadatas();
+ // This should be an empty list
+ Assert.assertTrue(resprops.isEmpty());
+ a1.setMetadata("tier1", tier1);
+ a1.setMetadata("tier2", tier2);
+ a1.setMetadata("table1", table1);
+ resprops = a1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 3);
+ // Check if the elements are in it
+ Assert.assertTrue(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(a1.getMetadata("tier1").equals(tier1));
+ Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
+ Assert.assertTrue(a1.getMetadata("table1").equals(table1));
+ // Now remove an element and make sure the remaining are
+ // correct
+ a1.removeMetadata("tier1");
+
+ resprops = a1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 2);
+ // Check if the elements are in it
+ Assert.assertFalse(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(a1.getMetadata("table1").equals(table1));
+ Assert.assertTrue(a1.getMetadata("tier2").equals(tier2));
+ Assert.assertNull(a1.getMetadata("tier1"));
+
+ // Check for an element never existed
+ Assert.assertNull(a1.getMetadata("table100"));
+
+ // Remove them all
+ a1.removeMetadata("tier2");
+ a1.removeMetadata("table1");
+
+ // Remove also a non-existent one
+ a1.removeMetadata("table100");
+
+ resprops = a1.getMetadatas();
+ // Check there are no elements left
+ Assert.assertTrue(resprops.size() == 0);
+
+ // Now check for exception on setting null values
+ try {
+ a1.setMetadata("foo", null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key
+ try {
+ a1.setMetadata(null, table1);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key and null value
+ try {
+ a1.setMetadata(null, null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+ }
}
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
+import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.opendaylight.controller.sal.core.Node;
import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.core.Property;
+import org.opendaylight.controller.sal.core.Tables;
+import org.opendaylight.controller.sal.core.Tier;
import org.opendaylight.controller.sal.utils.EtherTypes;
import org.opendaylight.controller.sal.utils.IPProtocols;
import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
// No intersection with null match, empty set
Assert.assertNull(m6.getIntersection(null));
}
+
+ @Test
+ public void testMetadata() {
+ Property tier1 = new Tier(1);
+ Property tier2 = new Tier(2);
+ Property table1 = new Tables((byte)0x7f);
+ Match m1 = new Match();
+ List<Property> resprops = null;
+ resprops = m1.getMetadatas();
+ // This should be null
+ Assert.assertTrue(resprops.isEmpty());
+ m1.setMetadata("tier1", tier1);
+ m1.setMetadata("tier2", tier2);
+ m1.setMetadata("table1", table1);
+ resprops = m1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 3);
+ // Check if the elements are in it
+ Assert.assertTrue(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(m1.getMetadata("tier1").equals(tier1));
+ Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
+ Assert.assertTrue(m1.getMetadata("table1").equals(table1));
+ // Now remove an element and make sure the remaining are
+ // correct
+ m1.removeMetadata("tier1");
+
+ resprops = m1.getMetadatas();
+ // Check for the number of elements in it
+ Assert.assertTrue(resprops.size() == 2);
+ // Check if the elements are in it
+ Assert.assertFalse(resprops.contains(tier1));
+ Assert.assertTrue(resprops.contains(tier2));
+ Assert.assertTrue(resprops.contains(table1));
+ // Check for single elements retrieve
+ Assert.assertTrue(m1.getMetadata("table1").equals(table1));
+ Assert.assertTrue(m1.getMetadata("tier2").equals(tier2));
+ Assert.assertNull(m1.getMetadata("tier1"));
+
+ // Check for an element never existed
+ Assert.assertNull(m1.getMetadata("table100"));
+
+ // Remove them all
+ m1.removeMetadata("tier2");
+ m1.removeMetadata("table1");
+
+ // Remove also a non-existent one
+ m1.removeMetadata("table100");
+
+ resprops = m1.getMetadatas();
+ // Check there are no elements left
+ Assert.assertTrue(resprops.size() == 0);
+
+ // Now check for exception on setting null values
+ try {
+ m1.setMetadata("foo", null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key
+ try {
+ m1.setMetadata(null, table1);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+
+ // Now check on using null key and null value
+ try {
+ m1.setMetadata(null, null);
+ // The line below should never be reached
+ Assert.assertTrue(false);
+ } catch (NullPointerException nue) {
+ // NPE should be raised for null value
+ Assert.assertTrue(true);
+ }
+ }
}