2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.util.HashMap;
14 import java.util.List;
17 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
18 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.FlowRemovedMask;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PacketInMask;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.async.body.grouping.PortStatusMask;
28 * Translates SetAsync messages
29 * @author timotej.kubas
30 * @author michal.polkorab
32 public class SetAsyncInputMessageFactory implements OFSerializer<SetAsyncInput> {
33 private static final byte MESSAGE_TYPE = 28;
34 private static final int MESSAGE_LENGTH = 32;
35 private static SetAsyncInputMessageFactory instance;
38 private SetAsyncInputMessageFactory() {
43 * @return singleton factory
45 public static synchronized SetAsyncInputMessageFactory getInstance() {
46 if (instance == null) {
47 instance = new SetAsyncInputMessageFactory();
53 public void messageToBuffer(short version, ByteBuf out,
54 SetAsyncInput message) {
55 ByteBufUtils.writeOFHeader(instance, message, out);
56 encodePacketInMask(message.getPacketInMask(), out);
57 encodePortStatusMask(message.getPortStatusMask(), out);
58 encodeFlowRemovedMask(message.getFlowRemovedMask(), out);
62 public int computeLength(SetAsyncInput message) {
63 return MESSAGE_LENGTH;
67 public byte getMessageType() {
71 private static void encodePacketInMask(List<PacketInMask> packetInMask, ByteBuf outBuffer) {
72 if (packetInMask != null) {
73 for (PacketInMask currentPacketMask : packetInMask) {
74 List<PacketInReason> mask = currentPacketMask.getMask();
76 Map<Integer, Boolean> packetInReasonMap = new HashMap<>();
77 for (PacketInReason packetInReason : mask) {
78 if (PacketInReason.OFPRNOMATCH.equals(packetInReason)) {
79 packetInReasonMap.put(PacketInReason.OFPRNOMATCH.getIntValue(), true);
80 } else if (PacketInReason.OFPRACTION.equals(packetInReason)) {
81 packetInReasonMap.put(PacketInReason.OFPRACTION.getIntValue(), true);
82 } else if (PacketInReason.OFPRINVALIDTTL.equals(packetInReason)) {
83 packetInReasonMap.put(PacketInReason.OFPRINVALIDTTL.getIntValue(), true);
86 outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(packetInReasonMap));
92 private static void encodePortStatusMask(List<PortStatusMask> portStatusMask, ByteBuf outBuffer) {
93 if (portStatusMask != null) {
94 for (PortStatusMask currentPortStatusMask : portStatusMask) {
95 List<PortReason> mask = currentPortStatusMask.getMask();
97 Map<Integer, Boolean> portStatusReasonMap = new HashMap<>();
98 for (PortReason packetInReason : mask) {
99 if (PortReason.OFPPRADD.equals(packetInReason)) {
100 portStatusReasonMap.put(PortReason.OFPPRADD.getIntValue(), true);
101 } else if (PortReason.OFPPRDELETE.equals(packetInReason)) {
102 portStatusReasonMap.put(PortReason.OFPPRDELETE.getIntValue(), true);
103 } else if (PortReason.OFPPRMODIFY.equals(packetInReason)) {
104 portStatusReasonMap.put(PortReason.OFPPRMODIFY.getIntValue(), true);
107 outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(portStatusReasonMap));
113 private static void encodeFlowRemovedMask(List<FlowRemovedMask> flowRemovedMask, ByteBuf outBuffer) {
114 if (flowRemovedMask != null) {
115 for (FlowRemovedMask currentFlowRemovedMask : flowRemovedMask) {
116 List<FlowRemovedReason> mask = currentFlowRemovedMask.getMask();
118 Map<Integer, Boolean> flowRemovedReasonMap = new HashMap<>();
119 for (FlowRemovedReason packetInReason : mask) {
120 if (FlowRemovedReason.OFPRRIDLETIMEOUT.equals(packetInReason)) {
121 flowRemovedReasonMap.put(FlowRemovedReason.OFPRRIDLETIMEOUT.getIntValue(), true);
122 } else if (FlowRemovedReason.OFPRRHARDTIMEOUT.equals(packetInReason)) {
123 flowRemovedReasonMap.put(FlowRemovedReason.OFPRRHARDTIMEOUT.getIntValue(), true);
124 } else if (FlowRemovedReason.OFPRRDELETE.equals(packetInReason)) {
125 flowRemovedReasonMap.put(FlowRemovedReason.OFPRRDELETE.getIntValue(), true);
126 } else if (FlowRemovedReason.OFPRRGROUPDELETE.equals(packetInReason)) {
127 flowRemovedReasonMap.put(FlowRemovedReason.OFPRRGROUPDELETE.getIntValue(), true);
130 outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(flowRemovedReasonMap));