Add methods that allows registering any serializer
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / experimenter / BundleControlFactory.java
1 /*
2  * Copyright (c) 2016 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.openflowjava.protocol.impl.deserialization.experimenter;
10
11 import io.netty.buffer.ByteBuf;
12 import java.util.ArrayList;
13 import java.util.List;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.DeserializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.util.ExperimenterDeserializerKeyFactory;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleControlType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundleId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.BundlePropertyType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.BundleProperty;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.BundlePropertyBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.BundleExperimenterPropertyBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.bundle.properties.bundle.property.bundle.property.entry.bundle.experimenter.property.BundleExperimenterPropertyData;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleControl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.approved.extensions.rev160802.experimenter.input.experimenter.data.of.choice.BundleControlBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
30
31 /**
32  * Translates BundleControl messages (OpenFlow v1.3 extension #230).
33  */
34 public class BundleControlFactory implements OFDeserializer<BundleControl>, DeserializerRegistryInjector {
35
36     private DeserializerRegistry deserializerRegistry;
37
38     @Override
39     public BundleControl deserialize(ByteBuf message) {
40         BundleId bundleId = new BundleId(message.readUnsignedInt());
41         BundleControlType type = BundleControlType.forValue(message.readUnsignedShort());
42         BundleFlags flags = createBundleFlags(message.readUnsignedShort());
43         BundleControlBuilder builder = new BundleControlBuilder();
44         List<BundleProperty> properties = createBundleProperties(message);
45         return builder.setBundleId(bundleId)
46                 .setType(type)
47                 .setFlags(flags)
48                 .setBundleProperty(properties)
49                 .build();
50     }
51
52     private static BundleFlags createBundleFlags(final int flags) {
53         Boolean isAtomic = (flags & (1 << 0)) != 0;
54         Boolean isOrdered = (flags & (1 << 1)) != 0;
55         return new BundleFlags(isAtomic, isOrdered);
56     }
57
58     private List<BundleProperty> createBundleProperties(final ByteBuf message) {
59         List<BundleProperty> properties = new ArrayList<>();
60         while (message.readableBytes() > 0) {
61             BundlePropertyType type = BundlePropertyType.forValue(message.readUnsignedShort());
62             int length = message.readUnsignedShort();
63             if (type != null && type.equals(BundlePropertyType.ONFETBPTEXPERIMENTER)) {
64                 properties.add(createExperimenterBundleProperty(length, message));
65             } else {
66                 message.skipBytes(length);
67             }
68         }
69         return properties;
70     }
71
72     private BundleProperty createExperimenterBundleProperty(final int length, final ByteBuf message) {
73         BundleExperimenterPropertyBuilder experimenterProperty = new BundleExperimenterPropertyBuilder();
74         long experimenterId = message.readUnsignedInt();
75         long expType = message.readUnsignedInt();
76         experimenterProperty.setExperimenter(new ExperimenterId(experimenterId));
77         experimenterProperty.setExpType(expType);
78
79         OFDeserializer<BundleExperimenterPropertyData> deserializer = deserializerRegistry.getDeserializer(
80                 ExperimenterDeserializerKeyFactory.createBundlePropertyDeserializerKey(EncodeConstants.OF13_VERSION_ID,
81                         experimenterId, expType));
82         experimenterProperty.setBundleExperimenterPropertyData(deserializer.deserialize(message.readBytes(length - 12)));
83
84         return new BundlePropertyBuilder().setType(BundlePropertyType.ONFETBPTEXPERIMENTER)
85                 .setBundlePropertyEntry(experimenterProperty.build())
86                 .build();
87     }
88
89     @Override
90     public void injectDeserializerRegistry(DeserializerRegistry deserializerRegistry) {
91         this.deserializerRegistry = deserializerRegistry;
92     }
93
94 }
95
96