Extend openflow-protocol-impl serialization
[openflowjava.git] / 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.HashMap;
12 import java.util.List;
13 import java.util.Map;
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.common.types.rev130731.FlowRemovedReason;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
24
25 /**
26  * @author giuseppex.petralia@intel.com
27  *
28  */
29
30 public class GetAsyncReplyMessageFactory implements OFSerializer<GetAsyncOutput> {
31     private static final byte MESSAGE_TYPE = 27;
32
33     @Override
34     public void serialize(GetAsyncOutput message, ByteBuf outBuffer) {
35         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
36         serializePacketInMask(message.getPacketInMask(), outBuffer);
37         serializePortStatusMask(message.getPortStatusMask(), outBuffer);
38         serializeFlowRemovedMask(message.getFlowRemovedMask(), outBuffer);
39         ByteBufUtils.updateOFHeaderLength(outBuffer);
40     }
41
42     private static void serializePacketInMask(List<PacketInMask> packetInMask, ByteBuf outBuffer) {
43         if (packetInMask != null) {
44             for (PacketInMask currentPacketMask : packetInMask) {
45                 List<PacketInReason> mask = currentPacketMask.getMask();
46                 if (mask != null) {
47                     Map<Integer, Boolean> packetInReasonMap = new HashMap<>();
48                     for (PacketInReason packetInReason : mask) {
49                         if (PacketInReason.OFPRNOMATCH.equals(packetInReason)) {
50                             packetInReasonMap.put(PacketInReason.OFPRNOMATCH.getIntValue(), true);
51                         } else if (PacketInReason.OFPRACTION.equals(packetInReason)) {
52                             packetInReasonMap.put(PacketInReason.OFPRACTION.getIntValue(), true);
53                         } else if (PacketInReason.OFPRINVALIDTTL.equals(packetInReason)) {
54                             packetInReasonMap.put(PacketInReason.OFPRINVALIDTTL.getIntValue(), true);
55                         }
56                     }
57                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(packetInReasonMap));
58                 }
59             }
60         }
61     }
62
63     private static void serializePortStatusMask(List<PortStatusMask> portStatusMask, ByteBuf outBuffer) {
64         if (portStatusMask != null) {
65             for (PortStatusMask currentPortStatusMask : portStatusMask) {
66                 List<PortReason> mask = currentPortStatusMask.getMask();
67                 if (mask != null) {
68                     Map<Integer, Boolean> portStatusReasonMap = new HashMap<>();
69                     for (PortReason packetInReason : mask) {
70                         if (PortReason.OFPPRADD.equals(packetInReason)) {
71                             portStatusReasonMap.put(PortReason.OFPPRADD.getIntValue(), true);
72                         } else if (PortReason.OFPPRDELETE.equals(packetInReason)) {
73                             portStatusReasonMap.put(PortReason.OFPPRDELETE.getIntValue(), true);
74                         } else if (PortReason.OFPPRMODIFY.equals(packetInReason)) {
75                             portStatusReasonMap.put(PortReason.OFPPRMODIFY.getIntValue(), true);
76                         }
77                     }
78                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(portStatusReasonMap));
79                 }
80             }
81         }
82     }
83
84     private static void serializeFlowRemovedMask(List<FlowRemovedMask> flowRemovedMask, ByteBuf outBuffer) {
85         if (flowRemovedMask != null) {
86             for (FlowRemovedMask currentFlowRemovedMask : flowRemovedMask) {
87                 List<FlowRemovedReason> mask = currentFlowRemovedMask.getMask();
88                 if (mask != null) {
89                     Map<Integer, Boolean> flowRemovedReasonMap = new HashMap<>();
90                     for (FlowRemovedReason packetInReason : mask) {
91                         if (FlowRemovedReason.OFPRRIDLETIMEOUT.equals(packetInReason)) {
92                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRIDLETIMEOUT.getIntValue(), true);
93                         } else if (FlowRemovedReason.OFPRRHARDTIMEOUT.equals(packetInReason)) {
94                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRHARDTIMEOUT.getIntValue(), true);
95                         } else if (FlowRemovedReason.OFPRRDELETE.equals(packetInReason)) {
96                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRDELETE.getIntValue(), true);
97                         } else if (FlowRemovedReason.OFPRRGROUPDELETE.equals(packetInReason)) {
98                             flowRemovedReasonMap.put(FlowRemovedReason.OFPRRGROUPDELETE.getIntValue(), true);
99                         }
100                     }
101                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(flowRemovedReasonMap));
102                 }
103             }
104         }
105     }
106
107 }