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