BUG-4826: BGP Evpn Nlri's registry 93/37593/4
authorClaudio D. Gasparini <cgaspari@cisco.com>
Tue, 5 Apr 2016 09:27:35 +0000 (11:27 +0200)
committerMilos Fabian <milfabia@cisco.com>
Tue, 19 Apr 2016 16:31:17 +0000 (16:31 +0000)
Implementation of registry for handle Nlri's.

Change-Id: If71959b5c37f5c3b62268c25c1c10469010a4c08
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnParser.java [new file with mode: 0644]
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnRegistry.java [new file with mode: 0644]
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnSerializer.java [new file with mode: 0644]
bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/pojo/SimpleEvpnNlriRegistry.java [new file with mode: 0644]

diff --git a/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnParser.java b/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnParser.java
new file mode 100644 (file)
index 0000000..cbe6cba
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, 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.protocol.bgp.evpn.spi;
+
+import io.netty.buffer.ByteBuf;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
+
+public interface EvpnParser {
+    /**
+     * Decode input buffer to BGP Evpn.
+     *
+     * @param buffer encoded Evpn body in Bytebuf
+     * @return Evpn
+     */
+    EvpnChoice parseEvpn(@Nonnull ByteBuf buffer);
+}
diff --git a/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnRegistry.java b/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnRegistry.java
new file mode 100644 (file)
index 0000000..b3d44fd
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, 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.protocol.bgp.evpn.spi;
+
+import io.netty.buffer.ByteBuf;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+
+public interface EvpnRegistry {
+    /**
+     * Decode input buffer to BGP Evpn.
+     *
+     * @param type Nlri Type
+     * @param buffer encoded EvpnChoice body in Bytebuf
+     * @return EvpnChoice
+     */
+    EvpnChoice parseEvpn(@Nonnull NlriType type, @Nonnull ByteBuf buffer);
+
+    /**
+     * Encode input BGP Evpn to output buffer
+     *
+     * @param evpn EvpnChoice
+     * @param common encoded common Evpn
+     * @return encoded EvpnChoice body in Bytebuf
+     */
+    ByteBuf serializeEvpn(@Nonnull EvpnChoice evpn, @Nonnull ByteBuf common);
+
+    /**
+     * Decode Evpn Model to Evpn.
+     *
+     * @param evpnChoice ChoiceNode containing Evpn
+     * @return EvpnChoice
+     */
+    EvpnChoice serializeEvpnModel(@Nonnull ChoiceNode evpnChoice);
+
+    /**
+     * Create Route key from Evpn model
+     *
+     * @param evpnChoice ChoiceNode containing Evpn
+     * @return EvpnChoice
+     */
+    EvpnChoice serializeEvpnRouteKey(@Nonnull ChoiceNode evpnChoice);
+}
diff --git a/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnSerializer.java b/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/EvpnSerializer.java
new file mode 100644 (file)
index 0000000..7e4e43b
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, 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.protocol.bgp.evpn.spi;
+
+import io.netty.buffer.ByteBuf;
+import javax.annotation.Nonnull;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+
+public interface EvpnSerializer {
+    /**
+     * @param evpn Evpn
+     * @param buffer Encode common Evpn parts to output buffer
+     * @return Encode Evpn to output buffer
+     */
+    ByteBuf serializeEvpn(@Nonnull EvpnChoice evpn, @Nonnull ByteBuf buffer);
+
+    /**
+     * Serialize Evpn
+     *
+     * @param evpn ChoiceNode containing Evpn
+     * @return Evpn
+     */
+    EvpnChoice serializeEvpnModel(@Nonnull ContainerNode evpn);
+
+    /**
+     * create Route key from Evpn model
+     *
+     * @param evpn ContainerNode containing Evpn
+     * @return Evpn
+     */
+    EvpnChoice createRouteKey(@Nonnull ContainerNode evpn);
+}
diff --git a/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/pojo/SimpleEvpnNlriRegistry.java b/bgp/evpn/src/main/java/org/opendaylight/protocol/bgp/evpn/spi/pojo/SimpleEvpnNlriRegistry.java
new file mode 100644 (file)
index 0000000..5da3816
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, 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.protocol.bgp.evpn.spi.pojo;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+import io.netty.buffer.ByteBuf;
+import org.opendaylight.protocol.bgp.evpn.spi.EvpnParser;
+import org.opendaylight.protocol.bgp.evpn.spi.EvpnRegistry;
+import org.opendaylight.protocol.bgp.evpn.spi.EvpnSerializer;
+import org.opendaylight.protocol.concepts.HandlerRegistry;
+import org.opendaylight.protocol.concepts.MultiRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.NlriType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.evpn.rev160321.evpn.EvpnChoice;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+
+public class SimpleEvpnNlriRegistry implements EvpnRegistry {
+    private static final SimpleEvpnNlriRegistry SINGLETON = new SimpleEvpnNlriRegistry();
+    private final HandlerRegistry<DataContainer, EvpnParser, EvpnSerializer> handlers = new HandlerRegistry<>();
+    private final MultiRegistry<NodeIdentifier, EvpnSerializer> modelHandlers = new MultiRegistry<>();
+
+    private SimpleEvpnNlriRegistry() {
+    }
+
+    @FunctionalInterface
+    private interface SerializerInterface {
+        EvpnChoice check(EvpnSerializer serializer, ContainerNode cont);
+    }
+
+    public static SimpleEvpnNlriRegistry getInstance() {
+        return SINGLETON;
+    }
+
+    public AutoCloseable registerNlriParser(final NlriType esiType, final EvpnParser parser) {
+        return this.handlers.registerParser(esiType.getIntValue(), parser);
+    }
+
+    public AutoCloseable registerNlriSerializer(final Class<? extends EvpnChoice> evpnClass, final EvpnSerializer serializer) {
+        return this.handlers.registerSerializer(evpnClass, serializer);
+    }
+
+    public AutoCloseable registerNlriModelSerializer(final QName qName, final EvpnSerializer serializer) {
+        return this.modelHandlers.register(new NodeIdentifier(qName), serializer);
+    }
+
+    @Override
+    public EvpnChoice parseEvpn(final NlriType type, final ByteBuf buffer) {
+        Preconditions.checkArgument(buffer != null && buffer.isReadable(), "Array of bytes is mandatory. Can't be null or empty.");
+        final EvpnParser parser = this.handlers.getParser(type.getIntValue());
+        if (parser == null) {
+            return null;
+        }
+        return parser.parseEvpn(buffer);
+    }
+
+    @Override
+    public ByteBuf serializeEvpn(final EvpnChoice evpn, final ByteBuf common) {
+        final EvpnSerializer serializer = this.handlers.getSerializer(evpn.getImplementedInterface());
+        if (serializer == null) {
+            return common;
+        }
+        return serializer.serializeEvpn(evpn, common);
+    }
+
+    @Override
+    public EvpnChoice serializeEvpnModel(final ChoiceNode evpnChoice) {
+        return getEvpnCase(evpnChoice, (serializer, cont) -> serializer.serializeEvpnModel(cont));
+    }
+
+    @Override
+    public EvpnChoice serializeEvpnRouteKey(final ChoiceNode evpnChoice) {
+        return getEvpnCase(evpnChoice, (serializer, cont) -> serializer.createRouteKey(cont));
+    }
+
+    private EvpnChoice getEvpnCase(final ChoiceNode evpnChoice, final SerializerInterface serializerInterface) {
+        Preconditions.checkArgument(evpnChoice != null && !evpnChoice.getValue().isEmpty(), "Evpn case is mandatory. Can't be null or empty.");
+        final ContainerNode cont = (ContainerNode) Iterables.getOnlyElement(evpnChoice.getValue());
+        final EvpnSerializer serializer = this.modelHandlers.get(cont.getIdentifier());
+        if (serializer == null) {
+            return null;
+        }
+        return serializerInterface.check(serializer, cont);
+    }
+}