0fe5a9c1aa7db8a8cf036f9ea07a9466eea0e9fb
[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 org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
11 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
12 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorUpdatedBuilder;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortReason;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.StateBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemovedBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdatedBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
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.PortStatusMessage;
29 import org.opendaylight.yangtools.yang.common.Uint32;
30 import org.opendaylight.yangtools.yang.common.Uint64;
31 import org.opendaylight.yangtools.yang.common.Uint8;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public abstract class PortTranslatorUtil {
36     private static final Logger LOG = LoggerFactory.getLogger(PortTranslatorUtil.class);
37
38     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
39             translatePortFeatures(final PortFeatures apf) {
40         return apf == null ? null
41             : new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures(
42                 apf.getAutoneg(),   //_autoeng
43                 apf.getCopper(),    //_copper
44                 apf.getFiber(),     //_fiber
45                 apf.get_40gbFd(),   //_fortyGbFd
46                 apf.get_100gbFd(),  //_hundredGbFd
47                 apf.get_100mbFd(),  //_hundredMbFd
48                 apf.get_100mbHd(),  //_hundredMbHd
49                 apf.get_1gbFd(),    //_oneGbFd
50                 apf.get_1gbHd(),    //_oneGbHd
51                 apf.get_1tbFd(),    //_oneTbFd
52                 apf.getOther(),     //_other
53                 apf.getPause(),     //_pause
54                 apf.getPauseAsym(), //_pauseAsym
55                 apf.get_10gbFd(),   //_tenGbFd
56                 apf.get_10mbFd(),   //_tenMbFd
57                 apf.get_10mbHd());  //_tenMbHd
58     }
59
60     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures
61             translatePortFeatures(final PortFeaturesV10 apf) {
62         return apf == null ? null
63             : new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures(
64                 apf.getAutoneg(),   //_autoeng
65                 apf.getCopper(),    //_copper
66                 apf.getFiber(),     //_fiber
67                 false,              //_fortyGbFd
68                 false,              //_hundredGbFd
69                 apf.get_100mbFd(),  //_hundredMbFd
70                 apf.get_100mbHd(),  //_hundredMbHd
71                 apf.get_1gbFd(),    //_oneGbFd
72                 apf.get_1gbHd(),    //_oneGbHd
73                 false,              //_oneTbFd
74                 false,              //_other
75                 apf.getPause(),     //_pause
76                 apf.getPauseAsym(), //_pauseAsym
77                 apf.get_10gbFd(),   //_tenGbFd
78                 apf.get_10mbFd(),   //_tenMbFd
79                 apf.get_10mbHd());  //_tenMbHd
80     }
81
82     public static State translatePortState(final PortState state) {
83         StateBuilder nstate = new StateBuilder();
84         if (state != null) {
85             nstate.setBlocked(state.getBlocked()).setLinkDown(state.getLinkDown()).setLive(state.getLive());
86         }
87         return nstate.build();
88     }
89
90     public static State translatePortState(final PortStateV10 state) {
91         StateBuilder nstate = new StateBuilder();
92         if (state != null) {
93             nstate.setBlocked(state.getBlocked()).setLinkDown(state.getLinkDown()).setLive(state.getLive());
94         }
95         return nstate.build();
96     }
97
98     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
99             translatePortConfig(final PortConfig pc) {
100         return pc == null ? null
101             : new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig(
102                 pc.getNoFwd(), pc.getNoPacketIn(), pc.getNoRecv(), pc.getPortDown());
103     }
104
105     public static org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig
106             translatePortConfig(final PortConfigV10 pc) {
107         return pc == null ? null
108             : new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig(
109                 pc.getNoFwd(), pc.getNoPacketIn(), pc.getNoRecv(), pc.getPortDown());
110     }
111
112     public static NodeConnectorUpdated translatePort(final Uint8 version, final Uint64 datapathId,
113                                                      final Uint32 portNumber, final PortGrouping port) {
114         OpenflowVersion ofVersion = OpenflowVersion.get(version);
115         final NodeConnectorUpdatedBuilder builder = InventoryDataServiceUtil
116                 .nodeConnectorUpdatedBuilderFromDatapathIdPortNo(datapathId, port.getPortNo(), ofVersion);
117         FlowCapableNodeConnectorUpdatedBuilder fcncub = new FlowCapableNodeConnectorUpdatedBuilder();
118         if (ofVersion == OpenflowVersion.OF13) {
119             fcncub.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(port.getAdvertisedFeatures()));
120             fcncub.setConfiguration(PortTranslatorUtil.translatePortConfig(port.getConfig()));
121             fcncub.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(port.getCurrentFeatures()));
122             fcncub.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(port.getPeerFeatures()));
123             fcncub.setState(PortTranslatorUtil.translatePortState(port.getState()));
124             fcncub.setSupported(PortTranslatorUtil.translatePortFeatures(port.getSupportedFeatures()));
125
126         } else if (ofVersion == OpenflowVersion.OF10) {
127             fcncub.setAdvertisedFeatures(PortTranslatorUtil.translatePortFeatures(port.getAdvertisedFeaturesV10()));
128             fcncub.setConfiguration(PortTranslatorUtil.translatePortConfig(port.getConfigV10()));
129             fcncub.setCurrentFeature(PortTranslatorUtil.translatePortFeatures(port.getCurrentFeaturesV10()));
130             fcncub.setPeerFeatures(PortTranslatorUtil.translatePortFeatures(port.getPeerFeaturesV10()));
131             fcncub.setState(PortTranslatorUtil.translatePortState(port.getStateV10()));
132             fcncub.setSupported(PortTranslatorUtil.translatePortFeatures(port.getSupportedFeaturesV10()));
133         }
134         if (port instanceof PortStatusMessage) {
135             if (((PortStatusMessage) port).getReason() != null) {
136                 fcncub.setReason(PortReason.forValue(((PortStatusMessage) port).getReason().getIntValue()));
137             } else {
138                 LOG.debug("PortStatus Message has reason as null");
139             }
140         }
141         fcncub.setCurrentSpeed(port.getCurrSpeed());
142         fcncub.setHardwareAddress(port.getHwAddr());
143         fcncub.setMaximumSpeed(port.getMaxSpeed());
144         fcncub.setName(port.getName());
145         fcncub.setPortNumber(OpenflowPortsUtil.getProtocolAgnosticPort(ofVersion, portNumber));
146         return builder.addAugmentation(fcncub.build()).build();
147     }
148
149     public static NodeConnectorRemoved translatePortRemoved(final Uint8 version, final Uint64 datapathId,
150                                                             final Uint32 portNumber, final PortGrouping port) {
151         OpenflowVersion ofVersion = OpenflowVersion.get(version);
152         NodeConnectorRemovedBuilder builder = new NodeConnectorRemovedBuilder();
153         builder.setNodeConnectorRef(
154                 InventoryDataServiceUtil.nodeConnectorRefFromDatapathIdPortno(datapathId, portNumber, ofVersion));
155         return builder.build();
156     }
157 }