Update MRI upstreams for Phosphorus
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PacketInMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.openflowjava.protocol.impl.serialization.factories;
9
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Objects;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
21
22 /**
23  * Translates PacketIn messages.
24  */
25 public class PacketInMessageFactory implements OFSerializer<PacketInMessage>, SerializerRegistryInjector {
26     private static final byte PADDING = 2;
27     private static final byte MESSAGE_TYPE = 10;
28     private SerializerRegistry registry;
29
30     @Override
31     @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") // FB doesn't recognize Objects.requireNonNull
32     public void serialize(final PacketInMessage message, final ByteBuf outBuffer) {
33         Objects.requireNonNull(registry);
34
35         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
36         outBuffer.writeInt(message.getBufferId().intValue());
37         outBuffer.writeShort(message.getTotalLen().intValue());
38         outBuffer.writeByte(message.getReason().getIntValue());
39         outBuffer.writeByte(message.getTableId().getValue().byteValue());
40         outBuffer.writeLong(message.getCookie().longValue());
41         OFSerializer<Match> matchSerializer = registry.getSerializer(
42                 new MessageTypeKey<>(message.getVersion(), Match.class));
43         matchSerializer.serialize(message.getMatch(), outBuffer);
44         outBuffer.writeZero(PADDING);
45
46         byte[] data = message.getData();
47
48         if (data != null) {
49             outBuffer.writeBytes(data);
50         }
51         ByteBufUtils.updateOFHeaderLength(outBuffer);
52     }
53
54     @Override
55     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
56         this.registry = serializerRegistry;
57     }
58 }