Merge "Async config message"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / messages / AsyncConfigMessageSerializer.java
1 /*
2  * Copyright (c) 2017 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.openflowplugin.impl.protocol.serialization.messages;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.Map;
13 import java.util.Objects;
14 import java.util.Optional;
15 import java.util.WeakHashMap;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.AsyncConfigMessage;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.FlowRemovedMask;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.PacketInMask;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.async.config.service.rev170619.PortStatusMask;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowRemovedReason;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PacketInReason;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
24
25 public class AsyncConfigMessageSerializer extends AbstractMessageSerializer<AsyncConfigMessage> {
26     @Override
27     public void serialize(AsyncConfigMessage message, ByteBuf outBuffer) {
28         final int index = outBuffer.writerIndex();
29         super.serialize(message, outBuffer);
30
31         Optional.ofNullable(message.getPacketInMask())
32                 .ifPresent(mask -> {
33                     serializePacketInMask(mask.getMasterMask(), outBuffer);
34                     serializePacketInMask(mask.getSlaveMask(), outBuffer);
35                 });
36
37         Optional.ofNullable(message.getPortStatusMask())
38                 .ifPresent(mask -> {
39                     serializePortStatusMask(mask.getMasterMask(), outBuffer);
40                     serializePortStatusMask(mask.getSlaveMask(), outBuffer);
41                 });
42
43         Optional.ofNullable(message.getFlowRemovedMask())
44                 .ifPresent(mask -> {
45                     serializeFlowRemovedMask(mask.getMasterMask(), outBuffer);
46                     serializeFlowRemovedMask(mask.getSlaveMask(), outBuffer);
47                 });
48
49         outBuffer.setShort(index + 2, outBuffer.writerIndex() - index);
50     }
51
52     @Override
53     protected byte getMessageType() {
54         return 28;
55     }
56
57     private static void serializePacketInMask(final PacketInMask mask, final ByteBuf outBuffer) {
58         if (Objects.isNull(mask)) {
59             return;
60         }
61
62         final Map<Integer, Boolean> map = new WeakHashMap<>();
63
64         if (mask.isNOMATCH()) {
65             map.put(PacketInReason.OFPRNOMATCH.getIntValue(), true);
66         }
67
68         if (mask.isACTION()) {
69             map.put(PacketInReason.OFPRACTION.getIntValue(), true);
70         }
71
72         if (mask.isINVALIDTTL()) {
73             map.put(PacketInReason.OFPRINVALIDTTL.getIntValue(), true);
74         }
75
76         outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(map));
77     }
78
79     private static void serializePortStatusMask(final PortStatusMask mask, final ByteBuf outBuffer) {
80         if (Objects.isNull(mask)) {
81             return;
82         }
83
84         final Map<Integer, Boolean> map = new WeakHashMap<>();
85
86         if (mask.isADD()) {
87             map.put(PortReason.OFPPRADD.getIntValue(), true);
88         }
89
90         if (mask.isDELETE()) {
91             map.put(PortReason.OFPPRDELETE.getIntValue(), true);
92         }
93
94         if (mask.isUPDATE()) {
95             map.put(PortReason.OFPPRMODIFY.getIntValue(), true);
96         }
97
98         outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(map));
99     }
100
101     private static void serializeFlowRemovedMask(final FlowRemovedMask mask, final ByteBuf outBuffer) {
102         if (Objects.isNull(mask)) {
103             return;
104         }
105
106         final Map<Integer, Boolean> map = new WeakHashMap<>();
107
108         if (mask.isIDLETIMEOUT()) {
109             map.put(FlowRemovedReason.OFPRRIDLETIMEOUT.getIntValue(), true);
110         }
111
112         if (mask.isHARDTIMEOUT()) {
113             map.put(FlowRemovedReason.OFPRRHARDTIMEOUT.getIntValue(), true);
114         }
115
116         if (mask.isDELETE()) {
117             map.put(FlowRemovedReason.OFPRRDELETE.getIntValue(), true);
118         }
119
120         if (mask.isGROUPDELETE()) {
121             map.put(FlowRemovedReason.OFPRRGROUPDELETE.getIntValue(), true);
122         }
123
124         outBuffer.writeInt(ByteBufUtils.fillBitMaskFromMap(map));
125     }
126 }