From baee9a73ebcf4db04d80f454094496a24595e142 Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Thu, 26 Oct 2017 13:44:41 +0200 Subject: HC2VPP-257: MPLS interface management Translates /hc2vpp-ietf-routing:routing/hc2vpp-ietf-mpls:mpls/interface to sw_interface_set_mpls_enable which is equivalent to: set interface mpls [...] enable MPLS table is created before configuring MPLS using mpls_table_add_del (required since VPP 17.10). Reading MPLS configuration state is not supported (VPP API is missing). Change-Id: I3f1b987c3669b0836a27649a711e75d0dc37a779 Signed-off-by: Marek Gradzki --- mpls/impl/asciidoc/Readme.adoc | 8 ++ mpls/impl/pom.xml | 77 ++++++++++++++ .../io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java | 114 +++++++++++++++++++++ .../main/java/io/fd/hc2vpp/mpls/MplsModule.java | 40 ++++++++ .../java/io/fd/hc2vpp/mpls/MplsWriterFactory.java | 63 ++++++++++++ .../hc2vpp/mpls/MplsInterfaceCustomizerTest.java | 113 ++++++++++++++++++++ mpls/pom.xml | 1 + 7 files changed, 416 insertions(+) create mode 100644 mpls/impl/asciidoc/Readme.adoc create mode 100644 mpls/impl/pom.xml create mode 100644 mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizer.java create mode 100644 mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsModule.java create mode 100644 mpls/impl/src/main/java/io/fd/hc2vpp/mpls/MplsWriterFactory.java create mode 100644 mpls/impl/src/test/java/io/fd/hc2vpp/mpls/MplsInterfaceCustomizerTest.java (limited to 'mpls') 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 @@ + + + + 4.0.0 + + + io.fd.hc2vpp.common + vpp-impl-parent + 1.18.01-SNAPSHOT + ../../vpp-common/vpp-impl-parent + + + io.fd.hc2vpp.mpls + mpls-impl + ${project.artifactId} + 1.18.01-SNAPSHOT + + + + + io.fd.honeycomb + translate-api + ${project.version} + + + io.fd.honeycomb + translate-impl + ${project.version} + + + io.fd.honeycomb + translate-spi + ${project.version} + + + + io.fd.hc2vpp.mpls + mpls-api + ${project.version} + + + + io.fd.hc2vpp.common + vpp-translate-utils + + + + com.google.inject + guice + + + com.google.inject.extensions + guice-multibindings + + + io.fd.hc2vpp.common + vpp-translate-test + ${project.version} + test + + + \ 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, 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 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 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 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 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 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 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 + 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_ID = InstanceIdentifier.create(Routing.class); + private static final InstanceIdentifier MPLS_ID = ROUTING_ID.augmentation(Routing1.class).child(Mpls.class); + private static final InstanceIdentifier 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_ID = InstanceIdentifier.create(Routing.class).augmentation + (Routing1.class).child(Mpls.class); + private static final InstanceIdentifier 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 diff --git a/mpls/pom.xml b/mpls/pom.xml index ee5f6467b..52cd9bd85 100644 --- a/mpls/pom.xml +++ b/mpls/pom.xml @@ -34,6 +34,7 @@ api + impl -- cgit 1.2.3-korg