Extend openflow-protocol-impl serialization
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / GetFeaturesOutputFactory.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.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
20
21 /**
22  * @author giuseppex.petralia@intel.com
23  *
24  */
25 public class GetFeaturesOutputFactory implements OFSerializer<GetFeaturesOutput>, SerializerRegistryInjector {
26
27     @SuppressWarnings("unused")
28     private SerializerRegistry registry;
29     private static final byte MESSAGE_TYPE = 6;
30     private static final byte PADDING = 2;
31
32     @Override
33     public void serialize(GetFeaturesOutput message, ByteBuf outBuffer) {
34         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
35         outBuffer.writeLong(message.getDatapathId().longValue());
36         outBuffer.writeInt(message.getBuffers().intValue());
37         outBuffer.writeByte(message.getTables().intValue());
38         outBuffer.writeByte(message.getAuxiliaryId().intValue());
39         outBuffer.writeZero(PADDING);
40         writeCapabilities(message.getCapabilities(), outBuffer);
41         outBuffer.writeInt(message.getReserved().intValue());
42         ByteBufUtils.updateOFHeaderLength(outBuffer);
43     }
44
45     @Override
46     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
47         this.registry = serializerRegistry;
48     }
49
50     private static void writeCapabilities(Capabilities capabilities, ByteBuf outBuffer) {
51         Map<Integer, Boolean> map = new HashMap<>();
52         map.put(0, capabilities.isOFPCFLOWSTATS());
53         map.put(1, capabilities.isOFPCTABLESTATS());
54         map.put(2, capabilities.isOFPCPORTSTATS());
55         map.put(3, capabilities.isOFPCGROUPSTATS());
56         map.put(5, capabilities.isOFPCIPREASM());
57         map.put(6, capabilities.isOFPCQUEUESTATS());
58         map.put(8, capabilities.isOFPCPORTBLOCKED());
59         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
60         outBuffer.writeInt(bitmap);
61     }
62 }