X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=sxp-integration%2Fsxp-ep-provider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgroupbasedpolicy%2Fsxp%2Fep%2Fprovider%2Fimpl%2Futil%2FEPTemplateUtil.java;fp=sxp-integration%2Fsxp-ep-provider%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fgroupbasedpolicy%2Fsxp%2Fep%2Fprovider%2Fimpl%2Futil%2FEPTemplateUtil.java;h=f7ea755761fe6d24f14de3fc67288292f09bdf76;hb=eace708ec9dd394c5eb7b74c46d1176d82e775e7;hp=0000000000000000000000000000000000000000;hpb=660f6fb82d48f2c43b92cd273593d49e3644bb41;p=groupbasedpolicy.git diff --git a/sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/util/EPTemplateUtil.java b/sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/util/EPTemplateUtil.java new file mode 100644 index 000000000..f7ea75576 --- /dev/null +++ b/sxp-integration/sxp-ep-provider/src/main/java/org/opendaylight/groupbasedpolicy/sxp/ep/provider/impl/util/EPTemplateUtil.java @@ -0,0 +1,136 @@ +/* + * 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.groupbasedpolicy.sxp.ep.provider.impl.util; + +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; +import com.google.common.base.Optional; +import com.google.common.collect.Ordering; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.apache.commons.lang3.tuple.MutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.net.util.SubnetUtils; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointForwardingTemplateBySubnet; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt; + +/** + * Purpose: util methods for {@link EndpointForwardingTemplateBySubnet} and {@link EndpointPolicyTemplateBySgt} + */ +public final class EPTemplateUtil { + + public static final String FULL_IPV4_MASK_SUFFIX = "/32"; + private static final Comparable EMPTY_COMPARABLE = ""; + + private EPTemplateUtil() { + throw new IllegalAccessError("constructing util class"); + } + + public static boolean isPlain(final IpPrefix key) { + return key.getIpv4Prefix().getValue().endsWith(FULL_IPV4_MASK_SUFFIX); + } + + public static SubnetInfoKeyDecorator buildSubnetInfoKey(@Nonnull final IpPrefix value) { + return new SubnetInfoKeyDecorator(new SubnetUtils(value.getIpv4Prefix().getValue()).getInfo()); + } + + public static ListenableFuture> compositeRead( + final ListenableFuture> leftRead, final ListenableFuture> rightRead) { + + final OptionalMutablePair compositeResult = new OptionalMutablePair<>(); + final List> results = new ArrayList<>(2); + + results.add(Futures.transform(leftRead, new Function, OptionalMutablePair>() { + @Nullable + @Override + public OptionalMutablePair apply(@Nullable final Optional input) { + compositeResult.setLeft(input); + return compositeResult; + } + })); + + results.add(Futures.transform(rightRead, new Function, OptionalMutablePair>() { + @Nullable + @Override + public OptionalMutablePair apply(@Nullable final Optional input) { + compositeResult.setRight(input); + return compositeResult; + } + })); + + return Futures.transform(Futures.successfulAsList(results), + new Function, OptionalMutablePair>() { + @Nullable + @Override + public OptionalMutablePair apply(@Nullable final List input) { + return compositeResult; + } + }); + } + + public static ListenableFuture> wrapToPair( + final K keyItem, + final ListenableFuture> valueFromRead) { + return Futures.transform(valueFromRead, new Function, Pair>() { + @Nullable + @Override + public Pair apply(@Nullable final Optional input) { + return new MutablePair<>(keyItem, input.orNull()); + } + }); + } + + public static ListenableFuture> wrapToOptional(final ListenableFuture value) { + return Futures.transform(value, new Function>() { + @Nullable + @Override + public Optional apply(@Nullable final V input) { + return Optional.fromNullable(input); + } + }); + } + + public static Ordering createEndpointGroupIdOrdering() { + return Ordering.natural().onResultOf(new Function() { + @Nullable + @Override + public Comparable apply(@Nullable final EndpointGroupId input) { + if (input == null) { + return EMPTY_COMPARABLE; + } + return MoreObjects.firstNonNull(input.getValue(), EMPTY_COMPARABLE); + } + }); + } + + public static Ordering createConditionNameOrdering() { + return Ordering.natural().onResultOf(new Function() { + @Nullable + @Override + public Comparable apply(@Nullable final ConditionName input) { + if (input == null) { + return EMPTY_COMPARABLE; + } + return MoreObjects.firstNonNull(input.getValue(), EMPTY_COMPARABLE); + } + }); + } + + public static class OptionalMutablePair extends MutablePair, Optional> { + public OptionalMutablePair() { + super(Optional.absent(), Optional.absent()); + } + } +}