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