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