BUG-6858: adapt to ise api, wire harvestAll to template-provider
[groupbasedpolicy.git] / sxp-integration / sxp-ise-adapter / src / main / java / org / opendaylight / groupbasedpolicy / sxp_ise_adapter / impl / EPPolicyTemplateProviderIseImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.sxp_ise_adapter.impl;
10
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;
28
29 /**
30  * Purpose: query ise in order to get name of sgt for given tenant and build {@link EndpointPolicyTemplateBySgt}
31  */
32 public class EPPolicyTemplateProviderIseImpl implements EPPolicyTemplateProviderFacade {
33
34     private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateProviderIseImpl.class);
35
36     private Optional<IseContext> iseContext = Optional.empty();
37     private GbpIseSgtHarvester iseSgtHarvester;
38
39     @Override
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()));
44     }
45
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>>() {
49             @Nullable
50             @Override
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)));
54             }
55         });
56     }
57
58     private EndpointPolicyTemplateBySgt buildTemplate(final @Nonnull Sgt sgt, final @Nonnull TenantId tenantId,
59                                                       final @Nonnull String sgtName) {
60         return new EndpointPolicyTemplateBySgtBuilder()
61                 .setSgt(sgt)
62                 .setEndpointGroups(Collections.singletonList(new EndpointGroupId(sgtName)))
63                 .setTenant(tenantId)
64                 // no conditions
65                 .setOrigin(TemplateGenerated.class)
66                 .build();
67     }
68
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>>() {
72             @Nullable
73             @Override
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));
80             }
81         });
82     }
83
84     private Optional<IseContext> findIseSourceConfigBySgt(final Sgt sgt) {
85         // expected relation (ise : tenant) == (1:1)
86         return iseContext
87                 .filter(context ->
88                         Range.closed(context.getIseSourceConfig().getSgtRangeMin().getValue(),
89                                 context.getIseSourceConfig().getSgtRangeMax().getValue())
90                                 .contains(sgt.getValue())
91                 );
92     }
93
94     @Override
95     public void assignIseContext(final @Nullable IseContext iseContext) {
96         this.iseContext = Optional.ofNullable(iseContext);
97     }
98
99     @Override
100     public void setIseSgtHarvester(final GbpIseSgtHarvester iseSgtHarvester) {
101         this.iseSgtHarvester = iseSgtHarvester;
102     }
103 }