2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.action;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.ConcurrentHashMap;
18 import org.opendaylight.controller.sal.core.Property;
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlRootElement;
24 import java.io.Serializable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * Represents the generic action to be applied to the matched
32 * frame/packet/message
35 @XmlAccessorType(XmlAccessType.NONE)
36 public abstract class Action implements Serializable {
37 private static final long serialVersionUID = 1L;
38 private static final Logger logger = LoggerFactory.getLogger(Action.class);
39 private static boolean debug = false; // Enable to find where in the code an
40 // invalid assignment is made
42 protected ActionType type;
43 private transient boolean isValid = true;
44 private ConcurrentMap<String, Property> props;
46 /* Dummy constructor for JAXB */
51 * Checks if the passed value is in the valid range for this action
56 protected void checkValue(int value) {
57 if (type.isValidTarget(value) == false) {
59 throwValueException(value);
64 * Checks if the passed value is in the valid range for the passed action
65 * type This method is used for complex Action types which are
70 protected void checkValue(ActionType type, int value) {
71 if (type.isValidTarget(value) == false) {
73 throwValueException(value);
78 * Throw and handle the invalid value exception
83 private void throwValueException(int value) {
84 String error = "Invalid field value assignement. For type: " + type.getId() + " Expected: " + type.getRange()
85 + ", Got: 0x" + Integer.toHexString(value);
87 throw new Exception(error);
88 } catch (Exception e) {
89 logger.error(e.getMessage());
97 * Gets the list of metadata currently registered with this match
99 * @return List of metadata currently registered
101 public List <Property> getMetadatas() {
102 if (this.props != null) {
103 // Return all the values in the map
104 Collection res = this.props.values();
106 return Collections.emptyList();
108 return new ArrayList<Property>(res);
110 return Collections.emptyList();
114 * Gets the metadata registered with a name if present
116 * @param name the name of the property to be extracted
118 * @return List of metadata currently registered
120 public Property getMetadata(String name) {
124 if (this.props != null) {
125 // Return the Property associated to the name
126 return this.props.get(name);
132 * Sets the metadata associated to a name. If the name or prop is NULL,
133 * an exception NullPointerException will be raised.
135 * @param name the name of the property to be set
136 * @param prop, property to be set
138 public void setMetadata(String name, Property prop) {
139 if (this.props == null) {
140 props = new ConcurrentHashMap<String, Property>();
143 if (this.props != null) {
144 this.props.put(name, prop);
149 * Remove the metadata associated to a name. If the name is NULL,
150 * nothing will be removed.
152 * @param name the name of the property to be set
153 * @param prop, property to be set
155 * @return List of metadata currently registered
157 public void removeMetadata(String name) {
158 if (this.props == null) {
162 if (this.props != null) {
163 this.props.remove(name);
165 // It's intentional to keep the this.props still allocated
166 // till the parent data structure will be alive, so to avoid
167 // unnecessary allocation/deallocation, even if it's holding
172 * Returns the type of this action
176 public ActionType getType() {
181 * Returns the id of this action
185 public String getId() {
190 * Returns whether the Action is valid or not
194 public boolean isValid() {
199 public int hashCode() {
200 final int prime = 31;
202 result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
207 public boolean equals(Object obj) {
214 if (getClass() != obj.getClass()) {
217 Action other = (Action) obj;
218 if (type != other.type) {
225 public String toString() {
226 return type.toString();