Fix of port-number model
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / PortConvertor.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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  * Contributor: usha.m.s@ericsson.com
9  */
10 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor;
11
12 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
13 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortState;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.Ports;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.port.desc._case.multipart.reply.port.desc.PortsBuilder;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * @author esssuuu This convertor class is used for Port Mod,port status and
30  *         port description messages,decodes SAL and encodes to OF Data
31  *
32  */
33 public final class PortConvertor {
34     private static final Logger LOG = LoggerFactory.getLogger(PortConvertor.class);
35
36     private PortConvertor() {
37
38     }
39
40     /**
41      * This method is used by PORT_MOD_MESSAGE
42      *
43      * @param source
44      * @return
45      */
46
47     public static PortModInput toPortModInput(
48             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port source,
49             short version) {
50
51
52         PortConfig config = maskPortConfigFields(source.getConfiguration());
53         PortConfigV10 configV10 = maskPortConfigV10Fields(source.getConfiguration());
54
55         PortModInputBuilder portModInputBuilder = new PortModInputBuilder();
56         portModInputBuilder.setAdvertise(getPortFeatures(source.getAdvertisedFeatures()));
57         portModInputBuilder.setPortNo(new PortNumber(
58                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())));
59
60         portModInputBuilder.setConfig(config);
61         portModInputBuilder.setMask(config);
62
63         portModInputBuilder.setHwAddress(new MacAddress(source.getHardwareAddress()));
64
65         portModInputBuilder.setVersion(version);
66
67         portModInputBuilder.setConfigV10(configV10);
68         portModInputBuilder.setMaskV10(configV10);
69         portModInputBuilder.setAdvertiseV10(getPortFeaturesV10(source.getAdvertisedFeatures()));
70         return portModInputBuilder.build();
71
72     }
73
74     private static PortConfig maskPortConfigFields(
75             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
76         Boolean portDown = configData.isPORTDOWN();
77         Boolean noRecv = configData.isNORECV();
78         Boolean noFwd = configData.isNOFWD();
79         Boolean noPacketIn = configData.isNOPACKETIN();
80
81         return new PortConfig(noFwd, noPacketIn, noRecv, portDown);
82
83     }
84
85     private static PortConfigV10 maskPortConfigV10Fields(
86             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig configData) {
87         Boolean portDown = configData.isPORTDOWN();
88         Boolean noRecv = configData.isNORECV();
89         Boolean noFwd = configData.isNOFWD();
90         Boolean noPacketIn = configData.isNOPACKETIN();
91
92         return new PortConfigV10(false, noFwd, noPacketIn, noRecv, true, true, portDown);
93
94     }
95     private static PortFeatures getPortFeatures(
96             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
97
98         return new PortFeatures(salPortFeatures.isHundredGbFd(), salPortFeatures.isHundredMbFd(),
99                 salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(),
100                 salPortFeatures.isTenMbHd(), salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(),
101                 salPortFeatures.isOneTbFd(), salPortFeatures.isFortyGbFd(), salPortFeatures.isAutoeng(),
102                 salPortFeatures.isCopper(), salPortFeatures.isFiber(), salPortFeatures.isOther(),
103                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
104     }
105
106     private static PortFeaturesV10 getPortFeaturesV10(
107             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures salPortFeatures) {
108
109         return new PortFeaturesV10(salPortFeatures.isHundredMbFd(), salPortFeatures.isHundredMbHd(), salPortFeatures.isTenGbFd(), salPortFeatures.isTenMbFd(), salPortFeatures.isTenMbHd(),
110                 salPortFeatures.isOneGbFd(), salPortFeatures.isOneGbHd(), salPortFeatures.isAutoeng(), salPortFeatures.isCopper(), salPortFeatures.isFiber(),
111                 salPortFeatures.isPause(), salPortFeatures.isPauseAsym());
112     }
113
114
115     /*
116      * This method is called as a reply to OFPMP_PORT_DESCRIPTION
117      * message(OF1.3.1)
118      */
119     /**
120      * @param source
121      *            :SAL FlowCapablePort
122      * @return OF:Ports
123      */
124     public static Ports toPortDesc(
125             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.FlowCapablePort source,
126             short version) {
127
128         PortConfig config = null;
129         PortState portState = null;
130
131         PortsBuilder oFPortDescDataBuilder = new PortsBuilder();
132
133         oFPortDescDataBuilder.setPortNo(
134                 OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.get(version), source.getPortNumber())); // portNO
135
136         oFPortDescDataBuilder.setHwAddr(source.getHardwareAddress());
137         oFPortDescDataBuilder.setName(source.getName());
138
139         config = maskPortConfigFields(source.getConfiguration());
140
141         oFPortDescDataBuilder.setConfig(config);
142
143         portState = getPortState(source.getState());
144
145         oFPortDescDataBuilder.setState(portState);
146         oFPortDescDataBuilder.setCurrentFeatures(getPortFeatures(source.getCurrentFeature()));
147         oFPortDescDataBuilder.setAdvertisedFeatures(getPortFeatures(source.getAdvertisedFeatures()));
148         oFPortDescDataBuilder.setSupportedFeatures(getPortFeatures(source.getSupported()));
149         oFPortDescDataBuilder.setPeerFeatures(getPortFeatures(source.getPeerFeatures()));
150         oFPortDescDataBuilder.setCurrSpeed(source.getCurrentSpeed());
151         oFPortDescDataBuilder.setMaxSpeed(source.getMaximumSpeed());
152
153         return oFPortDescDataBuilder.build();
154
155     }
156
157     private static PortState getPortState(
158             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortState state) {
159
160         boolean isLinkDown = state.isLinkDown();
161         boolean isBlocked = state.isBlocked();
162         boolean isLive = state.isLive();
163
164         return new PortState(isLinkDown, isBlocked, isLive);
165
166     }
167
168
169
170 }