6076293fbc5bf604b7300c136977ac58bf633d62
[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 org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import javax.xml.bind.annotation.*;
15 import java.io.Serializable;
16
17 /**
18  * Represents the generic action to be applied to the matched
19  * frame/packet/message
20  */
21 @XmlRootElement
22 @XmlAccessorType(XmlAccessType.NONE)
23 public abstract class Action implements Serializable {
24     private static final long serialVersionUID = 1L;
25     private static final Logger logger = LoggerFactory.getLogger(Action.class);
26     private static boolean debug = false; // Enable to find where in the code an
27     // invalid assignment is made
28     @XmlElement
29     protected ActionType type;
30     private transient boolean isValid = true;
31
32     /* Dummy constructor for JAXB */
33     public Action() {
34     }
35
36     /**
37      * Checks if the passed value is in the valid range for this action
38      *
39      * @param value
40      * @return boolean
41      */
42     protected void checkValue(int value) {
43         if (type.isValidTarget(value) == false) {
44             isValid = false;
45             throwValueException(value);
46         }
47     }
48
49     /**
50      * Checks if the passed value is in the valid range for the passed action
51      * type This method is used for complex Action types which are
52      *
53      * @param value
54      * @return boolean
55      */
56     protected void checkValue(ActionType type, int value) {
57         if (type.isValidTarget(value) == false) {
58             isValid = false;
59             throwValueException(value);
60         }
61     }
62
63     /**
64      * Throw and handle the invalid value exception
65      *
66      * @param value
67      * @return void
68      */
69     private void throwValueException(int value) {
70         String error = "Invalid field value assignement. For type: " + type.getId() + " Expected: " + type.getRange()
71                 + ", Got: 0x" + Integer.toHexString(value);
72         try {
73             throw new Exception(error);
74         } catch (Exception e) {
75             logger.error(e.getMessage());
76             if (debug) {
77                 logger.error("", e);
78             }
79         }
80     }
81
82     /**
83      * Returns the type of this action
84      *
85      * @return ActionType
86      */
87     public ActionType getType() {
88         return type;
89     }
90
91     /**
92      * Returns the id of this action
93      *
94      * @return String
95      */
96     public String getId() {
97         return type.getId();
98     }
99
100     /**
101      * Returns whether the Action is valid or not
102      *
103      * @return boolean
104      */
105     public boolean isValid() {
106         return isValid;
107     }
108
109     @Override
110     public int hashCode() {
111         final int prime = 31;
112         int result = 1;
113         result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
114         return result;
115     }
116
117     @Override
118     public boolean equals(Object obj) {
119         if (this == obj) {
120             return true;
121         }
122         if (obj == null) {
123             return false;
124         }
125         if (getClass() != obj.getClass()) {
126             return false;
127         }
128         Action other = (Action) obj;
129         if (type != other.type) {
130             return false;
131         }
132         return true;
133     }
134
135     @Override
136     public String toString() {
137         return type.toString();
138     }
139
140 }