b4f9cb58da7e800a115033deabb2662ee90bea40
[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.apache.commons.lang3.builder.EqualsBuilder;
19 import org.apache.commons.lang3.builder.HashCodeBuilder;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Represents the generic action to be applied to the matched 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, SwPath.class})
31 public abstract class Action {
32     private static final Logger logger = LoggerFactory.getLogger(Action.class);
33     private static boolean debug = false; // Enable to find where in the code an invalid assignment is made
34     @XmlTransient
35     protected ActionType type;
36     private transient boolean isValid = true;
37
38     /* Dummy constructor for JAXB */
39     public Action () {
40     }
41
42     /*
43     public Action (ActionType type, Object value) {
44         this.type = type;
45         this.value = value;
46         this.isValid = true;
47     } */
48
49     /**
50      * Checks if the passed value is in the valid range for this action
51      *
52      * @param value
53      * @return boolean
54      */
55     protected void checkValue(int value) {
56         if (type.isValidTarget(value) == false) {
57             isValid = false;
58             throwValueException(value);
59         }
60     }
61
62     /**
63      * Checks if the passed value is in the valid range for the passed action type
64      * This method is used for complex Action types which are
65      *
66      * @param value
67      * @return boolean
68      */
69     protected void checkValue(ActionType type, int value) {
70         if (type.isValidTarget(value) == false) {
71             isValid = false;
72             throwValueException(value);
73         }
74     }
75
76     /**
77      * Throw and handle the invalid value exception
78      *
79      * @param value
80      * @return void
81      */
82     private void throwValueException(int value) {
83         String error = "Invalid field value assignement. For type: "
84                 + type.getId() + " Expected: " + type.getRange() + ", Got: 0x"
85                 + Integer.toHexString(value);
86         try {
87             throw new Exception(error);
88         } catch (Exception e) {
89             logger.error(e.getMessage());
90             if (debug) {
91                 e.printStackTrace();
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         return HashCodeBuilder.reflectionHashCode(this);
126     }
127
128     @Override
129     public boolean equals(Object obj) {
130         return EqualsBuilder.reflectionEquals(this, obj);
131     }
132
133     @Override
134     public String toString() {
135         return type.toString();
136     }
137
138 }