Remove commented out code to fix sonar warnings
[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 @XmlSeeAlso({ Controller.class, Drop.class, Flood.class, FloodAll.class, HwPath.class, Loopback.class, Output.class,
24     PopVlan.class, PushVlan.class, SetDlDst.class, SetDlSrc.class, SetDlType.class, SetNwDst.class, SetNwSrc.class,
25     SetNwTos.class, SetTpDst.class, SetTpSrc.class, SetVlanCfi.class, SetVlanId.class, SetVlanPcp.class,
26     SwPath.class })
27 public abstract class Action implements Serializable {
28     private static final long serialVersionUID = 1L;
29     private static final Logger logger = LoggerFactory.getLogger(Action.class);
30     private static boolean debug = false; // Enable to find where in the code an
31     // invalid assignment is made
32     @XmlElement
33     protected ActionType type;
34     private transient boolean isValid = true;
35
36     /* Dummy constructor for JAXB */
37     public Action() {
38     }
39
40     /**
41      * Checks if the passed value is in the valid range for this action
42      *
43      * @param value
44      * @return boolean
45      */
46     protected void checkValue(int value) {
47         if (type.isValidTarget(value) == false) {
48             isValid = false;
49             throwValueException(value);
50         }
51     }
52
53     /**
54      * Checks if the passed value is in the valid range for the passed action
55      * type This method is used for complex Action types which are
56      *
57      * @param value
58      * @return boolean
59      */
60     protected void checkValue(ActionType type, int value) {
61         if (type.isValidTarget(value) == false) {
62             isValid = false;
63             throwValueException(value);
64         }
65     }
66
67     /**
68      * Throw and handle the invalid value exception
69      *
70      * @param value
71      * @return void
72      */
73     private void throwValueException(int value) {
74         String error = "Invalid field value assignement. For type: " + type.getId() + " Expected: " + type.getRange()
75                 + ", Got: 0x" + Integer.toHexString(value);
76         try {
77             throw new Exception(error);
78         } catch (Exception e) {
79             logger.error(e.getMessage());
80             if (debug) {
81                 logger.error("", e);
82             }
83         }
84     }
85
86     /**
87      * Returns the type of this action
88      *
89      * @return ActionType
90      */
91     public ActionType getType() {
92         return type;
93     }
94
95     /**
96      * Returns the id of this action
97      *
98      * @return String
99      */
100     public String getId() {
101         return type.getId();
102     }
103
104     /**
105      * Returns whether the Action is valid or not
106      *
107      * @return boolean
108      */
109     public boolean isValid() {
110         return isValid;
111     }
112
113     @Override
114     public int hashCode() {
115         final int prime = 31;
116         int result = 1;
117         result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
118         return result;
119     }
120
121     @Override
122     public boolean equals(Object obj) {
123         if (this == obj) {
124             return true;
125         }
126         if (obj == null) {
127             return false;
128         }
129         if (getClass() != obj.getClass()) {
130             return false;
131         }
132         Action other = (Action) obj;
133         if (type != other.type) {
134             return false;
135         }
136         return true;
137     }
138
139     @Override
140     public String toString() {
141         return type.toString();
142     }
143
144 }