Fix checkstyle
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / PortTranslatorUtil.java
1 /*
2  * Copyright (c) 2013 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 package org.opendaylight.openflowplugin.impl.util;
9
10 import java.math.BigInteger;
11 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
12 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
13 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdated;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdatedBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortReason;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.StateBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemovedBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdatedBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 public abstract class PortTranslatorUtil {
35     private static final Logger LOG = LoggerFactory.getLogger(PortTranslatorUtil.class);
36
37     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
38         translatePortFeatures(
39             final PortFeatures apf) {
40         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures napf = null;
41         if (apf != null) {
42             napf = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures(
43                     apf.isAutoneg(), //_autoeng
44                     apf.isCopper(), //_copper
45                     apf.isFiber(), //_fiber
46                     apf.is_40gbFd(), //_fortyGbFd
47                     apf.is_100gbFd(), //_hundredGbFd
48                     apf.is_100mbFd(), //_hundredMbFd
49                     apf.is_100mbHd(), //_hundredMbHd
50                     apf.is_1gbFd(), //_oneGbFd
51                     apf.is_1gbHd(), //_oneGbHd
52                     apf.is_1tbFd(), //_oneTbFd
53                     apf.isOther(), //_other
54                     apf.isPause(), //_pause
55                     apf.isPauseAsym(), //_pauseAsym
56                     apf.is_10gbFd(), //_tenGbFd
57                     apf.is_10mbFd(), //_tenMbFd
58                     apf.is_10mbHd()//_tenMbHd
59             );
60
61         }
62         return napf;
63     }
64
65     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
66         translatePortFeatures(
67             final PortFeaturesV10 apf) {
68         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures napf = null;
69         if (apf != null) {
70             napf = new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures(
71                     apf.isAutoneg(), //_autoeng
72                     apf.isCopper(), //_copper
73                     apf.isFiber(), //_fiber
74                     false, //_fortyGbFd
75                     false, //_hundredGbFd
76                     apf.is_100mbFd(), //_hundredMbFd
77                     apf.is_100mbHd(), //_hundredMbHd
78                     apf.is_1gbFd(), //_oneGbFd
79                     apf.is_1gbHd(), //_oneGbHd
80                     false, //_oneTbFd
81                     false, //_other
82                     apf.isPause(), //_pause
83                     apf.isPauseAsym(), //_pauseAsym
84                     apf.is_10gbFd(), //_tenGbFd
85                     apf.is_10mbFd(), //_tenMbFd
86                     apf.is_10mbHd()//_tenMbHd
87             );
88         }
89         return napf;
90     }
91
92     public static State translatePortState(final PortState state) {
93         StateBuilder nstate = new StateBuilder();
94         if (state != null) {
95             nstate.setBlocked(state.isBlocked());
96             nstate.setLinkDown(state.isLinkDown());
97             nstate.setLive(state.isLive());
98         }
99         return nstate.build();
100     }
101
102     public static State translatePortState(final PortStateV10 state) {
103         StateBuilder nstate = new StateBuilder();
104         if (state != null) {
105             nstate.setBlocked(state.isBlocked());
106             nstate.setLinkDown(state.isLinkDown());
107             nstate.setLive(state.isLive());
108         }
109         return nstate.build();
110     }
111
112     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
113         translatePortConfig(
114             final PortConfig pc) {
115         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig npc = null;
116         if (pc != null) {
117             npc = new org.opendaylight.yang.gen.v1.urn.opendaylight
118                     .flow.types.port.rev130925.PortConfig(pc.isNoFwd(),pc.isNoPacketIn(),pc.isNoRecv(),pc.isPortDown());
119         }
120         return npc;
121     }
122
123     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
124         translatePortConfig(
125             final PortConfigV10 pc) {
126         org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig npc = null;
127         if (pc != null) {
128             npc = new org.opendaylight.yang.gen.v1.urn.opendaylight
129                     .flow.types.port.rev130925.PortConfig(pc.isNoFwd(),pc.isNoPacketIn(),pc.isNoRecv(),pc.isPortDown());
130         }
131         return npc;
132     }
133
134     public static NodeConnectorUpdated translatePort(final Short version, final BigInteger datapathId,
135                                                      final Long portNumber, final PortGrouping port) {
136         OpenflowVersion ofVersion = OpenflowVersion.get(version);
137         final NodeConnectorUpdatedBuilder builder = InventoryDataServiceUtil
138                 .nodeConnectorUpdatedBuilderFromDatapathIdPortNo(datapathId, port.getPortNo(), ofVersion);
139         FlowCapableNodeConnectorUpdatedBuilder fcncub = new FlowCapableNodeConnectorUpdatedBuilder();
140         if (ofVersion == OpenflowVersion.OF13) {
141             fcncub.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(port.getAdvertisedFeatures()));
142             fcncub.setConfiguration(PortTranslatorUtil.translatePortConfig(port.getConfig()));
143             fcncub.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(port.getCurrentFeatures()));
144             fcncub.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(port.getPeerFeatures()));
145             fcncub.setState(PortTranslatorUtil.translatePortState(port.getState()));
146             fcncub.setSupported(PortTranslatorUtil.translatePortFeatures(port.getSupportedFeatures()));
147
148         } else if (ofVersion == OpenflowVersion.OF10) {
149             fcncub.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(port.getAdvertisedFeaturesV10()));
150             fcncub.setConfiguration(PortTranslatorUtil.translatePortConfig(port.getConfigV10()));
151             fcncub.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(port.getCurrentFeaturesV10()));
152             fcncub.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(port.getPeerFeaturesV10()));
153             fcncub.setState(PortTranslatorUtil.translatePortState(port.getStateV10()));
154             fcncub.setSupported(PortTranslatorUtil.translatePortFeatures(port.getSupportedFeaturesV10()));
155         }
156         if (port instanceof PortStatusMessage) {
157             if (((PortStatusMessage) port).getReason() != null) {
158                 fcncub.setReason(PortReason.forValue(((PortStatusMessage) port).getReason().getIntValue()));
159             } else {
160                 LOG.debug("PortStatus Message has reason as null");
161             }
162         }
163         fcncub.setCurrentSpeed(port.getCurrSpeed());
164         fcncub.setHardwareAddress(port.getHwAddr());
165         fcncub.setMaximumSpeed(port.getMaxSpeed());
166         fcncub.setName(port.getName());
167         fcncub.setPortNumber(OpenflowPortsUtil.getProtocolAgnosticPort(ofVersion, portNumber));
168         builder.addAugmentation(FlowCapableNodeConnectorUpdated.class, fcncub.build());
169         return builder.build();
170     }
171
172     public static NodeConnectorRemoved translatePortRemoved(final Short version, final BigInteger datapathId,
173                                                             final Long portNumber, final PortGrouping port) {
174         OpenflowVersion ofVersion = OpenflowVersion.get(version);
175         NodeConnectorRemovedBuilder builder = new NodeConnectorRemovedBuilder();
176         builder.setNodeConnectorRef(
177                 InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, portNumber, ofVersion));
178         return builder.build();
179     }
180 }