c647aaf708b53b605a9312d3e10b8eb59f498be6
[openflowjava.git] / 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
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
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;
26
27 /**
28  * Translates SetAsync messages
29  * @author timotej.kubas
30  * @author michal.polkorab
31  */
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;
36     
37
38     private SetAsyncInputMessageFactory() {
39         // singleton
40     }
41     
42     /**
43      * @return singleton factory
44      */
45     public static synchronized SetAsyncInputMessageFactory getInstance() {
46         if (instance == null) {
47             instance = new SetAsyncInputMessageFactory();
48         }
49         return instance;
50     }
51     
52     @Override
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);
59     }
60
61     @Override
62     public int computeLength(SetAsyncInput message) {
63         return MESSAGE_LENGTH;
64     }
65
66     @Override
67     public byte getMessageType() {
68         return MESSAGE_TYPE;
69     }
70     
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();
75                 if (mask != null)  {
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);
84                         }
85                     }
86                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(packetInReasonMap));
87                 }
88             }
89         }
90     }
91     
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();
96                 if (mask != null)  {
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);
105                         }
106                     }
107                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(portStatusReasonMap));
108                 }
109             }
110         }
111     }
112     
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();
117                 if (mask != null)  {
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);
128                         }
129                     }
130                     outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(flowRemovedReasonMap));
131                 }
132             }
133         }
134     }
135     
136 }