Merge "OPNFLWPLUG-972: Point to openflowplugin liblldp"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / factories / OF10FlowModInputMessageFactory.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.action.rev150203.actions.grouping.Action;
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.FlowModFlagsV10;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.v10.grouping.MatchV10;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInputBuilder;
28
29 /**
30  * Translates FlowModInput messages.
31  *
32  * @author giuseppex.petralia@intel.com
33  */
34 public class OF10FlowModInputMessageFactory implements OFDeserializer<FlowModInput>, DeserializerRegistryInjector {
35
36     private DeserializerRegistry registry;
37
38     @Override
39     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
40         registry = deserializerRegistry;
41     }
42
43     @Override
44     public FlowModInput deserialize(ByteBuf rawMessage) {
45         FlowModInputBuilder builder = new FlowModInputBuilder();
46         builder.setVersion((short) EncodeConstants.OF10_VERSION_ID);
47         builder.setXid(rawMessage.readUnsignedInt());
48         OFDeserializer<MatchV10> matchDeserializer = registry.getDeserializer(
49                 new MessageCodeKey(EncodeConstants.OF10_VERSION_ID, EncodeConstants.EMPTY_VALUE, MatchV10.class));
50         builder.setMatchV10(matchDeserializer.deserialize(rawMessage));
51         byte[] cookie = new byte[EncodeConstants.SIZE_OF_LONG_IN_BYTES];
52         rawMessage.readBytes(cookie);
53         builder.setCookie(new BigInteger(1, cookie));
54         builder.setCommand(FlowModCommand.forValue(rawMessage.readUnsignedShort()));
55         builder.setIdleTimeout(rawMessage.readUnsignedShort());
56         builder.setHardTimeout(rawMessage.readUnsignedShort());
57         builder.setPriority(rawMessage.readUnsignedShort());
58         builder.setBufferId(rawMessage.readUnsignedInt());
59         builder.setOutPort(new PortNumber((long) rawMessage.readUnsignedShort()));
60         builder.setFlagsV10(createFlowModFlagsFromBitmap(rawMessage.readUnsignedShort()));
61         CodeKeyMaker keyMaker = CodeKeyMakerFactory.createActionsKeyMaker(EncodeConstants.OF10_VERSION_ID);
62
63         List<Action> actions = ListDeserializer.deserializeList(EncodeConstants.OF10_VERSION_ID,
64                 rawMessage.readableBytes(), rawMessage, keyMaker, registry);
65         builder.setAction(actions);
66         return builder.build();
67     }
68
69     @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
70     private static FlowModFlagsV10 createFlowModFlagsFromBitmap(int input) {
71         final Boolean _oFPFFSENDFLOWREM = (input & 1 << 0) > 0;
72         final Boolean _oFPFFCHECKOVERLAP = (input & 1 << 1) > 0;
73         final Boolean _oFPFFEMERG = (input & 1 << 2) > 0;
74         return new FlowModFlagsV10(_oFPFFCHECKOVERLAP, _oFPFFEMERG, _oFPFFSENDFLOWREM);
75     }
76
77 }