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;
11 import com.google.common.base.Function;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import javax.annotation.Nullable;
17 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.api.EPPolicyTemplateDaoFacade;
18 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.api.EPToSgtMapper;
19 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao.EpPolicyTemplateValueKey;
20 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao.EpPolicyTemplateValueKeyFactory;
21 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.EPTemplateUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
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.sxp.database.rev160308.Sgt;
27 * Purpose: mapper implementation based on {@link ReadableAsyncByKey} (e.g.: {@link EPPolicyTemplateDaoFacade})
29 public class EPToSgtMapperImpl implements EPToSgtMapper {
31 public static final Function<Collection<EndpointPolicyTemplateBySgt>, Collection<Sgt>> TRANSFORM_TEMPLATE_TO_SGT =
32 new Function<Collection<EndpointPolicyTemplateBySgt>, Collection<Sgt>>() {
35 public Collection<Sgt> apply(@Nullable final Collection<EndpointPolicyTemplateBySgt> input) {
36 final Collection<Sgt> sgtBag = new HashSet<>();
37 for (EndpointPolicyTemplateBySgt template : input) {
38 sgtBag.add(template.getSgt());
44 private final ReadableAsyncByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> epPolicyTemplateReader;
45 private final EpPolicyTemplateValueKeyFactory keyFactory;
47 public EPToSgtMapperImpl(final ReadableAsyncByKey<EpPolicyTemplateValueKey, EndpointPolicyTemplateBySgt> epPolicyTemplateReader) {
48 this.epPolicyTemplateReader = epPolicyTemplateReader;
49 keyFactory = new EpPolicyTemplateValueKeyFactory(
50 EPTemplateUtil.createEndpointGroupIdOrdering(), EPTemplateUtil.createConditionNameOrdering());
55 public ListenableFuture<Collection<Sgt>> findSgtForEP(final AddressEndpointWithLocation endpointWithLocation) {
56 final EpPolicyTemplateValueKey rawKey = new EpPolicyTemplateValueKey(endpointWithLocation);
57 final EpPolicyTemplateValueKey key = keyFactory.sortValueKeyLists(rawKey);
58 return Futures.transform(epPolicyTemplateReader.readBy(key), TRANSFORM_TEMPLATE_TO_SGT);