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