Merge "Fix minor bug in FRM proactive flow code path"
[controller.git] / opendaylight / switchmanager / api / src / main / java / org / opendaylight / controller / switchmanager / SwitchConfig.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.switchmanager;
10
11 import java.io.Serializable;
12 import java.util.HashMap;
13 import java.util.Map;
14
15 import org.opendaylight.controller.sal.core.Description;
16 import org.opendaylight.controller.sal.core.ForwardingMode;
17 import org.opendaylight.controller.sal.core.Property;
18 import org.opendaylight.controller.sal.core.Tier;
19 import org.opendaylight.controller.sal.utils.Status;
20 import org.opendaylight.controller.sal.utils.StatusCode;
21
22 /**
23  * The class describes a switch configuration
24  */
25 public class SwitchConfig implements Cloneable, Serializable {
26     private static final long serialVersionUID = 1L;
27     private final String nodeId;
28     private final Map<String, Property> nodeProperties;
29
30     public SwitchConfig(String nodeId, Map<String, Property> nodeProperties) {
31         this.nodeId = nodeId;
32         this.nodeProperties = (nodeProperties == null) ? new HashMap<String, Property>()
33                 : new HashMap<String, Property>(nodeProperties);
34     }
35
36     @Deprecated
37     public SwitchConfig(String nodeId, String description, String tier, String mode) {
38         this.nodeId = nodeId;
39         this.nodeProperties = new HashMap<String, Property>();
40         Property desc = new Description(description);
41         this.nodeProperties.put(desc.getName(), desc);
42         Property nodeTier = new Tier(Integer.valueOf(tier));
43         this.nodeProperties.put(nodeTier.getName(), nodeTier);
44         Property forwardingMode = new ForwardingMode(Integer.valueOf(mode));
45         this.nodeProperties.put(forwardingMode.getName(), forwardingMode);
46     }
47
48     public String getNodeId() {
49         return this.nodeId;
50     }
51
52     public Map<String, Property> getNodeProperties() {
53         return new HashMap<String, Property>(this.nodeProperties);
54     }
55
56     public Property getProperty(String PropName) {
57         return nodeProperties.get(PropName);
58     }
59
60     /**
61      * This method returns the configured description of the node
62      *
63      * @return Configured description
64      *
65      * @deprecated replaced by getProperty(Description.propertyName)
66      */
67     @Deprecated
68     public String getNodeDescription() {
69         Description description = (Description) getProperty(Description.propertyName);
70         return (description == null) ? null : description.getValue();
71     }
72
73     /**
74      * This method returns the configured Tier of a node
75      *
76      * @return Configured tier
77      *
78      * @deprecated replaced by getProperty(Tier.TierPropName)
79      */
80     @Deprecated
81     public String getTier() {
82         Tier tier = (Tier) getProperty(Tier.TierPropName);
83         return (tier == null) ? null : String.valueOf(tier.getValue());
84     }
85
86     /**
87      * This method returns the configured Forwarding Mode of a node
88      *
89      * @return Configured Forwarding Mode
90      *
91      * @deprecated replaced by getProperty(ForwardingMode.name)
92      */
93     @Deprecated
94     public String getMode() {
95         ForwardingMode forwardingMode = (ForwardingMode) getProperty(ForwardingMode.name);
96         return (forwardingMode == null) ? null : String.valueOf(forwardingMode.getValue());
97     }
98
99     /**
100      * This method returns true, if the configured forwarding mode is proactive,
101      * else false
102      *
103      * @return true, if the configured forwarding mode is proactive, else false
104      *
105      * @deprecated replaced by isProactive() API of ForwardingMode property
106      */
107     @Deprecated
108     public boolean isProactive() {
109         return Integer.parseInt(getMode()) == ForwardingMode.PROACTIVE_FORWARDING;
110     }
111
112     public static long getSerialversionuid() {
113         return serialVersionUID;
114     }
115
116     public Status validate() {
117         Status validCheck = validateNodeId();
118         if (validCheck.isSuccess()) {
119             validCheck = validateNodeProperties();
120         }
121         return validCheck;
122     }
123
124     private Status validateNodeId() {
125         if (nodeId == null || nodeId.isEmpty()) {
126             return new Status(StatusCode.BADREQUEST, "NodeId cannot be empty");
127         }
128         return new Status(StatusCode.SUCCESS);
129     }
130
131     private Status validateNodeProperties() {
132         if (nodeProperties == null) {
133             return new Status(StatusCode.BADREQUEST, "nodeProperties cannot be null");
134         }
135         return new Status(StatusCode.SUCCESS);
136     }
137
138     @Override
139     public int hashCode() {
140         final int prime = 31;
141         int result = 1;
142         result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
143         result = prime * result + ((nodeProperties == null) ? 0 : nodeProperties.hashCode());
144         return result;
145     }
146
147     @Override
148     public boolean equals(Object obj) {
149         if (this == obj) {
150             return true;
151         }
152         if (obj == null) {
153             return false;
154         }
155         if (getClass() != obj.getClass()) {
156             return false;
157         }
158         SwitchConfig other = (SwitchConfig) obj;
159         if (nodeId == null) {
160             if (other.nodeId != null) {
161                 return false;
162             }
163         } else if (!nodeId.equals(other.nodeId)) {
164             return false;
165         }
166         if (nodeProperties == null) {
167             if (other.nodeProperties != null) {
168                 return false;
169             }
170         } else if (!nodeProperties.equals(other.nodeProperties)) {
171             return false;
172         }
173         return true;
174     }
175
176     @Override
177     public String toString() {
178         return ("SwitchConfig [Node=" + nodeId + ", Properties=" + nodeProperties + "]");
179     }
180
181     /**
182      * Implement clonable interface
183      */
184     @Override
185     public SwitchConfig clone() {
186         Map<String, Property> nodeProperties = (this.nodeProperties == null) ? null : new HashMap<String, Property>(
187                 this.nodeProperties);
188         return new SwitchConfig(this.nodeId, nodeProperties);
189     }
190
191 }