Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / adsal / containermanager / api / src / main / java / org / opendaylight / controller / containermanager / ContainerData.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.containermanager;
11
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Locale;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.concurrent.ConcurrentHashMap;
20 import java.util.concurrent.ConcurrentMap;
21
22 import org.opendaylight.controller.sal.core.ContainerFlow;
23 import org.opendaylight.controller.sal.core.Node;
24 import org.opendaylight.controller.sal.core.NodeConnector;
25
26
27 /**
28  * This Class contains all the configurations pertained to this container
29  *
30  */
31 public class ContainerData implements Serializable {
32     private static final long serialVersionUID = 1L;
33     private String containerAdminRole;
34     private String containerOperatorRole;
35     private String name;
36     private ConcurrentMap<Node, Set<NodeConnector>> swPorts;
37     private short staticVlan;
38     private List<ContainerFlow> cFlowList;
39
40     /**
41      * Build a ContainerData from container configuration
42      *
43      * @param conf configuration from where the ContainerData need to be extracted
44      *
45      * @return the constructed containerData
46      */
47     public ContainerData(ContainerConfig conf) {
48         /*
49          * Back-end Non-Configuration Classes store names in lower case
50          */
51         name = conf.getContainerName().toLowerCase(Locale.ENGLISH);
52         swPorts = new ConcurrentHashMap<Node, Set<NodeConnector>>();
53         cFlowList = new ArrayList<ContainerFlow>();
54         staticVlan = conf.getStaticVlanValue();
55         containerAdminRole = conf.getContainerAdminRole();
56         containerOperatorRole = conf.getContainerOperatorRole();
57     }
58
59     /**
60      * getter for containerName
61      *
62      *
63      * @return the container name
64      */
65     public String getContainerName() {
66         return name;
67     }
68
69     /**
70      * getter for static vlan
71      *
72      *
73      * @return attribute static vlan
74      */
75     public short getStaticVlan() {
76         return staticVlan;
77     }
78
79     /**
80      * Check if the static vlan has non-zero value in that case is
81      * assumed to be assigned
82      *
83      *
84      * @return true if static vlan non-zero
85      */
86     public boolean hasStaticVlanAssigned() {
87         return (staticVlan != 0);
88     }
89
90     /**
91      * retrieve all NodeConnectors on a given switch on the containerName
92      * used in this object
93      *
94      * @param switchId the Node for switch we want to retrieve the list of ports
95      *
96      * @return get all the ports in form of a set of nodeconnectors
97      */
98     public Set<NodeConnector> getPorts(Node switchId) {
99         Set<NodeConnector> swpinfo = swPorts.get(switchId);
100         if (swpinfo != null) {
101             return swpinfo;
102         }
103         return null;
104     }
105
106     /**
107      * Returns the DB of all the association Node -> List of
108      * NodeConnectors composing this container
109      *
110      * @return the ConcurrentMap of all the association Node -> List
111      * of NodeConnectors
112      */
113     public ConcurrentMap<Node, Set<NodeConnector>> getSwPorts() {
114         return swPorts;
115     }
116
117     /**
118      * Add to the container a NodeConnector, that implicitely means also
119      * the Node being added
120      *
121      * @param port NodeConnector corresponding to the port being added
122      * in the container
123      */
124     public void addPortToSwitch(NodeConnector port) {
125         Node switchId = port.getNode();
126         if (switchId == null) {
127             return;
128         }
129         Set<NodeConnector> portSet = swPorts.get(switchId);
130         if (portSet == null) {
131             portSet = new HashSet<NodeConnector>();
132             swPorts.put(switchId, portSet);
133         }
134         portSet.add(port);
135     }
136
137     /**
138      * Remove from the container a NodeConnector, that implicitely means also
139      * the Node being removed
140      *
141      * @param port NodeConnector corresponding to the port being removed
142      * in the container
143      */
144     public void removePortFromSwitch(NodeConnector port) {
145         Node switchId = port.getNode();
146         if (switchId == null) {
147             return;
148         }
149         Set<NodeConnector> swpinfo = swPorts.get(switchId);
150         if (swpinfo != null) {
151             swpinfo.remove(port);
152             if (swpinfo.isEmpty()) {
153                 // There are no more ports lets get ride of the switch
154                 swPorts.remove(switchId);
155             }
156         }
157     }
158
159     /**
160      * Existence check of a Node in a container
161      *
162      * @param switchId See if a particular node is part of the container
163      *
164      * @return true if the node is in it else false
165      */
166     public boolean isSwitchInContainer(Node switchId) {
167         if (swPorts.containsKey(switchId)) {
168             return true;
169         } else {
170             return false;
171         }
172     }
173
174     /**
175      * Test for existance of NodeConnectors on a Node in a container
176      *
177      * @param switchId Node we are testing for port existance
178      *
179      * @return ture if the portset is non-empty
180      */
181     public boolean portListEmpty(Node switchId) {
182         Set<NodeConnector> swpinfo = swPorts.get(switchId);
183         if (swpinfo != null) {
184             if (!swpinfo.isEmpty()) {
185                 return false;
186             }
187         }
188         return true;
189     }
190
191     public void deleteFlowSpec(ContainerFlow cFlow) {
192         cFlowList.remove(cFlow);
193     }
194
195     public void addFlowSpec(ContainerFlow cFlow) {
196         cFlowList.add(cFlow);
197     }
198
199     public boolean isFlowSpecEmpty() {
200         return (cFlowList.isEmpty());
201
202     }
203
204     public boolean containsFlowSpec(ContainerFlow cFlow) {
205         return cFlowList.contains(cFlow);
206     }
207
208     public int getFlowSpecCount() {
209         return cFlowList.size();
210     }
211
212     public List<ContainerFlow> getContainerFlowSpecs() {
213         return cFlowList;
214     }
215
216     public boolean matchName(String name) {
217         return this.name.equalsIgnoreCase(name);
218     }
219
220     /**
221      * Returns all the node connectors part of the container
222      * @return The node connectors belonging to this container. The returning set is never null.
223      */
224     public Set<NodeConnector> getNodeConnectors() {
225         Set<NodeConnector> set = new HashSet<NodeConnector>();
226         for (Map.Entry<Node, Set<NodeConnector>> entry : swPorts.entrySet()) {
227             set.addAll(entry.getValue());
228         }
229         return set;
230     }
231
232     public String getContainerAdminRole() {
233         return containerAdminRole;
234     }
235
236     public String getContainerOperatorRole() {
237         return containerOperatorRole;
238     }
239 }