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.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Optional;
18 import javax.annotation.Nonnull;
19 import javax.annotation.Nullable;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.TemplateGenerated;
23 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;
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.EndpointPolicyTemplateBySgtBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Purpose: query ise in order to get name of sgt for given tenant and build {@link EndpointPolicyTemplateBySgt}
32 public class EPPolicyTemplateProviderIseImpl implements EPPolicyTemplateProviderFacade {
34 private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateProviderIseImpl.class);
36 private Optional<IseContext> iseContext = Optional.empty();
37 private GbpIseSgtHarvester iseSgtHarvester;
40 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> provideTemplate(@Nonnull final Sgt sgt) {
41 return findIseSourceConfigBySgt(sgt)
42 .map(iseContext -> queryIseAndBuildTemplate(iseContext, sgt))
43 .orElse(Futures.immediateFuture(Optional.empty()));
46 private ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> queryIseAndBuildTemplate(final IseContext iseContext, final Sgt sgt) {
47 final ListenableFuture<Optional<String>> sgtNameFu = queryIseOnSgt(iseContext, sgt);
48 return Futures.transform(sgtNameFu, new Function<Optional<String>, Optional<EndpointPolicyTemplateBySgt>>() {
51 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Optional<String> input) {
52 return Optional.ofNullable(input)
53 .flatMap(i -> i.map(sgtName -> buildTemplate(sgt, iseContext.getIseSourceConfig().getTenant(), sgtName)));
58 private EndpointPolicyTemplateBySgt buildTemplate(final @Nonnull Sgt sgt, final @Nonnull TenantId tenantId,
59 final @Nonnull String sgtName) {
60 return new EndpointPolicyTemplateBySgtBuilder()
62 .setEndpointGroups(Collections.singletonList(new EndpointGroupId(sgtName)))
65 .setOrigin(TemplateGenerated.class)
69 private ListenableFuture<Optional<String>> queryIseOnSgt(final IseContext iseContext, @Nonnull final Sgt sgt) {
70 final ListenableFuture<Collection<SgtInfo>> sgtUpdateFu = iseSgtHarvester.harvestAll(iseContext);
71 return Futures.transform(sgtUpdateFu, new Function<Collection<SgtInfo>, Optional<String>>() {
74 public Optional<String> apply(@Nullable final Collection<SgtInfo> input) {
75 // pick first sgtInfo which equals to given sgt
76 return Optional.ofNullable(input)
77 .flatMap(safeInput -> safeInput.stream()
78 .filter(sgtInfo -> sgt.equals(sgtInfo.getSgt())).findFirst()
79 .map(SgtInfo::getName));
84 private Optional<IseContext> findIseSourceConfigBySgt(final Sgt sgt) {
85 // expected relation (ise : tenant) == (1:1)
88 Range.closed(context.getIseSourceConfig().getSgtRangeMin().getValue(),
89 context.getIseSourceConfig().getSgtRangeMax().getValue())
90 .contains(sgt.getValue())
95 public void assignIseContext(final @Nullable IseContext iseContext) {
96 this.iseContext = Optional.ofNullable(iseContext);
100 public void setIseSgtHarvester(final GbpIseSgtHarvester iseSgtHarvester) {
101 this.iseSgtHarvester = iseSgtHarvester;