BUG-2825: use provided Ipv4/MacAddress factories
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / PortStatusMessageFactory.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.deserialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
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.PortFeatures;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
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.PortStatusMessage;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder;
21
22 /**
23  * Translates PortStatus messages
24  * @author michal.polkorab
25  * @author timotej.kubas
26  */
27 public class PortStatusMessageFactory implements OFDeserializer<PortStatusMessage> {
28
29     private static final byte PADDING_IN_PORT_STATUS_HEADER = 7;
30     private static final byte PADDING_IN_OFP_PORT_HEADER_1 = 4;
31     private static final byte PADDING_IN_OFP_PORT_HEADER_2 = 2;
32
33     @Override
34     public PortStatusMessage deserialize(final ByteBuf rawMessage) {
35         PortStatusMessageBuilder builder = new PortStatusMessageBuilder();
36         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
37         builder.setXid(rawMessage.readUnsignedInt());
38         builder.setReason(PortReason.forValue(rawMessage.readUnsignedByte()));
39         rawMessage.skipBytes(PADDING_IN_PORT_STATUS_HEADER);
40         builder.setPortNo(rawMessage.readUnsignedInt());
41         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_1);
42         builder.setHwAddr(ByteBufUtils.readIetfMacAddress(rawMessage));
43         rawMessage.skipBytes(PADDING_IN_OFP_PORT_HEADER_2);
44         builder.setName(ByteBufUtils.decodeNullTerminatedString(rawMessage, EncodeConstants.MAX_PORT_NAME_LENGTH));
45         builder.setConfig(createPortConfig(rawMessage.readUnsignedInt()));
46         builder.setState(createPortState(rawMessage.readUnsignedInt()));
47         builder.setCurrentFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
48         builder.setAdvertisedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
49         builder.setSupportedFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
50         builder.setPeerFeatures(createPortFeatures(rawMessage.readUnsignedInt()));
51         builder.setCurrSpeed(rawMessage.readUnsignedInt());
52         builder.setMaxSpeed(rawMessage.readUnsignedInt());
53         return builder.build();
54     }
55
56     private static PortFeatures createPortFeatures(final long input){
57         final Boolean pf10mbHd = ((input) & (1<<0)) != 0;
58         final Boolean pf10mbFd = ((input) & (1<<1)) != 0;
59         final Boolean pf100mbHd = ((input) & (1<<2)) != 0;
60         final Boolean pf100mbFd = ((input) & (1<<3)) != 0;
61         final Boolean pf1gbHd = ((input) & (1<<4)) != 0;
62         final Boolean pf1gbFd = ((input) & (1<<5)) != 0;
63         final Boolean pf10gbFd = ((input) & (1<<6)) != 0;
64         final Boolean pf40gbFd = ((input) & (1<<7)) != 0;
65         final Boolean pf100gbFd = ((input) & (1<<8)) != 0;
66         final Boolean pf1tbFd = ((input) & (1<<9)) != 0;
67         final Boolean pfOther = ((input) & (1<<10)) != 0;
68         final Boolean pfCopper = ((input) & (1<<11)) != 0;
69         final Boolean pfFiber = ((input) & (1<<12)) != 0;
70         final Boolean pfAutoneg = ((input) & (1<<13)) != 0;
71         final Boolean pfPause = ((input) & (1<<14)) != 0;
72         final Boolean pfPauseAsym = ((input) & (1<<15)) != 0;
73         return new PortFeatures(pf100gbFd, pf100mbFd, pf100mbHd, pf10gbFd, pf10mbFd, pf10mbHd, pf1gbFd,
74                 pf1gbHd, pf1tbFd, pf40gbFd, pfAutoneg, pfCopper, pfFiber, pfOther, pfPause, pfPauseAsym);
75     }
76
77     private static PortState createPortState(final long input){
78         final Boolean psLinkDown = ((input) & (1<<0)) != 0;
79         final Boolean psBblocked  = ((input) & (1<<1)) != 0;
80         final Boolean psLive     = ((input) & (1<<2)) != 0;
81         return new PortState(psBblocked, psLinkDown, psLive);
82     }
83
84     private static PortConfig createPortConfig(final long input){
85         final Boolean pcPortDown   = ((input) & (1<<0)) != 0;
86         final Boolean pcNoRecv    = ((input) & (1<<2)) != 0;
87         final Boolean pcNoFwd       = ((input) & (1<<5)) != 0;
88         final Boolean pcNoPacketIn = ((input) & (1<<6)) != 0;
89         return new PortConfig(pcNoFwd, pcNoPacketIn, pcNoRecv, pcPortDown);
90     }
91 }