Mark AD-SAL interfaces as deprecated
[controller.git] / opendaylight / adsal / 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.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.concurrent.ConcurrentMap;
16 import java.util.concurrent.ConcurrentHashMap;
17
18 import org.opendaylight.controller.sal.core.Property;
19
20 import javax.xml.bind.annotation.XmlAccessType;
21 import javax.xml.bind.annotation.XmlAccessorType;
22 import javax.xml.bind.annotation.XmlElement;
23 import javax.xml.bind.annotation.XmlRootElement;
24 import java.io.Serializable;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 /**
31  * Represents the generic action to be applied to the matched
32  * frame/packet/message
33  */
34 @XmlRootElement
35 @XmlAccessorType(XmlAccessType.NONE)
36 @Deprecated
37 public abstract class Action implements Serializable {
38     private static final long serialVersionUID = 1L;
39     private static final Logger logger = LoggerFactory.getLogger(Action.class);
40     private static boolean debug = false; // Enable to find where in the code an
41     // invalid assignment is made
42     @XmlElement
43     protected ActionType type;
44     private transient boolean isValid = true;
45     private ConcurrentMap<String, Property> props;
46
47     /* Dummy constructor for JAXB */
48     public Action() {
49     }
50
51     /**
52      * Checks if the passed value is in the valid range for this action
53      *
54      * @param value
55      * @return boolean
56      */
57     protected void checkValue(int value) {
58         if (type.isValidTarget(value) == false) {
59             isValid = false;
60             throwValueException(value);
61         }
62     }
63
64     /**
65      * Checks if the passed value is in the valid range for the passed action
66      * type This method is used for complex Action types which are
67      *
68      * @param value
69      * @return boolean
70      */
71     protected void checkValue(ActionType type, int value) {
72         if (type.isValidTarget(value) == false) {
73             isValid = false;
74             throwValueException(value);
75         }
76     }
77
78     /**
79      * Throw and handle the invalid value exception
80      *
81      * @param value
82      * @return void
83      */
84     private void throwValueException(int value) {
85         String error = "Invalid field value assignement. For type: " + type.getId() + " Expected: " + type.getRange()
86                 + ", Got: 0x" + Integer.toHexString(value);
87         try {
88             throw new Exception(error);
89         } catch (Exception e) {
90             logger.error(e.getMessage());
91             if (debug) {
92                 logger.error("", e);
93             }
94         }
95     }
96
97     /**
98      * Gets the list of metadata currently registered with this match
99      *
100      * @return List of metadata currently registered
101      */
102     public List <Property> getMetadatas() {
103         if (this.props != null) {
104             // Return all the values in the map
105             Collection res = this.props.values();
106             if (res == null) {
107                 return Collections.emptyList();
108             }
109             return new ArrayList<Property>(res);
110         }
111         return Collections.emptyList();
112     }
113
114     /**
115      * Gets the metadata registered with a name if present
116      *
117      * @param name the name of the property to be extracted
118      *
119      * @return List of metadata currently registered
120      */
121     public Property getMetadata(String name) {
122         if (name == null) {
123             return null;
124         }
125         if (this.props != null) {
126             // Return the Property associated to the name
127             return this.props.get(name);
128         }
129         return null;
130     }
131
132     /**
133      * Sets the metadata associated to a name. If the name or prop is NULL,
134      * an exception NullPointerException will be raised.
135      *
136      * @param name the name of the property to be set
137      * @param prop, property to be set
138      */
139     public void setMetadata(String name, Property prop) {
140         if (this.props == null) {
141             props = new ConcurrentHashMap<String, Property>();
142         }
143
144         if (this.props != null) {
145             this.props.put(name, prop);
146         }
147     }
148
149     /**
150      * Remove the metadata associated to a name. If the name is NULL,
151      * nothing will be removed.
152      *
153      * @param name the name of the property to be set
154      * @param prop, property to be set
155      *
156      * @return List of metadata currently registered
157      */
158     public void removeMetadata(String name) {
159         if (this.props == null) {
160             return;
161         }
162
163         if (this.props != null) {
164             this.props.remove(name);
165         }
166         // It's intentional to keep the this.props still allocated
167         // till the parent data structure will be alive, so to avoid
168         // unnecessary allocation/deallocation, even if it's holding
169         // nothing
170     }
171
172     /**
173      * Returns the type of this action
174      *
175      * @return ActionType
176      */
177     public ActionType getType() {
178         return type;
179     }
180
181     /**
182      * Returns the id of this action
183      *
184      * @return String
185      */
186     public String getId() {
187         return type.getId();
188     }
189
190     /**
191      * Returns whether the Action is valid or not
192      *
193      * @return boolean
194      */
195     public boolean isValid() {
196         return isValid;
197     }
198
199     @Override
200     public int hashCode() {
201         final int prime = 31;
202         int result = 1;
203         result = prime * result + ((type == null) ? 0 : type.calculateConsistentHashCode());
204         return result;
205     }
206
207     @Override
208     public boolean equals(Object obj) {
209         if (this == obj) {
210             return true;
211         }
212         if (obj == null) {
213             return false;
214         }
215         if (getClass() != obj.getClass()) {
216             return false;
217         }
218         Action other = (Action) obj;
219         if (type != other.type) {
220             return false;
221         }
222         return true;
223     }
224
225     @Override
226     public String toString() {
227         return type.toString();
228     }
229
230 }