Bump MRI upstreams
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GetAsyncReplyMessageFactory.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 io.netty.buffer.ByteBuf;
11 import java.util.Collection;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
20 import org.opendaylight.yangtools.yang.binding.Enumeration;
21
22 /**
23  * Translates GetAsyncOutput messages.
24  *
25  * @author giuseppex.petralia@intel.com
26  */
27 public class GetAsyncReplyMessageFactory implements OFSerializer<GetAsyncOutput> {
28     private static final byte MESSAGE_TYPE = 27;
29
30     @Override
31     public void serialize(final GetAsyncOutput message, final ByteBuf outBuffer) {
32         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
33         serializePacketInMask(message.getPacketInMask(), outBuffer);
34         serializePortStatusMask(message.getPortStatusMask(), outBuffer);
35         serializeFlowRemovedMask(message.getFlowRemovedMask(), outBuffer);
36         ByteBufUtils.updateOFHeaderLength(outBuffer);
37     }
38
39     private static void serializePacketInMask(final List<PacketInMask> packetInMask, final ByteBuf outBuffer) {
40         if (packetInMask != null) {
41             for (PacketInMask currentPacketMask : packetInMask) {
42                 serializeReasons(currentPacketMask.getMask(), outBuffer);
43             }
44         }
45     }
46
47     private static void serializePortStatusMask(final List<PortStatusMask> portStatusMask, final ByteBuf outBuffer) {
48         if (portStatusMask != null) {
49             for (PortStatusMask currentPortStatusMask : portStatusMask) {
50                 serializeReasons(currentPortStatusMask.getMask(), outBuffer);
51             }
52         }
53     }
54
55     private static void serializeFlowRemovedMask(final List<FlowRemovedMask> flowRemovedMask, final ByteBuf outBuffer) {
56         if (flowRemovedMask != null) {
57             for (FlowRemovedMask currentFlowRemovedMask : flowRemovedMask) {
58                 serializeReasons(currentFlowRemovedMask.getMask(), outBuffer);
59             }
60         }
61     }
62
63     private static void serializeReasons(final Collection<? extends Enumeration> reasons, final ByteBuf outBuffer) {
64         if (reasons != null) {
65             int bitmap = 0;
66             for (Enumeration reason : reasons) {
67                 bitmap |= 1 << reason.getIntValue();
68             }
69             outBuffer.writeInt(bitmap);
70         }
71     }
72 }