Merge "Removing the Container Aware dependency from Clustering Services."
[controller.git] / opendaylight / containermanager / implementation / src / main / java / org / opendaylight / controller / containermanager / internal / ContainerImpl.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 /**
11  * @file   ContainerImpl.java
12  *
13  * @brief  Class that instantiated per-container implements the
14  * interface IContainer
15  *
16  */
17 package org.opendaylight.controller.containermanager.internal;
18
19 import java.util.concurrent.ConcurrentMap;
20 import java.util.ArrayList;
21 import java.util.HashSet;
22 import java.util.Dictionary;
23 import java.util.Map;
24
25 import org.apache.felix.dm.Component;
26 import org.opendaylight.controller.sal.core.ContainerFlow;
27 import org.opendaylight.controller.sal.core.IContainer;
28 import org.opendaylight.controller.sal.core.Node;
29 import org.opendaylight.controller.sal.core.NodeConnector;
30
31 import org.opendaylight.controller.containermanager.ContainerData;
32
33 import java.util.Set;
34 import java.util.List;
35
36 public class ContainerImpl implements IContainer {
37     private String containerName = null;
38     private IContainerInternal iContainerInternal = null;
39
40     public void setIContainerInternal(IContainerInternal s) {
41         this.iContainerInternal = s;
42     }
43
44     public void unsetIContainerInternal(IContainerInternal s) {
45         if (this.iContainerInternal == s) {
46             this.iContainerInternal = null;
47         }
48     }
49
50     /**
51      * Function called by the dependency manager when all the required
52      * dependencies are satisfied
53      *
54      */
55     void init(Component c) {
56         Dictionary<?, ?> props = c.getServiceProperties();
57         if (props != null) {
58             this.containerName = (String) props.get("containerName");
59         }
60     }
61
62     @Override
63     public String getName() {
64         return this.containerName;
65     }
66
67     @Override
68     public List<ContainerFlow> getContainerFlows() {
69         List<ContainerFlow> list = new ArrayList<ContainerFlow>();
70
71         ContainerData d = this.iContainerInternal.getContainerData(this.containerName);
72         if (d != null) {
73             list.addAll(d.getContainerFlowSpecs());
74         }
75         return list;
76     }
77
78     @Override
79     public short getTag(Node n) {
80         ContainerData d = this.iContainerInternal.getContainerData(this.containerName);
81         if (d != null) {
82             return d.getStaticVlan();
83         }
84         // Return 0 because in containerData that means an unassigned tag
85         return (short) 0;
86     }
87
88     @Override
89     public Set<NodeConnector> getNodeConnectors() {
90         Set<NodeConnector> set = new HashSet<NodeConnector>();
91
92         ContainerData d = this.iContainerInternal.getContainerData(this.containerName);
93         if (d != null) {
94             ConcurrentMap<Node, Set<NodeConnector>> m = d.getSwPorts();
95             if (m != null) {
96                 for (Map.Entry<Node, Set<NodeConnector>> entry : m.entrySet()) {
97                     set.addAll(entry.getValue());
98                 }
99             }
100         }
101         return set;
102     }
103
104     @Override
105     public Set<Node> getNodes() {
106         Set<Node> set = new HashSet<Node>();
107
108         ContainerData d = this.iContainerInternal.getContainerData(this.containerName);
109         if (d != null) {
110             ConcurrentMap<Node, Set<NodeConnector>> m = d.getSwPorts();
111             if (m != null) {
112                 set.addAll(m.keySet());
113             }
114         }
115         return set;
116     }
117 }