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