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.io.Serializable;
13 import javax.xml.bind.annotation.XmlAccessType;
14 import javax.xml.bind.annotation.XmlAccessorType;
15 import javax.xml.bind.annotation.XmlRootElement;
16 import javax.xml.bind.annotation.XmlSeeAlso;
17 import javax.xml.bind.annotation.XmlTransient;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * Represents the generic action to be applied to the matched
24 * frame/packet/message
27 @XmlAccessorType(XmlAccessType.NONE)
28 @XmlSeeAlso({ Controller.class, Drop.class, Flood.class, FloodAll.class, HwPath.class, Loopback.class, Output.class,
29 PopVlan.class, PushVlan.class, SetDlDst.class, SetDlSrc.class, SetDlType.class, SetNwDst.class, SetNwSrc.class,
30 SetNwTos.class, SetTpDst.class, SetTpSrc.class, SetVlanCfi.class, SetVlanId.class, SetVlanPcp.class,
32 public abstract class Action implements Serializable {
33 private static final long serialVersionUID = 1L;
34 private static final Logger logger = LoggerFactory.getLogger(Action.class);
35 private static boolean debug = false; // Enable to find where in the code an
36 // invalid assignment is made
38 protected ActionType type;
39 private transient boolean isValid = true;
41 /* Dummy constructor for JAXB */
46 * public Action (ActionType type, Object value) { this.type = type;
47 * this.value = value; this.isValid = true; }
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 * Returns the type of this action
101 public ActionType getType() {
106 * Returns the id of this action
110 public String getId() {
115 * Returns whether the Action is valid or not
119 public boolean isValid() {
124 public int hashCode() {
125 final int prime = 31;
127 result = prime * result + ((type == null) ? 0 : type.hashCode());
132 public boolean equals(Object obj) {
139 if (getClass() != obj.getClass()) {
142 Action other = (Action) obj;
143 if (type != other.type) {
150 public String toString() {
151 return type.toString();