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)
37 public abstract class Action implements Serializable {
38 private static final long serialVersionUID = 1L;
39 private static final Logger logger = LoggerFactory.getLogger(Action.class);
40 private static boolean debug = false; // Enable to find where in the code an
41 // invalid assignment is made
43 protected ActionType type;
44 private transient boolean isValid = true;
45 private ConcurrentMap<String, Property> props;
47 /* Dummy constructor for JAXB */
52 * Checks if the passed value is in the valid range for this action
57 protected void checkValue(int value) {
58 if (type.isValidTarget(value) == false) {
60 throwValueException(value);
65 * Checks if the passed value is in the valid range for the passed action
66 * type This method is used for complex Action types which are
71 protected void checkValue(ActionType type, int value) {
72 if (type.isValidTarget(value) == false) {
74 throwValueException(value);
79 * Throw and handle the invalid value exception
84 private void throwValueException(int value) {
85 String error = "Invalid field value assignement. For type: " + type.getId() + " Expected: " + type.getRange()
86 + ", Got: 0x" + Integer.toHexString(value);
88 throw new Exception(error);
89 } catch (Exception e) {
90 logger.error(e.getMessage());
98 * Gets the list of metadata currently registered with this match
100 * @return List of metadata currently registered
102 public List <Property> getMetadatas() {
103 if (this.props != null) {
104 // Return all the values in the map
105 Collection res = this.props.values();
107 return Collections.emptyList();
109 return new ArrayList<Property>(res);
111 return Collections.emptyList();
115 * Gets the metadata registered with a name if present
117 * @param name the name of the property to be extracted
119 * @return List of metadata currently registered
121 public Property getMetadata(String name) {
125 if (this.props != null) {
126 // Return the Property associated to the name
127 return this.props.get(name);
133 * Sets the metadata associated to a name. If the name or prop is NULL,
134 * an exception NullPointerException will be raised.
136 * @param name the name of the property to be set
137 * @param prop, property to be set
139 public void setMetadata(String name, Property prop) {
140 if (this.props == null) {
141 props = new ConcurrentHashMap<String, Property>();
144 if (this.props != null) {
145 this.props.put(name, prop);
150 * Remove the metadata associated to a name. If the name is NULL,
151 * nothing will be removed.
153 * @param name the name of the property to be set
154 * @param prop, property to be set
156 * @return List of metadata currently registered
158 public void removeMetadata(String name) {
159 if (this.props == null) {
163 if (this.props != null) {
164 this.props.remove(name);
166 // It's intentional to keep the this.props still allocated
167 // till the parent data structure will be alive, so to avoid
168 // unnecessary allocation/deallocation, even if it's holding
173 * Returns the type of this action
177 public ActionType getType() {
182 * Returns the id of this action
186 public String getId() {
191 * Returns whether the Action is valid or not
195 public boolean isValid() {
200 public int hashCode() {
201 final int prime = 31;
203 result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
208 public boolean equals(Object obj) {
215 if (getClass() != obj.getClass()) {
218 Action other = (Action) obj;
219 if (type != other.type) {
226 public String toString() {
227 return type.toString();