Added OFPM_ALL constant
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / common / NodeConnectorTranslatorUtil.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.common;
10
11 import com.google.common.base.Preconditions;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.List;
15 import javax.annotation.CheckForNull;
16 import javax.annotation.Nullable;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnectorBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.State;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.flow.capable.port.StateBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
34
35 /**
36  * openflowplugin-impl
37  * org.opendaylight.openflowplugin.impl.common
38  *
39  * Translator helper for Translating OF java models to MD-SAL inventory models.
40  * Translator focus for {@link NodeConnector} object and relevant OF augmentation
41  * {@link FlowCapableNodeConnector}.
42  *
43  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
44  *
45  * Created: Mar 31, 2015
46  */
47 public final class NodeConnectorTranslatorUtil {
48
49     private NodeConnectorTranslatorUtil () {
50         throw new UnsupportedOperationException("Utility class");
51     }
52
53     /**
54      * Method translates {@link PhyPort} object directly to {@link NodeConnector} which is augmented
55      * by {@link FlowCapableNodeConnector} and contains all relevant content translated by actual OF version.
56      *
57      * @param featuresReply
58      * @return
59      */
60     public static List<NodeConnector> translateNodeConnectorFromFeaturesReply(@CheckForNull final FeaturesReply featuresReply) {
61         Preconditions.checkArgument(featuresReply != null);
62         Preconditions.checkArgument(featuresReply.getPhyPort() != null);
63         final Short version = featuresReply.getVersion();
64         final BigInteger dataPathId = featuresReply.getDatapathId();
65         final List<NodeConnector> resultList = new ArrayList<>(featuresReply.getPhyPort().size());
66         for (final PhyPort port : featuresReply.getPhyPort()) {
67             final NodeConnectorBuilder ncBuilder = new NodeConnectorBuilder();
68             ncBuilder.setId(makeNodeConnectorId(dataPathId, port.getName(), port.getPortNo()));
69             ncBuilder.addAugmentation(FlowCapableNodeConnector.class, translateFlowCapableNodeFromPhyPort(port, version));
70             resultList.add(ncBuilder.build());
71         }
72         return resultList;
73     }
74
75     /**
76      * Method translates {@link PhyPort} object directly to {@link FlowCapableNodeConnector} which is augmented
77      * by {@link NodeConnector} and contains all relevant content translated by actual OF version.
78      *
79      * @param port
80      * @param version
81      * @return
82      */
83     public static FlowCapableNodeConnector translateFlowCapableNodeFromPhyPort(@CheckForNull final PhyPort port, final short version) {
84         Preconditions.checkArgument(port != null);
85         final FlowCapableNodeConnectorBuilder fcncBuilder = new FlowCapableNodeConnectorBuilder();
86         fcncBuilder.setHardwareAddress(port.getHwAddr());
87         fcncBuilder.setCurrentSpeed(port.getCurrSpeed());
88         fcncBuilder.setMaximumSpeed(port.getMaxSpeed());
89         fcncBuilder.setName(port.getName());
90         fcncBuilder.setPortNumber(new PortNumberUni(port.getPortNo()));
91         if (OFConstants.OFP_VERSION_1_3 == version) {
92             fcncBuilder.setAdvertisedFeatures(translatePortFeatures(port.getAdvertisedFeatures()));
93             fcncBuilder.setConfiguration(translatePortConfig(port.getConfig()));
94             fcncBuilder.setCurrentFeature(translatePortFeatures(port.getCurrentFeatures()));
95             fcncBuilder.setPeerFeatures(translatePortFeatures(port.getPeerFeatures()));
96             fcncBuilder.setSupported(translatePortFeatures(port.getSupportedFeatures()));
97             fcncBuilder.setState(translatePortState(port.getState()));
98         } else if (OFConstants.OFP_VERSION_1_0 == version) {
99             fcncBuilder.setAdvertisedFeatures(translatePortFeatures(port.getAdvertisedFeaturesV10()));
100             fcncBuilder.setConfiguration(translatePortConfig(port.getConfigV10()));
101             fcncBuilder.setCurrentFeature(translatePortFeatures(port.getCurrentFeaturesV10()));
102             fcncBuilder.setPeerFeatures(translatePortFeatures(port.getPeerFeaturesV10()));
103             fcncBuilder.setSupported(translatePortFeatures(port.getSupportedFeaturesV10()));
104             fcncBuilder.setState(translatePortState(port.getStateV10()));
105         } else {
106             throw new IllegalArgumentException("Unknown OF version " + version);
107         }
108         return fcncBuilder.build();
109     }
110
111     private static PortConfig translatePortConfig(@CheckForNull final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig pc) {
112         Preconditions.checkArgument(pc != null);
113         return new PortConfig(pc.isNoFwd(), pc.isNoPacketIn(), pc.isNoRecv(), pc.isPortDown());
114     }
115
116     private static PortConfig translatePortConfig(@CheckForNull final PortConfigV10 pc) {
117         Preconditions.checkArgument(pc != null);
118         return new PortConfig(pc.isNoFwd(), pc.isNoPacketIn(), pc.isNoRecv(), pc.isPortDown());
119     }
120
121     private static PortFeatures translatePortFeatures(@CheckForNull final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures pf) {
122         Preconditions.checkArgument(pf != null);
123         return new PortFeatures(pf.isAutoneg(), pf.isCopper(), pf.isFiber(), pf.is_40gbFd(), pf.is_100gbFd(), pf.is_100mbFd(), pf.is_100mbHd(),
124                 pf.is_1gbFd(), pf.is_1gbHd(), pf.is_1tbFd(), pf.isOther(), pf.isPause(), pf.isPauseAsym(), pf.is_10gbFd(), pf.is_10mbFd(), pf.is_10mbHd());
125     }
126
127     private static PortFeatures translatePortFeatures(@CheckForNull final PortFeaturesV10 pf) {
128         Preconditions.checkArgument(pf != null);
129         return new PortFeatures(pf.isAutoneg(), pf.isCopper(), pf.isFiber(), Boolean.FALSE, Boolean.FALSE, pf.is_100mbFd(), pf.is_100mbHd(),
130                 pf.is_1gbFd(), pf.is_1gbHd(), Boolean.FALSE, Boolean.FALSE, pf.isPause(), pf.isPauseAsym(), pf.is_10gbFd(), pf.is_10mbFd(), pf.is_10mbHd());
131     }
132
133     private static State translatePortState(@CheckForNull final PortState state) {
134         Preconditions.checkArgument(state != null);
135         return new StateBuilder().setBlocked(state.isBlocked()).setLinkDown(state.isLinkDown()).setLive(state.isLive()).build();
136     }
137
138     private static State translatePortState(@CheckForNull final PortStateV10 state) {
139         Preconditions.checkArgument(state != null);
140         return new StateBuilder().setBlocked(state.isBlocked()).setLinkDown(state.isLinkDown()).setLive(state.isLive()).build();
141     }
142
143     /**
144      * Method makes NodeConnectorId with prefix  "openflow:" from dataPathId and logical name or port number
145      *
146      * @param dataPathId
147      * @param logicalName
148      * @param portNo
149      * @return
150      */
151     public static NodeConnectorId makeNodeConnectorId(@CheckForNull final BigInteger dataPathId,
152             @Nullable final String logicalName, final long portNo) {
153         Preconditions.checkArgument(dataPathId != null);
154         return new NodeConnectorId(OFConstants.OF_URI_PREFIX + dataPathId + ":" + (logicalName == null ? portNo : logicalName));
155     }
156 }