a1bf55ddaf54f519b22504eb0a18c2068216c03d
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / action / Action.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.action;
10
11 import java.io.Serializable;
12
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;
19
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Represents the generic action to be applied to the matched
25  * frame/packet/message
26  */
27 @XmlRootElement
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,
32     SwPath.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
38     @XmlElement
39     protected ActionType type;
40     private transient boolean isValid = true;
41
42     /* Dummy constructor for JAXB */
43     public Action() {
44     }
45
46     /*
47      * public Action (ActionType type, Object value) { this.type = type;
48      * this.value = value; this.isValid = true; }
49      */
50
51     /**
52      * Checks if the passed value is in the valid range for this action
53      *
54      * @param value
55      * @return boolean
56      */
57     protected void checkValue(int value) {
58         if (type.isValidTarget(value) == false) {
59             isValid = false;
60             throwValueException(value);
61         }
62     }
63
64     /**
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
67      *
68      * @param value
69      * @return boolean
70      */
71     protected void checkValue(ActionType type, int value) {
72         if (type.isValidTarget(value) == false) {
73             isValid = false;
74             throwValueException(value);
75         }
76     }
77
78     /**
79      * Throw and handle the invalid value exception
80      *
81      * @param value
82      * @return void
83      */
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);
87         try {
88             throw new Exception(error);
89         } catch (Exception e) {
90             logger.error(e.getMessage());
91             if (debug) {
92                 logger.error("", e);
93             }
94         }
95     }
96
97     /**
98      * Returns the type of this action
99      *
100      * @return ActionType
101      */
102     public ActionType getType() {
103         return type;
104     }
105
106     /**
107      * Returns the id of this action
108      *
109      * @return String
110      */
111     public String getId() {
112         return type.getId();
113     }
114
115     /**
116      * Returns whether the Action is valid or not
117      *
118      * @return boolean
119      */
120     public boolean isValid() {
121         return isValid;
122     }
123
124     @Override
125     public int hashCode() {
126         final int prime = 31;
127         int result = 1;
128         result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
129         return result;
130     }
131
132     @Override
133     public boolean equals(Object obj) {
134         if (this == obj) {
135             return true;
136         }
137         if (obj == null) {
138             return false;
139         }
140         if (getClass() != obj.getClass()) {
141             return false;
142         }
143         Action other = (Action) obj;
144         if (type != other.type) {
145             return false;
146         }
147         return true;
148     }
149
150     @Override
151     public String toString() {
152         return type.toString();
153     }
154
155 }