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