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.ep.provider.impl.dao;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.util.concurrent.AsyncFunction;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Collection;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.api.EPPolicyTemplateDaoFacade;
23 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.api.EPPolicyTemplateProvider;
24 import org.opendaylight.groupbasedpolicy.util.IidFactory;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Description;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.Name;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroupBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.SxpEpMapper;
31 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;
32 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.EndpointPolicyTemplateBySgtKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Purpose: add template provider fallback to {@link EPPolicyTemplateDaoImpl}
41 public class EPPolicyTemplateDaoFacadeImpl implements EPPolicyTemplateDaoFacade {
43 private static final Logger LOG = LoggerFactory.getLogger(EPPolicyTemplateDaoFacadeImpl.class);
45 private final EPPolicyTemplateDaoImpl epPolicyTemplateDao;
46 private final DataBroker dataBroker;
48 private EPPolicyTemplateProvider templateProvider;
50 public EPPolicyTemplateDaoFacadeImpl(final DataBroker dataBroker, final EPPolicyTemplateDaoImpl epPolicyTemplateDao) {
51 this.dataBroker = dataBroker;
52 this.epPolicyTemplateDao = epPolicyTemplateDao;
56 public void setTemplateProvider(final EPPolicyTemplateProvider templateProvider) {
57 this.templateProvider = templateProvider;
62 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read(@Nonnull final Sgt key) {
64 final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> templateFu = epPolicyTemplateDao.read(key);
66 // involve fallback if template is absent
67 return Futures.transform(templateFu, new AsyncFunction<Optional<EndpointPolicyTemplateBySgt>, Optional<EndpointPolicyTemplateBySgt>>() {
69 public ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> apply(
70 @Nonnull final Optional<EndpointPolicyTemplateBySgt> templateOpt) throws Exception {
72 return templateOpt.transform(template -> Futures.immediateFuture(templateOpt))
73 // failed to read template -> invoke fallback if available
74 .or(() -> java.util.Optional.ofNullable(templateProvider)
75 .flatMap(provider -> templateProvider.provideTemplate(key))
76 .map(template -> storeTemplateAndEpg(template))
77 .orElse(Futures.immediateFuture(Optional.absent()))
83 private ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> storeTemplateAndEpg(final EndpointPolicyTemplateBySgt template) {
84 // store EPG (presume that it does not exist)
85 final Sgt sgtValue = template.getSgt();
86 LOG.trace("storing EPGs for generated epPolicyTemplate: {} [{}]",
87 sgtValue.getValue(), template.getEndpointGroups().size());
88 final WriteTransaction wTx = dataBroker.newWriteOnlyTransaction();
90 boolean createParent = true;
91 for (EndpointGroupId epgId : template.getEndpointGroups()) {
92 final InstanceIdentifier<EndpointGroup> epgPath = IidFactory.endpointGroupIid(template.getTenant(), epgId);
93 final EndpointGroup epg = new EndpointGroupBuilder()
95 .setDescription(new Description("imported from ISE for sgt=" + sgtValue.getValue()))
96 .setName(new Name(String.format("%s_ISE_SGT_%d", epgId.getValue(), sgtValue.getValue())))
98 wTx.put(LogicalDatastoreType.CONFIGURATION, epgPath, epg, createParent);
102 // store ep-policy-template
103 LOG.trace("storing generated epPolicyTemplate: {}", sgtValue.getValue());
104 final InstanceIdentifier<EndpointPolicyTemplateBySgt> epPolicyTemplatePath = InstanceIdentifier
105 .create(SxpEpMapper.class)
106 .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(sgtValue));
107 wTx.put(LogicalDatastoreType.CONFIGURATION, epPolicyTemplatePath, template, true);
109 return Futures.transform(wTx.submit(), createStoreOutcomeHandler(template));
112 private Function<Void, Optional<EndpointPolicyTemplateBySgt>> createStoreOutcomeHandler(final EndpointPolicyTemplateBySgt template) {
113 return new Function<Void, Optional<EndpointPolicyTemplateBySgt>>() {
116 public Optional<EndpointPolicyTemplateBySgt> apply(@Nullable final Void aVoid) {
117 return Optional.of(template);
123 public Collection<EndpointPolicyTemplateBySgt> readBy(@Nonnull final EpPolicyTemplateValueKey specialKey) {
124 return epPolicyTemplateDao.readBy(specialKey);