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