Bug 6674 - the key of the serialization function registered by the vendor is not... 70/45770/1
authoryunyunhan <han.yunyun@zte.com.cn>
Fri, 9 Sep 2016 17:34:15 +0000 (01:34 +0800)
committeryunyunhan <han.yunyun@zte.com.cn>
Sun, 18 Sep 2016 01:07:28 +0000 (01:07 +0000)
Change-Id: Ibe9c63f03850730c7d7346820d37786d6711f861
Signed-off-by: yunyunhan <han.yunyun@zte.com.cn>
(cherry picked from commit f93ddeaf5e4f25a0a47438786777d13c9b95e46d)

openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/ExperimenterIdMeterSubTypeSerializerKey.java [new file with mode: 0755]
openflow-protocol-api/src/main/yang/openflow-augments.yang [changed mode: 0644->0755]
openflow-protocol-api/src/main/yang/openflow-types.yang [changed mode: 0644->0755]
openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/serialization/factories/MeterModInputMessageFactory.java [changed mode: 0644->0755]
openflowjava-util/src/main/java/org/opendaylight/openflowjava/util/ExperimenterSerializerKeyFactory.java [changed mode: 0644->0755]
openflowjava-util/src/test/java/org/opendaylight/openflowjava/util/ExperimenterSerializerKeyFactoryTest.java [changed mode: 0644->0755]

diff --git a/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/ExperimenterIdMeterSubTypeSerializerKey.java b/openflow-protocol-api/src/main/java/org/opendaylight/openflowjava/protocol/api/keys/ExperimenterIdMeterSubTypeSerializerKey.java
new file mode 100755 (executable)
index 0000000..f568a02
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2016 ZTE, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.openflowjava.protocol.api.keys;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
+
+/**
+ * Created by hyy on 2016/9/8.
+ */
+public class ExperimenterIdMeterSubTypeSerializerKey<T extends DataContainer> extends ExperimenterIdSerializerKey<T> {
+
+    private Class<? extends ExperimenterMeterBandSubType> meterSubType;
+
+    /**
+     * @param msgVersion      protocol wire version
+     * @param experimenterId  experimenter / vendor ID
+     * @param objectClass     class of object to be serialized
+     * @param meterSubType    vendor defined subtype
+     */
+    public ExperimenterIdMeterSubTypeSerializerKey(short msgVersion, long experimenterId,
+                                                   Class<T> objectClass, Class<? extends ExperimenterMeterBandSubType> meterSubType) {
+        super(msgVersion, experimenterId, objectClass);
+        this.meterSubType = meterSubType;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + ((meterSubType == null) ? 0 : meterSubType.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        ExperimenterIdMeterSubTypeSerializerKey other = (ExperimenterIdMeterSubTypeSerializerKey) obj;
+        if (meterSubType == null) {
+            if (other.meterSubType != null) {
+                return false;
+            }
+        } else if (!meterSubType.equals(other.meterSubType)) {
+            return false;
+        }
+        return true;
+    }
+
+}
old mode 100644 (file)
new mode 100755 (executable)
index e5be7e5..4a60c37
          leaf experimenter {
              type oft:experimenter-id;
          }
+         leaf sub-type {
+             type identityref {
+                 base oft:experimenter-meter-band-sub-type;
+             }
+         }
      }
 }
old mode 100644 (file)
new mode 100755 (executable)
index 251a845..7d11baf
         description "Base identity for action types";
     }
 
+    identity experimenter-meter-band-sub-type {
+        description "The base identity for vendor's meter bands.";
+    }
+
     typedef metadata {
         type binary;
     }
old mode 100644 (file)
new mode 100755 (executable)
index 770c0cf..36eb64f
@@ -9,9 +9,6 @@
 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
 
 import io.netty.buffer.ByteBuf;
-
-import java.util.List;
-
 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
@@ -19,6 +16,7 @@ import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
 import org.opendaylight.openflowjava.util.ByteBufUtils;
 import org.opendaylight.openflowjava.util.ExperimenterSerializerKeyFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ExperimenterIdMeterBand;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterBandCommons;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
