diff options
Diffstat (limited to 'mpls/impl')
-rw-r--r-- | mpls/impl/asciidoc/Readme.adoc | 8 | ||||
-rw-r--r-- | mpls/impl/pom.xml | 77 | ||||
-rw-r--r-- | mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java | 114 | ||||
-rw-r--r-- | mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsModule.java | 40 | ||||
-rw-r--r-- | mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java | 63 | ||||
-rw-r--r-- | mpls/impl/src/test/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizerTest.java | 113 |
6 files changed, 415 insertions, 0 deletions
diff --git a/mpls/impl/asciidoc/Readme.adoc b/mpls/impl/asciidoc/Readme.adoc new file mode 100644 index 000000000..22b60cb7e --- /dev/null +++ b/mpls/impl/asciidoc/Readme.adoc @@ -0,0 +1,8 @@ += mpls-impl + +Translates +/ietf-routing:routing/ietf-mpls:mpls/interface + +to + +sw_interface_set_mpls_enable.
\ No newline at end of file diff --git a/mpls/impl/pom.xml b/mpls/impl/pom.xml new file mode 100644 index 000000000..4005e6368 --- /dev/null +++ b/mpls/impl/pom.xml @@ -0,0 +1,77 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + Copyright (c) 2017 Cisco and/or its affiliates. + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at: + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-impl-parent</artifactId> + <version>1.18.01-SNAPSHOT</version> + <relativePath>../../vpp-common/vpp-impl-parent</relativePath> + </parent> + + <groupId>io.fd.hc2vpp.mpls</groupId> + <artifactId>mpls-impl</artifactId> + <name>${project.artifactId}</name> + <version>1.18.01-SNAPSHOT</version> + + <dependencies> + <!-- Honeycomb infrastructure --> + <dependency> + <groupId>io.fd.honeycomb</groupId> + <artifactId>translate-api</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>io.fd.honeycomb</groupId> + <artifactId>translate-impl</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>io.fd.honeycomb</groupId> + <artifactId>translate-spi</artifactId> + <version>${project.version}</version> + </dependency> + <!-- MPLS api --> + <dependency> + <groupId>io.fd.hc2vpp.mpls</groupId> + <artifactId>mpls-api</artifactId> + <version>${project.version}</version> + </dependency> + <!-- Translation --> + <dependency> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-translate-utils</artifactId> + </dependency> + <!-- DI --> + <dependency> + <groupId>com.google.inject</groupId> + <artifactId>guice</artifactId> + </dependency> + <dependency> + <groupId>com.google.inject.extensions</groupId> + <artifactId>guice-multibindings</artifactId> + </dependency> + <dependency> + <groupId>io.fd.hc2vpp.common</groupId> + <artifactId>vpp-translate-test</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + </dependencies> +</project>
\ No newline at end of file diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java new file mode 100644 index 000000000..718a69924 --- /dev/null +++ b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.mpls; + +import static com.google.common.base.Preconditions.checkArgument; +import static java.util.Objects.requireNonNull; + +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.hc2vpp.common.translate.util.FutureJVppCustomizer; +import io.fd.hc2vpp.common.translate.util.JvppReplyConsumer; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.spi.write.ListWriterCustomizer; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.vpp.jvpp.core.dto.MplsTableAddDel; +import io.fd.vpp.jvpp.core.dto.SwInterfaceSetMplsEnable; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.InterfaceKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class MplsInterfaceCustomizer extends FutureJVppCustomizer + implements ListWriterCustomizer<Interface, InterfaceKey>, JvppReplyConsumer, ByteDataTranslator { + + private static final Logger LOG = LoggerFactory.getLogger(MplsInterfaceCustomizer.class); + + private final NamingContext ifcContext; + + MplsInterfaceCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull final NamingContext ifcContext) { + super(vppApi); + this.ifcContext = requireNonNull(ifcContext, "ifcContext should not be null"); + } + + @Override + public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id, + @Nonnull final Interface ifc, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + final String swIfName = ifc.getName(); + final int swIfIndex = ifcContext.getIndex(swIfName, writeContext.getMappingContext()); + checkArgument(ifc.getConfig() != null, "MPLS interface configuration missing"); + final Boolean enabled = ifc.getConfig().isEnabled(); + LOG.debug("Configuring MPLS on interface {}(id={}): enabled={}", swIfName, swIfIndex, enabled); + + // The MPLS default table must also be explicitly created via the API before we enable it on interface + // If table already exists, request is ignored by VPP. + // In future, it might be useful to implement MPLS table management (HC2VPP-260). + createDefaultMplsTable(id); + + // ietf-mpls does not define config node as mandatory child of MPLS interface list + setInterfaceMplsState(id, swIfName, swIfIndex, enabled); + + LOG.debug("MPLS successfully configured on interface {}(id={})", swIfName, swIfIndex); + } + + @Override + public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id, + @Nonnull final Interface dataBefore, + @Nonnull final Interface dataAfter, @Nonnull final WriteContext writeContext) + throws WriteFailedException { + writeCurrentAttributes(id, dataAfter, writeContext); + } + + @Override + public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Interface> id, + @Nonnull final Interface ifc, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + final String swIfName = ifc.getName(); + final int swIfIndex = ifcContext.getIndex(swIfName, writeContext.getMappingContext()); + LOG.debug("Disabling MPLS on interface {}(id={})", swIfName, swIfIndex); + + // Map delete to MPLS disable regardless of previous MPLS config: + setInterfaceMplsState(id, swIfName, swIfIndex, false); + + LOG.debug("MPLS successfully disabled on interface {}(id={})", swIfName, swIfIndex); + } + + private void createDefaultMplsTable(@Nonnull final InstanceIdentifier<Interface> id) throws WriteFailedException { + final MplsTableAddDel request = new MplsTableAddDel(); + // Map delete to MPLS disable regardless of previous MPLS config: + request.mtIsAdd = 1; + request.mtTableId = 0; + request.mtName = new byte[0]; + LOG.trace("Creating default MPLS table", request); + getReplyForWrite(getFutureJVpp().mplsTableAddDel(request).toCompletableFuture(), id); + + } + + private void setInterfaceMplsState(@Nonnull final InstanceIdentifier<Interface> id, @Nonnull final String swIfName, + final int swIfIndex, final boolean enabled) throws WriteFailedException { + final SwInterfaceSetMplsEnable request = new SwInterfaceSetMplsEnable(); + // Map delete to MPLS disable regardless of previous MPLS config: + request.enable = booleanToByte(enabled); + request.swIfIndex = swIfIndex; + LOG.trace("Updating MPLS flag for interface {}(id={}): {}", swIfName, swIfIndex, request); + getReplyForWrite(getFutureJVpp().swInterfaceSetMplsEnable(request).toCompletableFuture(), id); + } +} diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsModule.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsModule.java new file mode 100644 index 000000000..4ada54ec1 --- /dev/null +++ b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsModule.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.mpls; + +import com.google.inject.AbstractModule; +import com.google.inject.multibindings.Multibinder; +import io.fd.honeycomb.translate.write.WriterFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class MplsModule extends AbstractModule { + + private static final Logger LOG = LoggerFactory.getLogger(MplsModule.class); + + @Override + protected void configure() { + LOG.info("Installing MPLS module"); + + LOG.info("Injecting MPLS writers"); + final Multibinder<WriterFactory> writerFactoryBinder = + Multibinder.newSetBinder(binder(), WriterFactory.class); + writerFactoryBinder.addBinding().to(MplsWriterFactory.class); + + LOG.info("MPLS module successfully configured"); + } +} diff --git a/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java new file mode 100644 index 000000000..7d420e029 --- /dev/null +++ b/mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.mpls; + +import com.google.common.collect.ImmutableSet; +import com.google.inject.Inject; +import com.google.inject.name.Named; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.impl.write.GenericListWriter; +import io.fd.honeycomb.translate.write.WriterFactory; +import io.fd.honeycomb.translate.write.registry.ModifiableWriterRegistryBuilder; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.Routing1; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.routing.Mpls; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.Routing; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +final class MplsWriterFactory implements WriterFactory { + private static final InstanceIdentifier<org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface> + IFC_ID = + InstanceIdentifier.create(Interfaces.class).child( + org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.Interface.class); + + private static final InstanceIdentifier<Routing> ROUTING_ID = InstanceIdentifier.create(Routing.class); + private static final InstanceIdentifier<Mpls> MPLS_ID = ROUTING_ID.augmentation(Routing1.class).child(Mpls.class); + private static final InstanceIdentifier<Interface> INTERFACE_ID = MPLS_ID.child(Interface.class); + + @Inject + @Named("interface-context") + private NamingContext ifcContext; + @Inject + private FutureJVppCore vppApi; + + @Override + public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) { + // /ietf-routing:routing/ietf-mpls:mpls/interface + // after + // /ietf-interfaces:interfaces/interface + // First enable interface, then configure MPLS: + registry.subtreeAddAfter( + ImmutableSet.of(InstanceIdentifier.create(Interface.class).child( + org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls._interface.Config.class)), + new GenericListWriter<>(INTERFACE_ID, new MplsInterfaceCustomizer(vppApi, ifcContext)), + IFC_ID); + } +} diff --git a/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizerTest.java b/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizerTest.java new file mode 100644 index 000000000..8fa1abf29 --- /dev/null +++ b/mpls/impl/src/test/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizerTest.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2017 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.hc2vpp.mpls; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.fd.hc2vpp.common.test.write.WriterCustomizerTest; +import io.fd.hc2vpp.common.translate.util.ByteDataTranslator; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.vpp.jvpp.core.dto.MplsTableAddDel; +import io.fd.vpp.jvpp.core.dto.MplsTableAddDelReply; +import io.fd.vpp.jvpp.core.dto.SwInterfaceSetMplsEnable; +import io.fd.vpp.jvpp.core.dto.SwInterfaceSetMplsEnableReply; +import io.fd.vpp.jvpp.core.future.FutureJVppCoreFacade; +import org.junit.Test; +import org.mockito.Mock; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.Routing1; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.Interface; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.InterfaceBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls.InterfaceKey; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.interfaces.mpls._interface.ConfigBuilder; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.mpls.rev170702.routing.Mpls; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev140524.Routing; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class MplsInterfaceCustomizerTest extends WriterCustomizerTest implements ByteDataTranslator { + + private static final String IF_NAME = "local0"; + private static final int IF_INDEX = 123; + + private static final Interface MPLS_ENABLED = getInterfaceMpls(true); + private static final Interface MPLS_DISABLED = getInterfaceMpls(false); + + private static final InstanceIdentifier<Mpls> MPLS_ID = InstanceIdentifier.create(Routing.class).augmentation + (Routing1.class).child(Mpls.class); + private static final InstanceIdentifier<Interface> IID = MPLS_ID.child(Interface.class, new InterfaceKey(IF_NAME)); + + @Mock + private FutureJVppCoreFacade jvpp; + private MplsInterfaceCustomizer customizer; + + private static Interface getInterfaceMpls(final boolean enabled) { + final Interface data = new InterfaceBuilder() + .setName(IF_NAME) + .setConfig(new ConfigBuilder() + .setEnabled(enabled) + .build()) + .build(); + return data; + } + + @Override + public void setUpTest() { + final String ifcCtxName = "ifc-test-instance"; + final NamingContext ifcContext = new NamingContext("generatedIfaceName", ifcCtxName); + defineMapping(mappingContext, IF_NAME, IF_INDEX, ifcCtxName); + customizer = new MplsInterfaceCustomizer(jvpp, ifcContext); + when(jvpp.swInterfaceSetMplsEnable(any())).thenReturn(future(new SwInterfaceSetMplsEnableReply())); + when(jvpp.mplsTableAddDel(any())).thenReturn(future(new MplsTableAddDelReply())); + } + + @Test + public void testWrite() throws WriteFailedException { + customizer.writeCurrentAttributes(IID, MPLS_ENABLED, writeContext); + verify(jvpp).mplsTableAddDel(getMplsTableRequest()); + verify(jvpp).swInterfaceSetMplsEnable(getInterfaceMplsRequest(true)); + } + + @Test + public void testUpdate() throws WriteFailedException { + customizer.updateCurrentAttributes(IID, MPLS_ENABLED, MPLS_DISABLED, writeContext); + verify(jvpp).mplsTableAddDel(getMplsTableRequest()); + verify(jvpp).swInterfaceSetMplsEnable(getInterfaceMplsRequest(false)); + } + + @Test + public void testDelete() throws WriteFailedException { + customizer.deleteCurrentAttributes(IID, MPLS_ENABLED, writeContext); + verify(jvpp).swInterfaceSetMplsEnable(getInterfaceMplsRequest(false)); + } + + private MplsTableAddDel getMplsTableRequest() { + final MplsTableAddDel request = new MplsTableAddDel(); + request.mtIsAdd = 1; + request.mtTableId = 0; + request.mtName = new byte[0]; + return request; + } + + private SwInterfaceSetMplsEnable getInterfaceMplsRequest(final boolean enable) { + final SwInterfaceSetMplsEnable request = new SwInterfaceSetMplsEnable(); + request.enable = booleanToByte(enable); + request.swIfIndex = IF_INDEX; + return request; + } +}
\ No newline at end of file |