From 3de41b2429269dd3c79d286d136da667f2f143bc Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Thu, 14 Apr 2016 15:22:06 +0200 Subject: HONEYCOMB-38: Fix vpp interface customizers The order of execution was not correct + CompositeWriter ignored some changes Change-Id: I53fd9fda4b7a0379e0fa8451fa894865f67ebace Signed-off-by: Maros Marsalek Signed-off-by: Marek Gradzki --- .../v3po/interfaces/InterfaceCustomizer.java | 94 ++++++++++++++++++++-- 1 file changed, 86 insertions(+), 8 deletions(-) (limited to 'v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java') diff --git a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java index 6b91e2e0a..30600d81e 100644 --- a/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java +++ b/v3po/v3po2vpp/src/main/java/io/fd/honeycomb/v3po/translate/v3po/interfaces/InterfaceCustomizer.java @@ -19,6 +19,9 @@ package io.fd.honeycomb.v3po.translate.v3po.interfaces; import io.fd.honeycomb.v3po.translate.Context; import io.fd.honeycomb.v3po.translate.spi.write.ListWriterCustomizer; import io.fd.honeycomb.v3po.translate.v3po.util.VppApiCustomizer; +import io.fd.honeycomb.v3po.translate.v3po.util.VppApiInvocationException; +import io.fd.honeycomb.v3po.translate.v3po.utils.V3poUtils; +import io.fd.honeycomb.v3po.translate.write.WriteFailedException; import java.util.List; import javax.annotation.Nonnull; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.Interfaces; @@ -27,14 +30,15 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces. import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.openvpp.vppjapi.vppApi; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Ietf interface write customizer that only caches interface objects for child writers */ public class InterfaceCustomizer extends VppApiCustomizer implements ListWriterCustomizer { - public static final String IFC_AFTER_CTX = InterfaceCustomizer.class.toString() + "ifcAfter"; - public static final String IFC_BEFORE_CTX = InterfaceCustomizer.class.toString() + "ifcBefore"; + private static final Logger LOG = LoggerFactory.getLogger(InterfaceCustomizer.class); public InterfaceCustomizer(final vppApi vppApi) { super(vppApi); @@ -43,24 +47,38 @@ public class InterfaceCustomizer extends VppApiCustomizer implements ListWriterC @Override public void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final Interface dataAfter, - @Nonnull final Context writeContext) { - writeContext.put(IFC_AFTER_CTX, dataAfter); + @Nonnull final Context writeContext) + throws WriteFailedException { + + try { + setInterface(id, dataAfter); + } catch (VppApiInvocationException e) { + LOG.warn("Update of VppInterfaceAugment failed", e); + throw new WriteFailedException.CreateFailedException(id, dataAfter, e); + } } @Override public void updateCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final Interface dataBefore, @Nonnull final Interface dataAfter, - @Nonnull final Context writeContext) { - writeContext.put(IFC_BEFORE_CTX, dataBefore); - writeContext.put(IFC_AFTER_CTX, dataAfter); + @Nonnull final Context writeContext) + throws WriteFailedException.UpdateFailedException { + + try { + updateInterface(id, dataBefore, dataAfter); + } catch (VppApiInvocationException e) { + LOG.warn("Update of VppInterfaceAugment failed", e); + throw new WriteFailedException.UpdateFailedException(id, dataBefore, dataAfter, e); + } } @Override public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final Interface dataBefore, @Nonnull final Context writeContext) { - writeContext.put(IFC_BEFORE_CTX, dataBefore); + + // TODO Handle deletes } @Nonnull @@ -69,4 +87,64 @@ public class InterfaceCustomizer extends VppApiCustomizer implements ListWriterC @Nonnull final DataObject parentData) { return ((Interfaces) parentData).getInterface(); } + + + private void setInterface(final InstanceIdentifier id, final Interface swIf) + throws VppApiInvocationException, WriteFailedException { + LOG.info("Setting interface {}, type: {}", swIf.getName(), swIf.getType().getSimpleName()); + LOG.debug("Setting interface {}", swIf); + + String swIfName = swIf.getName(); + int swIfIndex = getVppApi().swIfIndexFromName(swIfName); + + setInterfaceAttributes(swIf, swIfName); + } + + private void setInterfaceAttributes(final Interface swIf, final String swIfName) + throws VppApiInvocationException { + LOG.debug("Creating {} interface {}", swIf.getType().getSimpleName(), swIf.getName()); + + setInterfaceFlags(swIfName, getVppApi().swIfIndexFromName(swIfName), + swIf.isEnabled() ? (byte) 1 : (byte) 0); + + setDescription(swIf); + } + + private void updateInterface(final InstanceIdentifier id, + final Interface dataBefore, + final Interface dataAfter) throws VppApiInvocationException { + LOG.info("Updating interface {}, type: {}", dataAfter.getName(), dataAfter.getType().getSimpleName()); + LOG.debug("Updating interface {}", dataAfter); + + String swIfName = dataAfter.getName(); + int swIfIndex = getVppApi().swIfIndexFromName(swIfName); + + setInterfaceAttributes(dataAfter, swIfName); + } + + private void setInterfaceFlags(final String swIfName, final int swIfIndex, final byte enabled) + throws VppApiInvocationException { + int ctxId = getVppApi().swInterfaceSetFlags(swIfIndex, enabled, enabled, (byte) 0 /* deleted */); + + LOG.debug("Updating interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex, + enabled, ctxId); + + final int rv = V3poUtils.waitForResponse(ctxId, getVppApi()); + if (rv < 0) { + LOG.warn("Failed to update interface flags for: {}, index: {}, enabled: {}, ctxId: {}", swIfName, swIfIndex, + enabled, ctxId); + throw new VppApiInvocationException("swInterfaceSetFlags", ctxId, rv); + } else { + LOG.debug("Interface flags updated successfully for: {}, index: {}, enabled: {}, ctxId: {}", + swIfName, swIfIndex, enabled, ctxId); + } + } + + private void setDescription(final Interface swIf) { + if (swIf.getDescription() != null) { + getVppApi().setInterfaceDescription(swIf.getName(), swIf.getDescription()); + } else { + getVppApi().setInterfaceDescription(swIf.getName(), ""); + } + } } -- cgit 1.2.3-korg