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 java.util.Collection;
17 import java.util.Collections;
18 import java.util.Optional;
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.TemplateGenerated;
24 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;
25 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;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Purpose: query ise in order to get name of sgt for given tenant and build {@link EndpointPolicyTemplateBySgt}
33 public class EPPolicyTemplateProviderIseImpl implements EPPolicyTemplateProviderFacade {
35 private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateProviderIseImpl.class);
37 private Optional<IseContext> iseContext = Optional.empty();
38 private GbpIseSgtHarvester iseSgtHarvester;
41 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> provideTemplate(@Nonnull final Sgt sgt) {
42 return findIseSourceConfigBySgt(sgt)
43 .map(iseContext -> queryIseAndBuildTemplate(iseContext, sgt))
44 .orElse(Futures.immediateFuture(Optional.empty()));
47 private ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> queryIseAndBuildTemplate(final IseContext iseContext, final Sgt sgt) {
48 final ListenableFuture<Optional<String>> sgtNameFu = queryIseOnSgt(iseContext, sgt);
49 return Futures.transform(sgtNameFu, new Function<Optional<String>, Optional<EndpointPolicyTemplateBySgt>>() {
52 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<String> input) {
53 return Optional.ofNullable(input)
54 .flatMap(i -> i.map(sgtName -> buildTemplate(sgt, iseContext.getIseSourceConfig().getTenant(), sgtName)));
59 private EndpointPolicyTemplateBySgt buildTemplate(final @Nonnull Sgt sgt, final @Nonnull TenantId tenantId,
60 final @Nonnull String sgtName) {
61 return new EndpointPolicyTemplateBySgtBuilder()
63 .setEndpointGroups(Collections.singletonList(new EndpointGroupId(sgtName)))
66 .setOrigin(TemplateGenerated.class)
70 private ListenableFuture<Optional<String>> queryIseOnSgt(final IseContext iseContext, @Nonnull final Sgt sgt) {
71 final ListenableFuture<Collection<SgtInfo>> sgtUpdateFu = iseSgtHarvester.harvestAll(iseContext);
73 Futures.addCallback(sgtUpdateFu, new FutureCallback<Collection<SgtInfo>>() {
75 public void onSuccess(@Nullable final Collection<SgtInfo> result) {
76 final Integer amount = Optional.ofNullable(result).map(Collection::size).orElse(0);
77 LOG.debug("[epPolicyTemplateProvider] harvestAll succeeded: {}", amount);
81 public void onFailure(final Throwable t) {
82 LOG.debug("[epPolicyTemplateProvider] harvestAll FAILED: {}", t.getMessage());
86 return Futures.transform(sgtUpdateFu, new Function<Collection<SgtInfo>, Optional<String>>() {
89 public Optional<String> apply(@Nullable final Collection<SgtInfo> input) {
90 // pick first sgtInfo which equals to given sgt
91 return Optional.ofNullable(input)
92 .flatMap(safeInput -> safeInput.stream()
93 .filter(sgtInfo -> sgt.equals(sgtInfo.getSgt())).findFirst()
94 .map(SgtInfo::getName));
99 private Optional<IseContext> findIseSourceConfigBySgt(final Sgt sgt) {
100 // expected relation (ise : tenant) == (1:1)
103 context.getIseSourceConfig() != null && Range.closed(
104 context.getIseSourceConfig().getSgtRangeMin().getValue(),
105 context.getIseSourceConfig().getSgtRangeMax().getValue()
106 ).contains(sgt.getValue())
111 public void assignIseContext(final @Nullable IseContext iseContext) {
112 this.iseContext = Optional.ofNullable(iseContext);
116 public void setIseSgtHarvester(final GbpIseSgtHarvester iseSgtHarvester) {
117 this.iseSgtHarvester = iseSgtHarvester;