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