DevideManagerImpl
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceStateImpl.java
1 /**
2  * Copyright (c) 2015 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.openflowplugin.impl.device;
10
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import javax.annotation.CheckForNull;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
31
32 /**
33  * openflowplugin-impl
34  * org.opendaylight.openflowplugin.impl.device
35  * <p/>
36  * DeviceState is builded from {@link FeaturesReply} and {@link NodeId}. Both values are inside
37  * {@link org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext}
38  *
39  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
40  *         <p/>
41  *         Created: Mar 29, 2015
42  */
43 class DeviceStateImpl implements DeviceState {
44
45     private final GetFeaturesOutput featuresOutput;
46     private final Map<Long, PortGrouping> portGrouping;
47     private final Map<Long, Long> portsBandwidth;
48     private final NodeId nodeId;
49     private final KeyedInstanceIdentifier<Node, NodeKey> nodeII;
50     private final short version;
51     private boolean valid;
52
53     public DeviceStateImpl(@CheckForNull final FeaturesReply featuresReply, @Nonnull final NodeId nodeId) {
54         Preconditions.checkArgument(featuresReply != null);
55         Preconditions.checkArgument(featuresReply.getPhyPort() != null);
56         featuresOutput = new GetFeaturesOutputBuilder(featuresReply).build();
57         this.nodeId = Preconditions.checkNotNull(nodeId);
58         nodeII = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
59         version = featuresReply.getVersion();
60         portGrouping = new HashMap<>();
61         portsBandwidth = new HashMap<>();
62         for (final PhyPort port : featuresReply.getPhyPort()) {
63             portGrouping.put(port.getPortNo(), port);
64             portsBandwidth.put(port.getPortNo(), port.getMaxSpeed());
65         }
66     }
67
68     @Override
69     public NodeId getNodeId() {
70         return nodeId;
71     }
72
73     @Override
74     public KeyedInstanceIdentifier<Node, NodeKey> getNodeInstanceIdentifier() {
75         return nodeII;
76     }
77
78     @Override
79     public GetFeaturesOutput getFeatures() {
80         return featuresOutput;
81     }
82
83     @Override
84     public boolean isValid() {
85         return valid;
86     }
87
88     @Override
89     public void setValid(final boolean valid) {
90         this.valid = valid;
91     }
92
93     @Override
94     public Map<Long, PortGrouping> getPhysicalPorts() {
95         return portGrouping;
96     }
97
98     @Override
99     public Map<Long, Long> getPortsBandwidth() {
100         return portsBandwidth;
101     }
102
103     @Override
104     public Set<Long> getPorts() {
105         return portGrouping.keySet();
106     }
107
108     @Override
109     public PortGrouping getPhysicalPort(final Long portNumber) {
110         return portGrouping.get(portNumber);
111     }
112
113     @Override
114     public Long getPortBandwidth(final Long portNumber) {
115         return portsBandwidth.get(portNumber);
116     }
117
118     @Override
119     public boolean isPortEnabled(final long portNumber) {
120         return portGrouping.containsKey(portNumber);
121     }
122
123     @Override
124     public boolean isPortEnabled(final PortGrouping port) {
125         return portGrouping.containsValue(port);
126     }
127
128     @Override
129     public List<PortGrouping> getEnabledPorts() {
130         return new ArrayList<PortGrouping>(portGrouping.values());
131     }
132
133     @Override
134     public int getSeed() {
135         return hashCode();
136     }
137
138     @Override
139     public short getVersion() {
140         return version;
141     }
142
143 }