Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / FlowModInputMessageFactory.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.deserialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.math.BigInteger;
12 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageCodeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMaker;
19 import org.opendaylight.openflowjava.protocol.impl.util.CodeKeyMakerFactory;
20 import org.opendaylight.openflowjava.protocol.impl.util.ListDeserializer;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.TableId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
29
30 /**
31  * Translates FlowModInput messages
32  */
33 public class FlowModInputMessageFactory implements OFDeserializer<FlowModInput>, DeserializerRegistryInjector {
34
35     private static final byte PADDING = 2;
36     private DeserializerRegistry registry;
37
38     @Override
39     public FlowModInput deserialize(ByteBuf rawMessage) {
40         FlowModInputBuilder builder = new FlowModInputBuilder();
41         builder.setVersion((short) EncodeConstants.OF13_VERSION_ID);
42         builder.setXid(rawMessage.readUnsignedInt());
43         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
44         rawMessage.readBytes(cookie);
45         builder.setCookie(new BigInteger(1, cookie));
46         byte[] cookie_mask = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
47         rawMessage.readBytes(cookie_mask);
48         builder.setCookieMask(new BigInteger(1, cookie_mask));
49         builder.setTableId(new TableId((long) rawMessage.readUnsignedByte()));
50         builder.setCommand(FlowModCommand.forValue(rawMessage.readUnsignedByte()));
51         builder.setIdleTimeout(rawMessage.readUnsignedShort());
52         builder.setHardTimeout(rawMessage.readUnsignedShort());
53         builder.setPriority(rawMessage.readUnsignedShort());
54         builder.setBufferId(rawMessage.readUnsignedInt());
55         builder.setOutPort(new PortNumber(rawMessage.readUnsignedInt()));
56         builder.setOutGroup(rawMessage.readUnsignedInt());
57         builder.setFlags(createFlowModFlagsFromBitmap(rawMessage.readUnsignedShort()));
58         rawMessage.skipBytes(PADDING);
59         OFDeserializer<Match> matchDeserializer = registry.getDeserializer(
60                 new MessageCodeKey(EncodeConstants.OF13_VERSION_ID, EncodeConstants.EMPTY_VALUE, Match.class));
61         builder.setMatch(matchDeserializer.deserialize(rawMessage));
62         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createInstructionsKeyMaker(EncodeConstants.OF13_VERSION_ID);
63         List<Instruction> instructions = ListDeserializer.deserializeList(EncodeConstants.OF13_VERSION_ID,
64                 rawMessage.readableBytes(), rawMessage, keyMaker, registry);
65         builder.setInstruction(instructions);
66         return builder.build();
67     }
68
69     private static FlowModFlags createFlowModFlagsFromBitmap(int input) {
70         final Boolean _oFPFFSENDFLOWREM = (input & (1 << 0)) > 0;
71         final Boolean _oFPFFCHECKOVERLAP = (input & (1 << 1)) > 0;
72         final Boolean _oFPFFRESETCOUNTS = (input & (1 << 2)) > 0;
73         final Boolean _oFPFFNOPKTCOUNTS = (input & (1 << 3)) > 0;
74         final Boolean _oFPFFNOBYTCOUNTS = (input & (1 << 4)) > 0;
75         return new FlowModFlags(_oFPFFCHECKOVERLAP, _oFPFFNOBYTCOUNTS, _oFPFFNOPKTCOUNTS, _oFPFFRESETCOUNTS,
76                 _oFPFFSENDFLOWREM);
77     }
78
79     @Override
80     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
81         registry = deserializerRegistry;
82     }
83 }