Revert "Checkstyle enforcer"
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / action / Action.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.action;
11
12 import javax.xml.bind.annotation.XmlAccessType;
13 import javax.xml.bind.annotation.XmlAccessorType;
14 import javax.xml.bind.annotation.XmlRootElement;
15 import javax.xml.bind.annotation.XmlSeeAlso;
16 import javax.xml.bind.annotation.XmlTransient;
17
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Represents the generic action to be applied to the matched frame/packet/message
23  */
24 @XmlRootElement
25 @XmlAccessorType(XmlAccessType.NONE)
26 @XmlSeeAlso({Controller.class, Drop.class, Flood.class, FloodAll.class, HwPath.class, Loopback.class, Output.class,
27                          PopVlan.class, PushVlan.class, SetDlDst.class, SetDlSrc.class, SetDlType.class, SetNwDst.class, SetNwSrc.class,
28                          SetNwTos.class, SetTpDst.class, SetTpSrc.class, SetVlanCfi.class, SetVlanId.class, SetVlanPcp.class, SwPath.class})
29 public abstract class Action {
30     private static final Logger logger = LoggerFactory.getLogger(Action.class);
31     private static boolean debug = false; // Enable to find where in the code an invalid assignment is made
32     @XmlTransient
33     protected ActionType type;
34     private transient boolean isValid = true;
35
36     /* Dummy constructor for JAXB */
37     public Action () {
38     }
39
40     /*
41     public Action (ActionType type, Object value) {
42         this.type = type;
43         this.value = value;
44         this.isValid = true;
45     } */
46
47     /**
48      * Checks if the passed value is in the valid range for this action
49      *
50      * @param value
51      * @return boolean
52      */
53     protected void checkValue(int value) {
54         if (type.isValidTarget(value) == false) {
55             isValid = false;
56             throwValueException(value);
57         }
58     }
59
60     /**
61      * Checks if the passed value is in the valid range for the passed action type
62      * This method is used for complex Action types which are
63      *
64      * @param value
65      * @return boolean
66      */
67     protected void checkValue(ActionType type, int value) {
68         if (type.isValidTarget(value) == false) {
69             isValid = false;
70             throwValueException(value);
71         }
72     }
73
74     /**
75      * Throw and handle the invalid value exception
76      *
77      * @param value
78      * @return void
79      */
80     private void throwValueException(int value) {
81         String error = "Invalid field value assignement. For type: "
82                 + type.getId() + " Expected: " + type.getRange() + ", Got: 0x"
83                 + Integer.toHexString(value);
84         try {
85             throw new Exception(error);
86         } catch (Exception e) {
87             logger.error(e.getMessage());
88             if (debug) {
89                 logger.error("",e);
90             }
91         }
92     }
93
94     /**
95      * Returns the type of this action
96      *
97      * @return ActionType
98      */
99     public ActionType getType() {
100         return type;
101     }
102
103     /**
104      * Returns the id of this action
105      *
106      * @return String
107      */
108     public String getId() {
109         return type.getId();
110     }
111
112     /**
113      * Returns whether the Action is valid or not
114      *
115      * @return  boolean
116      */
117     public boolean isValid() {
118         return isValid;
119     }
120
121     @Override
122     public int hashCode() {
123         final int prime = 31;
124         int result = 1;
125         result = prime * result + ((type == null) ? 0 : type.hashCode());
126         return result;
127     }
128
129     @Override
130     public boolean equals(Object obj) {
131         if (this == obj)
132             return true;
133         if (obj == null)
134             return false;
135         if (getClass() != obj.getClass())
136             return false;
137         Action other = (Action) obj;
138         if (type != other.type)
139             return false;
140         return true;
141     }
142
143     @Override
144     public String toString() {
145         return type.toString();
146     }
147
148 }