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