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