Merge "Improve logging in NetconfClient, logging-bridge. Increase connection timeout...
[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
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 (!isValidResourceName(nodeId)) {
127             return new Status(StatusCode.BADREQUEST, "Invalid NodeId");
128         }
129         return new Status(StatusCode.SUCCESS);
130     }
131
132     private Status validateNodeProperties() {
133         if (nodeProperties == null) {
134             return new Status(StatusCode.BADREQUEST, "nodeProperties cannot be null");
135         }
136         return new Status(StatusCode.SUCCESS);
137     }
138
139     @Override
140     public int hashCode() {
141         final int prime = 31;
142         int result = 1;
143         result = prime * result + ((nodeId == null) ? 0 : nodeId.hashCode());
144         result = prime * result + ((nodeProperties == null) ? 0 : nodeProperties.hashCode());
145         return result;
146     }
147
148     @Override
149     public boolean equals(Object obj) {
150         if (this == obj) {
151             return true;
152         }
153         if (obj == null) {
154             return false;
155         }
156         if (getClass() != obj.getClass()) {
157             return false;
158         }
159         SwitchConfig other = (SwitchConfig) obj;
160         if (nodeId == null) {
161             if (other.nodeId != null) {
162                 return false;
163             }
164         } else if (!nodeId.equals(other.nodeId)) {
165             return false;
166         }
167         if (nodeProperties == null) {
168             if (other.nodeProperties != null) {
169                 return false;
170             }
171         } else if (!nodeProperties.equals(other.nodeProperties)) {
172             return false;
173         }
174         return true;
175     }
176
177     @Override
178     public String toString() {
179         return ("SwitchConfig [Node=" + nodeId + ", Properties=" + nodeProperties + "]");
180     }
181
182     /**
183      * Implement clonable interface
184      */
185     @Override
186     public SwitchConfig clone() {
187         Map<String, Property> nodeProperties = (this.nodeProperties == null) ? null : new HashMap<String, Property>(
188                 this.nodeProperties);
189         return new SwitchConfig(this.nodeId, nodeProperties);
190     }
191
192 }