update deprecated transform and addCallback methods
[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.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;
17
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;
31
32 /**
33  * Purpose: query ise in order to get name of sgt for given tenant and build {@link EndpointPolicyTemplateBySgt}
34  */
35 public class EPPolicyTemplateProviderIseImpl implements EPPolicyTemplateProviderFacade {
36
37     private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateProviderIseImpl.class);
38
39     private Optional<IseContext> iseContext = Optional.empty();
40     private GbpIseSgtHarvester iseSgtHarvester;
41
42     @Override
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()));
47     }
48
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>>() {
52             @Nullable
53             @Override
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)));
57             }
58         }, MoreExecutors.directExecutor());
59     }
60
61     private EndpointPolicyTemplateBySgt buildTemplate(final @Nonnull Sgt sgt, final @Nonnull TenantId tenantId,
62                                                       final @Nonnull String sgtName) {
63         return new EndpointPolicyTemplateBySgtBuilder()
64                 .setSgt(sgt)
65                 .setEndpointGroups(Collections.singletonList(new EndpointGroupId(sgtName)))
66                 .setTenant(tenantId)
67                 // no conditions
68                 .setOrigin(TemplateGenerated.class)
69                 .build();
70     }
71
72     private ListenableFuture<Optional<String>> queryIseOnSgt(final IseContext iseContext, @Nonnull final Sgt sgt) {
73         final ListenableFuture<Collection<SgtInfo>> sgtUpdateFu = iseSgtHarvester.harvestAll(iseContext);
74
75         Futures.addCallback(sgtUpdateFu, new FutureCallback<Collection<SgtInfo>>() {
76             @Override
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);
80             }
81
82             @Override
83             public void onFailure(final Throwable t) {
84                 LOG.debug("[epPolicyTemplateProvider] harvestAll FAILED: {}", t.getMessage());
85             }
86         }, MoreExecutors.directExecutor());
87
88         return Futures.transform(sgtUpdateFu, new Function<Collection<SgtInfo>, Optional<String>>() {
89             @Nullable
90             @Override
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));
97             }
98         }, MoreExecutors.directExecutor());
99     }
100
101     private Optional<IseContext> findIseSourceConfigBySgt(final Sgt sgt) {
102         // expected relation (ise : tenant) == (1:1)
103         return iseContext
104                 .filter(context ->
105                         context.getIseSourceConfig() != null && Range.closed(
106                                 context.getIseSourceConfig().getSgtRangeMin().getValue(),
107                                 context.getIseSourceConfig().getSgtRangeMax().getValue()
108                         ).contains(sgt.getValue())
109                 );
110     }
111
112     @Override
113     public void assignIseContext(final @Nullable IseContext iseContext) {
114         this.iseContext = Optional.ofNullable(iseContext);
115     }
116
117     @Override
118     public void setIseSgtHarvester(final GbpIseSgtHarvester iseSgtHarvester) {
119         this.iseSgtHarvester = iseSgtHarvester;
120     }
121 }