@@ -30,6 +28,10 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.dscp.remark._case.MeterBandDscpRemark;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.experimenter._case.MeterBandExperimenter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.mod.Bands;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
 
 /**
  * Translates MeterMod messages
@@ -39,6 +41,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731
 public class MeterModInputMessageFactory implements OFSerializer<MeterModInput>,
         SerializerRegistryInjector {
 
+    private static final Logger LOG = LoggerFactory
+            .getLogger(MeterModInputMessageFactory.class);
     private static final byte MESSAGE_TYPE = 29;
     private static final short LENGTH_OF_METER_BANDS = 16;
     private static final short PADDING_IN_METER_BAND_DROP = 4;
@@ -81,12 +85,20 @@ public class MeterModInputMessageFactory implements OFSerializer<MeterModInput>,
                 } else if (meterBand instanceof MeterBandExperimenterCase) {
                     MeterBandExperimenterCase experimenterBandCase = (MeterBandExperimenterCase) meterBand;
                     MeterBandExperimenter experimenterBand = experimenterBandCase.getMeterBandExperimenter();
-                    long expId = experimenterBand.getAugmentation(ExperimenterIdMeterBand.class)
-                            .getExperimenter().getValue();
-                    OFSerializer<MeterBandExperimenterCase> serializer = registry.getSerializer(
-                            ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
-                                    EncodeConstants.OF13_VERSION_ID, expId));
-                    serializer.serialize(experimenterBandCase, outBuffer);
+                    ExperimenterIdMeterBand expIdMeterBand = experimenterBand.getAugmentation(ExperimenterIdMeterBand.class);
+                    if (expIdMeterBand != null) {
+                        long expId = expIdMeterBand.getExperimenter().getValue();
+                        Class<? extends ExperimenterMeterBandSubType> meterBandSubType = expIdMeterBand.getSubType();
+                        try {
+                            OFSerializer<MeterBandExperimenterCase> serializer = registry.getSerializer(
+                                    ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
+                                            EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType));
+                            serializer.serialize(experimenterBandCase, outBuffer);
+                        } catch (final IllegalStateException e) {
+                            LOG.warn("Serializer for key: {} wasn't found, exception {}", ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
+                                    EncodeConstants.OF13_VERSION_ID, expId, meterBandSubType), e);
+                        }
+                    }
                 }
             }
         }
old mode 100644 (file)
new mode 100755 (executable)
index edc6f25..0155893
@@ -8,8 +8,10 @@
 
 package org.opendaylight.openflowjava.util;
 
+import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdMeterSubTypeSerializerKey;
 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdSerializerKey;
 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdTypeSerializerKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
@@ -59,6 +61,11 @@ public abstract class ExperimenterSerializerKeyFactory {
      */
     public static ExperimenterIdSerializerKey<MeterBandExperimenterCase> createMeterBandSerializerKey(
             short msgVersion, long experimenterId) {
-        return new ExperimenterIdSerializerKey<>(msgVersion, experimenterId, MeterBandExperimenterCase.class);
+        return new ExperimenterIdMeterSubTypeSerializerKey<>(msgVersion, experimenterId, MeterBandExperimenterCase.class, null);
+    }
+
+    public static ExperimenterIdSerializerKey<MeterBandExperimenterCase> createMeterBandSerializerKey(
+            short msgVersion, long experimenterId, Class<? extends ExperimenterMeterBandSubType> meterSubType) {
+        return new ExperimenterIdMeterSubTypeSerializerKey<>(msgVersion, experimenterId, MeterBandExperimenterCase.class, meterSubType);
     }
 }
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 211186d..471e413
@@ -10,9 +10,11 @@ package org.opendaylight.openflowjava.util;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdMeterSubTypeSerializerKey;
 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdSerializerKey;
 import org.opendaylight.openflowjava.protocol.api.keys.ExperimenterIdTypeSerializerKey;
 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterMeterBandSubType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandExperimenterCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.table.features.properties.grouping.TableFeatureProperties;
@@ -71,4 +73,32 @@ public class ExperimenterSerializerKeyFactoryTest {
                 43L, MeterBandExperimenterCase.class);
         Assert.assertEquals("Wrong key created", comparationKey, createdKey);
     }
+
+    @Test
+    public void testCreateMeterBandSubTypeSerializerKey() throws Exception {
+        ExperimenterIdSerializerKey<?> createdKey;
+        ExperimenterIdSerializerKey<?> comparationKey1;
+        ExperimenterIdSerializerKey<?> comparationKey2;
+        ExperimenterIdSerializerKey<?> comparationKey3;
+        ExperimenterIdSerializerKey<?> comparationKey4;
+        ExperimenterIdSerializerKey<?> comparationKey5;
+
+        createdKey = ExperimenterSerializerKeyFactory.createMeterBandSerializerKey(
+                EncodeConstants.OF10_VERSION_ID, 43L, ExperimenterMeterBandSubType.class);
+        comparationKey1 = new ExperimenterIdMeterSubTypeSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
+                43L, MeterBandExperimenterCase.class, ExperimenterMeterBandSubType.class);
+        comparationKey2 = new ExperimenterIdMeterSubTypeSerializerKey<>(EncodeConstants.OF10_VERSION_ID,
+                42L, MeterBandExperimenterCase.class, ExperimenterMeterBandSubType.class);
+        comparationKey3 = new ExperimenterIdMeterSubTypeSerializerKey<>(EncodeConstants.OF10_VERSION_ID,
+                43L, null, ExperimenterMeterBandSubType.class);
+        comparationKey4 = new ExperimenterIdMeterSubTypeSerializerKey<>(EncodeConstants.OF10_VERSION_ID,
+                43L, MeterBandExperimenterCase.class, null);
+        comparationKey5 = new ExperimenterIdMeterSubTypeSerializerKey<>(EncodeConstants.OF10_VERSION_ID,
+                43L, MeterBandExperimenterCase.class, ExperimenterMeterBandSubType.class);
+        Assert.assertNotEquals("Wrong key created", comparationKey1, createdKey);
+        Assert.assertNotEquals("Wrong key created", comparationKey2, createdKey);
+        Assert.assertNotEquals("Wrong key created", comparationKey3, createdKey);
+        Assert.assertNotEquals("Wrong key created", comparationKey4, createdKey);
+        Assert.assertEquals("Wrong key created", comparationKey5, createdKey);
+    }
 }
\ No newline at end of file