2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl;
11 import com.google.common.base.Function;
12 import com.google.common.collect.Range;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.TemplateGenerated;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgtBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Purpose: query ise in order to get name of sgt for given tenant and build {@link EndpointPolicyTemplateBySgt}
35 public class EPPolicyTemplateProviderIseImpl implements EPPolicyTemplateProviderFacade {
37 private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateProviderIseImpl.class);
39 private Optional<IseContext> iseContext = Optional.empty();
40 private GbpIseSgtHarvester iseSgtHarvester;
43 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> provideTemplate(@Nonnull final Sgt sgt) {
44 return findIseSourceConfigBySgt(sgt)
45 .map(iseContext -> queryIseAndBuildTemplate(iseContext, sgt))
46 .orElse(Futures.immediateFuture(Optional.empty()));
49 private ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> queryIseAndBuildTemplate(final IseContext iseContext, final Sgt sgt) {
50 final ListenableFuture<Optional<String>> sgtNameFu = queryIseOnSgt(iseContext, sgt);
51 return Futures.transform(sgtNameFu, new Function<Optional<String>, Optional<EndpointPolicyTemplateBySgt>>() {
54 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<String> input) {
55 return Optional.ofNullable(input)
56 .flatMap(i -> i.map(sgtName -> buildTemplate(sgt, iseContext.getIseSourceConfig().getTenant(), sgtName)));
58 }, MoreExecutors.directExecutor());
61 private EndpointPolicyTemplateBySgt buildTemplate(final @Nonnull Sgt sgt, final @Nonnull TenantId tenantId,
62 final @Nonnull String sgtName) {
63 return new EndpointPolicyTemplateBySgtBuilder()
65 .setEndpointGroups(Collections.singletonList(new EndpointGroupId(sgtName)))
68 .setOrigin(TemplateGenerated.class)
72 private ListenableFuture<Optional<String>> queryIseOnSgt(final IseContext iseContext, @Nonnull final Sgt sgt) {
73 final ListenableFuture<Collection<SgtInfo>> sgtUpdateFu = iseSgtHarvester.harvestAll(iseContext);
75 Futures.addCallback(sgtUpdateFu, new FutureCallback<Collection<SgtInfo>>() {
77 public void onSuccess(@Nullable final Collection<SgtInfo> result) {
78 final Integer amount = Optional.ofNullable(result).map(Collection::size).orElse(0);
79 LOG.debug("[epPolicyTemplateProvider] harvestAll succeeded: {}", amount);
83 public void onFailure(final Throwable t) {
84 LOG.debug("[epPolicyTemplateProvider] harvestAll FAILED: {}", t.getMessage());
86 }, MoreExecutors.directExecutor());
88 return Futures.transform(sgtUpdateFu, new Function<Collection<SgtInfo>, Optional<String>>() {
91 public Optional<String> apply(@Nullable final Collection<SgtInfo> input) {
92 // pick first sgtInfo which equals to given sgt
93 return Optional.ofNullable(input)
94 .flatMap(safeInput -> safeInput.stream()
95 .filter(sgtInfo -> sgt.equals(sgtInfo.getSgt())).findFirst()
96 .map(SgtInfo::getName));
98 }, MoreExecutors.directExecutor());
101 private Optional<IseContext> findIseSourceConfigBySgt(final Sgt sgt) {
102 // expected relation (ise : tenant) == (1:1)
105 context.getIseSourceConfig() != null && Range.closed(
106 context.getIseSourceConfig().getSgtRangeMin().getValue(),
107 context.getIseSourceConfig().getSgtRangeMax().getValue()
108 ).contains(sgt.getValue())
113 public void assignIseContext(final @Nullable IseContext iseContext) {
114 this.iseContext = Optional.ofNullable(iseContext);
118 public void setIseSgtHarvester(final GbpIseSgtHarvester iseSgtHarvester) {
119 this.iseSgtHarvester = iseSgtHarvester;