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.XmlElement;
16 import javax.xml.bind.annotation.XmlRootElement;
17 import javax.xml.bind.annotation.XmlSeeAlso;
18 import javax.xml.bind.annotation.XmlTransient;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Represents the generic action to be applied to the matched
25 * frame/packet/message
28 @XmlAccessorType(XmlAccessType.NONE)
29 @XmlSeeAlso({ Controller.class, Drop.class, Flood.class, FloodAll.class, HwPath.class, Loopback.class, Output.class,
30 PopVlan.class, PushVlan.class, SetDlDst.class, SetDlSrc.class, SetDlType.class, SetNwDst.class, SetNwSrc.class,
31 SetNwTos.class, SetTpDst.class, SetTpSrc.class, SetVlanCfi.class, SetVlanId.class, SetVlanPcp.class,
33 public abstract class Action implements Serializable {
34 private static final long serialVersionUID = 1L;
35 private static final Logger logger = LoggerFactory.getLogger(Action.class);
36 private static boolean debug = false; // Enable to find where in the code an
37 // invalid assignment is made
39 protected ActionType type;
40 private transient boolean isValid = true;
42 /* Dummy constructor for JAXB */
47 * public Action (ActionType type, Object value) { this.type = type;
48 * this.value = value; this.isValid = true; }
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 * Returns the type of this action
102 public ActionType getType() {
107 * Returns the id of this action
111 public String getId() {
116 * Returns whether the Action is valid or not
120 public boolean isValid() {
125 public int hashCode() {
126 final int prime = 31;
128 result = prime * result + ((type == null) ? 0 : type.hashCode());
133 public boolean equals(Object obj) {
140 if (getClass() != obj.getClass()) {
143 Action other = (Action) obj;
144 if (type != other.type) {
151 public String toString() {
152 return type.toString();