From 4616f0300655582153362a21910bd1f0b14937ae Mon Sep 17 00:00:00 2001 From: Marek Gradzki Date: Fri, 10 Mar 2017 12:50:53 +0100 Subject: HC2VPP-55: policer&policer assignment CRUD support Change-Id: I627d2a56ab2a282744ea0172b4a0c72240b0032f Signed-off-by: Marek Gradzki --- .../policer/write/InterfacePolicerCustomizer.java | 97 ++++++++++++ .../write/InterfacePolicerWriterFactory.java | 60 ++++++++ .../fd/hc2vpp/policer/write/PolicerCustomizer.java | 165 +++++++++++++++++++++ .../hc2vpp/policer/write/PolicerWriterFactory.java | 52 +++++++ 4 files changed, 374 insertions(+) create mode 100644 vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerCustomizer.java create mode 100644 vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerWriterFactory.java create mode 100644 vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerCustomizer.java create mode 100644 vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerWriterFactory.java (limited to 'vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write') diff --git a/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerCustomizer.java b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerCustomizer.java new file mode 100644 index 000000000..d2c094f0d --- /dev/null +++ b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerCustomizer.java @@ -0,0 +1,97 @@ +/* + * 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.policer.write; + +import static com.google.common.base.Preconditions.checkNotNull; + +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.hc2vpp.vpp.classifier.context.VppClassifierContextManager; +import io.fd.honeycomb.translate.MappingContext; +import io.fd.honeycomb.translate.spi.write.WriterCustomizer; +import io.fd.honeycomb.translate.write.WriteContext; +import io.fd.honeycomb.translate.write.WriteFailedException; +import io.fd.vpp.jvpp.core.dto.PolicerClassifySetInterface; +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.Interface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.policer.rev170315._interface.policer.attributes.Policer; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +final class InterfacePolicerCustomizer extends FutureJVppCustomizer implements WriterCustomizer, + ByteDataTranslator, JvppReplyConsumer { + private static final Logger LOG = LoggerFactory.getLogger(InterfacePolicerCustomizer.class); + + private final NamingContext interfaceContext; + private final VppClassifierContextManager classifyTableContext; + + InterfacePolicerCustomizer(@Nonnull final FutureJVppCore vppApi, @Nonnull final NamingContext interfaceContext, + @Nonnull final VppClassifierContextManager classifyTableContext) { + super(vppApi); + this.interfaceContext = checkNotNull(interfaceContext, "interfaceContext should not be null"); + this.classifyTableContext = checkNotNull(classifyTableContext, "classifyTableContext should not be null");; + } + + @Override + public void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final Policer dataAfter, @Nonnull final WriteContext writeContext) + throws WriteFailedException { + LOG.debug("Applying policer id={}: {} to interface", id, dataAfter); + assignPolicer(id, dataAfter, true, writeContext.getMappingContext()); + } + + @Override + public void updateCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final Policer dataBefore, @Nonnull final Policer dataAfter, + @Nonnull final WriteContext writeContext) throws WriteFailedException { + LOG.debug("Updating policer-interface assignment id={} dataBefore={} dataAfter={}", id, dataBefore, dataAfter); + assignPolicer(id, dataAfter, true, writeContext.getMappingContext()); + } + + @Override + public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final Policer dataBefore, @Nonnull final WriteContext writeContext) + throws WriteFailedException { + LOG.debug("Removing policer-interface assignment id={} dataBefore={}", id, dataBefore); + assignPolicer(id, dataBefore, true, writeContext.getMappingContext()); + } + + private void assignPolicer(final InstanceIdentifier id, final Policer policer, final boolean isAdd, + final MappingContext ctx) throws WriteFailedException { + final PolicerClassifySetInterface request = new PolicerClassifySetInterface(); + request.isAdd = booleanToByte(isAdd); + + request.swIfIndex = interfaceContext.getIndex(id.firstKeyOf(Interface.class).getName(), ctx); + request.ip4TableIndex = ~0; + request.ip6TableIndex = ~0; + request.l2TableIndex = ~0; + if (policer.getL2Table() != null) { + request.l2TableIndex = classifyTableContext.getTableIndex(policer.getL2Table(), ctx); + } + if (policer.getIp4Table() != null) { + request.ip4TableIndex = classifyTableContext.getTableIndex(policer.getIp4Table(), ctx); + } + if (policer.getIp6Table() != null) { + request.ip6TableIndex = classifyTableContext.getTableIndex(policer.getIp6Table(), ctx); + } + getReplyForWrite(getFutureJVpp().policerClassifySetInterface(request).toCompletableFuture(), id); + } +} diff --git a/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerWriterFactory.java b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerWriterFactory.java new file mode 100644 index 000000000..f07998fe2 --- /dev/null +++ b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/InterfacePolicerWriterFactory.java @@ -0,0 +1,60 @@ +/* + * 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.policer.write; + +import static io.fd.hc2vpp.vpp.classifier.factory.write.VppClassifierHoneycombWriterFactory.CLASSIFY_SESSION_ID; +import static io.fd.hc2vpp.vpp.classifier.factory.write.VppClassifierHoneycombWriterFactory.CLASSIFY_TABLE_ID; + +import com.google.common.collect.Sets; +import com.google.inject.Inject; +import com.google.inject.name.Named; +import io.fd.hc2vpp.common.translate.util.NamingContext; +import io.fd.hc2vpp.vpp.classifier.context.VppClassifierContextManager; +import io.fd.honeycomb.translate.impl.write.GenericWriter; +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.interfaces.rev140508.interfaces.Interface; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.policer.rev170315.PolicerInterfaceAugmentation; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang._interface.policer.rev170315._interface.policer.attributes.Policer; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class InterfacePolicerWriterFactory implements WriterFactory { + private static final InstanceIdentifier IFC_ID = + InstanceIdentifier.create(Interfaces.class).child(Interface.class); + private static final InstanceIdentifier POLICER_IFC_ID = + IFC_ID.augmentation(PolicerInterfaceAugmentation.class); + static final InstanceIdentifier POLICER_ID = POLICER_IFC_ID.child(Policer.class); + + @Inject + private FutureJVppCore vppApi; + @Inject + @Named("interface-context") + private NamingContext ifcContext; + @Inject + @Named("classify-table-context") + private VppClassifierContextManager classifyTableContext; + + @Override + public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) { + registry.addAfter( + new GenericWriter<>(POLICER_ID, new InterfacePolicerCustomizer(vppApi, ifcContext, classifyTableContext)), + Sets.newHashSet(CLASSIFY_TABLE_ID, CLASSIFY_SESSION_ID)); + } +} diff --git a/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerCustomizer.java b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerCustomizer.java new file mode 100644 index 000000000..69eff100a --- /dev/null +++ b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerCustomizer.java @@ -0,0 +1,165 @@ +/* + * 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.policer.write; + +import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; + +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.PolicerAddDel; +import io.fd.vpp.jvpp.core.dto.PolicerAddDelReply; +import io.fd.vpp.jvpp.core.future.FutureJVppCore; +import java.nio.charset.StandardCharsets; +import javax.annotation.Nonnull; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.DscpType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionDrop; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionMarkDscp; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionParams; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionTransmit; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.MeterActionType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ConformAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ExceedAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ViolateAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policers.Policer; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policers.PolicerKey; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class PolicerCustomizer extends FutureJVppCustomizer implements ListWriterCustomizer, + JvppReplyConsumer, ByteDataTranslator { + private static final Logger LOG = LoggerFactory.getLogger(PolicerCustomizer.class); + private final NamingContext policerContext; + + public PolicerCustomizer(@Nonnull final FutureJVppCore futureJVppCore, @Nonnull final NamingContext policerContext) { + super(futureJVppCore); + this.policerContext = checkNotNull(policerContext, "policerContext should not be null"); + } + + @Override + public void writeCurrentAttributes(@Nonnull final InstanceIdentifier id, @Nonnull final Policer dataAfter, + @Nonnull final WriteContext ctx) throws WriteFailedException { + LOG.debug("Writing Policer {} dataAfter={}", id, dataAfter); + final int policerIndex = policerAddDel(id, dataAfter, true); + policerContext.addName(policerIndex, dataAfter.getName(), ctx.getMappingContext()); + } + + @Override + public void updateCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final Policer dataBefore, + @Nonnull final Policer dataAfter, @Nonnull final WriteContext ctx) + throws WriteFailedException { + LOG.debug("Updating Policer {} dataBefore={} dataAfter={}", id, dataBefore, dataAfter); + policerAddDel(id, dataAfter, true); + } + + @Override + public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier id, + @Nonnull final Policer dataBefore, + @Nonnull final WriteContext ctx) + throws WriteFailedException { + LOG.debug("Removing Policer {} dataBefore={}", id, dataBefore); + policerAddDel(id, dataBefore, false); + policerContext.removeName(dataBefore.getName(), ctx.getMappingContext()); + } + + private int policerAddDel(final InstanceIdentifier id, final Policer policer, final boolean isAdd) + throws WriteFailedException { + final PolicerAddDel request = new PolicerAddDel(); + request.isAdd = booleanToByte(isAdd); + request.name = policer.getName().getBytes(StandardCharsets.US_ASCII); + + // policer_add_del expects host order unlike most of the other VPP APIs + // jvpp by default converts ordering to network order, so we need additional reverse + if (policer.getCir() != null) { + request.cir = Integer.reverseBytes(policer.getCir().intValue()); + } + if (policer.getEir() != null) { + request.eir = Integer.reverseBytes(policer.getEir().intValue()); + } + if (policer.getCb() != null) { + request.cb = Long.reverseBytes(policer.getCb().longValue()); + } + if (policer.getEb() != null) { + request.eb = Long.reverseBytes(policer.getEb().longValue()); + } + if (policer.getRateType() != null) { + request.rateType = (byte) policer.getRateType().getIntValue(); + } + if (policer.getRoundType() != null) { + request.roundType = (byte) policer.getRoundType().getIntValue(); + } + if (policer.getType() != null) { + request.type = (byte) policer.getType().getIntValue(); + } + request.colorAware = booleanToByte(policer.isColorAware()); + final ConformAction conformAction = policer.getConformAction(); + if (conformAction != null) { + request.conformActionType = parseActiontype(conformAction.getMeterActionType()); + request.conformDscp = parseDscp(conformAction); + } + final ExceedAction exceedAction = policer.getExceedAction(); + if (exceedAction != null) { + request.exceedActionType = parseActiontype(exceedAction.getMeterActionType()); + request.exceedDscp = parseDscp(exceedAction); + } + final ViolateAction violateAction = policer.getViolateAction(); + if (violateAction != null) { + request.violateActionType = parseActiontype(violateAction.getMeterActionType()); + request.violateDscp = parseDscp(violateAction); + } + LOG.debug("Policer config change id={} request={}", id, request); + final PolicerAddDelReply reply = + getReplyForWrite(getFutureJVpp().policerAddDel(request).toCompletableFuture(), id); + return reply.policerIndex; + } + + private byte parseDscp(@Nonnull MeterActionParams actionParams) { + final DscpType dscp = actionParams.getDscp(); + if (dscp == null) { + return 0; + } + final Class meterActionType = actionParams.getMeterActionType(); + checkArgument(MeterActionMarkDscp.class == meterActionType, + "dcsp is supported only for meter-action-mark-dscp, but %s defined", meterActionType); + if (dscp.getVppDscpType() != null) { + return (byte) dscp.getVppDscpType().getIntValue(); + } + if (dscp.getDscp() != null) { + return dscp.getDscp().getValue().byteValue(); + } + return 0; + } + + private byte parseActiontype(@Nonnull final Class meterActionType) { + if (MeterActionDrop.class == meterActionType) { + return 0; + } else if (MeterActionTransmit.class == meterActionType) { + return 1; + } else if (MeterActionMarkDscp.class == meterActionType) { + return 2; + } else { + throw new IllegalArgumentException("Unsupported meter action type " + meterActionType); + } + } +} diff --git a/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerWriterFactory.java b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerWriterFactory.java new file mode 100644 index 000000000..43c2f5ef9 --- /dev/null +++ b/vpp-classifier/impl/src/main/java/io/fd/hc2vpp/policer/write/PolicerWriterFactory.java @@ -0,0 +1,52 @@ +/* + * 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.policer.write; + +import com.google.common.collect.Sets; +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.opendaylight.params.xml.ns.yang.policer.rev170315.Policers; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ConformAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ExceedAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policer.base.attributes.ViolateAction; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.policer.rev170315.policers.Policer; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class PolicerWriterFactory implements WriterFactory { + private static final InstanceIdentifier POLICER_IID = InstanceIdentifier.create(Policers.class).child(Policer.class); + + @Inject + private FutureJVppCore vppApi; + @Inject + @Named("policer-context") + private NamingContext policerContext; + + @Override + public void init(@Nonnull final ModifiableWriterRegistryBuilder registry) { + InstanceIdentifier IID = InstanceIdentifier.create(Policer.class); + registry.subtreeAdd( + Sets.newHashSet(IID.child(ConformAction.class), IID.child(ExceedAction.class), + IID.child(ViolateAction.class)), + new GenericListWriter<>(POLICER_IID, new PolicerCustomizer(vppApi, policerContext))); + } +} -- cgit 1.2.3-korg