import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.IosXeRendererProvider;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.manager.PolicyManager;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache.EpPolicyCacheImpl;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.listener.EpPolicyTemplateBySgtListenerImpl;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.listener.IosXeCapableNodeListenerImpl;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.listener.RendererConfigurationListenerImpl;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager.NodeManager;
private final DataBroker dataBroker;
private final RendererName rendererName;
private RendererConfigurationListenerImpl rendererConfigurationListener;
- private EpPolicyTemplateBySgtListenerImpl epPolicyTemplateBySgtListener;
private IosXeCapableNodeListenerImpl iosXeCapableNodeListener;
- private EpPolicyCacheImpl epPolicyCache;
private PolicyManager policyManager;
private NodeManager nodeManager;
if (rendererConfigurationListener != null) {
rendererConfigurationListener.close();
}
- if (epPolicyTemplateBySgtListener != null) {
- epPolicyTemplateBySgtListener.close();
- }
if (iosXeCapableNodeListener != null) {
iosXeCapableNodeListener.close();
}
- if (epPolicyCache != null) {
- epPolicyCache.invalidateAll();
- }
}
@Override
//TODO register listeners:
// node-manager
nodeManager = new NodeManager(dataBroker, providerContext);
- // ep-policy-template-by-sgt
- epPolicyCache = new EpPolicyCacheImpl();
- epPolicyTemplateBySgtListener = new EpPolicyTemplateBySgtListenerImpl(dataBroker, epPolicyCache);
// network-topology
iosXeCapableNodeListener = new IosXeCapableNodeListenerImpl(dataBroker, nodeManager);
// policy-manager and delegates
- policyManager = new PolicyManagerImpl(dataBroker, epPolicyCache);
+ policyManager = new PolicyManagerImpl(dataBroker);
final PolicyManager policyManagerZip = new PolicyManagerZipImpl(policyManager);
// renderer-configuration endpoints
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache;
-
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.cache.DSTreeBasedCache;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.RendererPolicyUtil;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
-
-/**
- * Purpose: cache for {@link EndpointPolicyTemplateBySgt}
- */
-public class EpPolicyCacheImpl implements DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> {
-
- private final ConcurrentMap<EpPolicyTemplateCacheKey, Sgt> cache;
- private final EpPolicyTemplateCacheKeyFactory keyFactory;
-
- public EpPolicyCacheImpl() {
- cache = new ConcurrentHashMap<>();
- keyFactory = new EpPolicyTemplateCacheKeyFactory(RendererPolicyUtil.createEndpointGroupIdOrdering(),
- RendererPolicyUtil.createConditionNameOrdering());
- }
-
- @Override
- public void invalidate(final EndpointPolicyTemplateBySgt exSource) {
- cache.remove(keyFactory.createKey(exSource));
- }
-
- @Override
- public void add(final EndpointPolicyTemplateBySgt newSource) {
- final EpPolicyTemplateCacheKey key = keyFactory.createKey(newSource);
- cache.put(key, newSource.getSgt());
- }
-
- @Override
- public void update(final EndpointPolicyTemplateBySgt before, final EndpointPolicyTemplateBySgt after) {
- cache.remove(keyFactory.createKey(before));
- cache.put(keyFactory.createKey(after), after.getSgt());
- }
-
- @Override
- public Sgt lookupValue(final EpPolicyTemplateCacheKey key) {
- return cache.get(keyFactory.createKey(key));
- }
-
- @Override
- public void invalidateAll() {
- cache.clear();
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache;
-
-import java.util.List;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-
-/**
- * Purpose: composite key holcer for {@link EndpointPolicyTemplateBySgt}
- */
-public class EpPolicyTemplateCacheKey {
-
- private final TenantId tenantId;
- private final List<EndpointGroupId> epgId;
- private final List<ConditionName> conditionName;
-
- public EpPolicyTemplateCacheKey(final TenantId tenantId, final List<EndpointGroupId> epgId, final List<ConditionName> conditionName) {
- this.tenantId = tenantId;
- this.epgId = epgId;
- this.conditionName = conditionName;
- }
-
- public EpPolicyTemplateCacheKey(AddressEndpointWithLocation endpoint) {
- this(endpoint.getTenant(), endpoint.getEndpointGroup(), endpoint.getCondition());
- }
-
- public TenantId getTenantId() {
- return tenantId;
- }
-
- public List<EndpointGroupId> getEpgId() {
- return epgId;
- }
-
- public List<ConditionName> getConditionName() {
- return conditionName;
- }
-
- @Override
- public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final EpPolicyTemplateCacheKey that = (EpPolicyTemplateCacheKey) o;
-
- if (tenantId != null ? !tenantId.equals(that.tenantId) : that.tenantId != null) return false;
- if (epgId != null ? !epgId.equals(that.epgId) : that.epgId != null) return false;
- return conditionName != null ? conditionName.equals(that.conditionName) : that.conditionName == null;
-
- }
-
- @Override
- public int hashCode() {
- return tenantId != null ? tenantId.hashCode() : 0;
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache;
-
-import com.google.common.collect.Ordering;
-import java.util.Collections;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-
-/**
- * Purpose: create cache keys with ordered lists inside
- */
-public class EpPolicyTemplateCacheKeyFactory {
- private final Ordering<EndpointGroupId> epgIdOrdering;
- private final Ordering<ConditionName> conditionOrdering;
-
- public EpPolicyTemplateCacheKeyFactory(final Ordering<EndpointGroupId> epgIdOrdering,
- final Ordering<ConditionName> conditionOrdering) {
- this.epgIdOrdering = epgIdOrdering;
- this.conditionOrdering = conditionOrdering;
- }
-
- public EpPolicyTemplateCacheKey createKey(final EndpointPolicyTemplateBySgt newSource) {
- Collections.sort(newSource.getEndpointGroups(), epgIdOrdering);
- Collections.sort(newSource.getConditions(), conditionOrdering);
-
- return new EpPolicyTemplateCacheKey(
- newSource.getTenant(), newSource.getEndpointGroups(), newSource.getConditions());
- }
-
- public EpPolicyTemplateCacheKey createKey(final EpPolicyTemplateCacheKey existingKey) {
- Collections.sort(existingKey.getEpgId(), epgIdOrdering);
- Collections.sort(existingKey.getConditionName(), conditionOrdering);
- return existingKey;
- }
-}
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.listener;
-
-import com.google.common.base.Preconditions;
-import java.util.Collection;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.cache.DSTreeBasedCache;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache.EpPolicyTemplateCacheKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.SxpMapper;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Purpose: listen to {@link EndpointPolicyTemplateBySgt} changes for caching purposes
- */
-public class EpPolicyTemplateBySgtListenerImpl implements DataTreeChangeListener<EndpointPolicyTemplateBySgt>, AutoCloseable {
-
- private static final Logger LOG = LoggerFactory.getLogger(EpPolicyTemplateBySgtListenerImpl.class);
-
- private final ListenerRegistration<EpPolicyTemplateBySgtListenerImpl> listenerRegistration;
- private final DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> cache;
-
- public EpPolicyTemplateBySgtListenerImpl(final DataBroker dataBroker,
- final DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> cache) {
- this.cache = Preconditions.checkNotNull(cache, "missing ep-policy-template cache");
- final InstanceIdentifier<EndpointPolicyTemplateBySgt> templatePath = InstanceIdentifier.create(SxpMapper.class)
- .child(EndpointPolicyTemplateBySgt.class);
-
- final DataTreeIdentifier<EndpointPolicyTemplateBySgt> treePath = new DataTreeIdentifier<>(
- LogicalDatastoreType.CONFIGURATION, templatePath);
- listenerRegistration = dataBroker.registerDataTreeChangeListener(treePath, this);
- LOG.info("ep-policy-template listener registered");
- }
-
- @Override
- public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<EndpointPolicyTemplateBySgt>> collection) {
- LOG.debug("ep-policy-template changed");
- for (DataTreeModification<EndpointPolicyTemplateBySgt> epPolicyTemplateModification : collection) {
- final DataObjectModification<EndpointPolicyTemplateBySgt> rootNode = epPolicyTemplateModification
- .getRootNode();
- final DataObjectModification.ModificationType modificationType = rootNode.getModificationType();
- switch (modificationType) {
- case DELETE:
- // invalidate cache
- cache.invalidate(rootNode.getDataBefore());
- break;
- case WRITE:
- // extend cache
- cache.add(rootNode.getDataAfter());
- break;
- case SUBTREE_MODIFIED:
- // update cache
- cache.update(rootNode.getDataBefore(), rootNode.getDataAfter());
- break;
- default:
- LOG.warn("modification type not supported: {}", modificationType);
- }
- }
- }
-
- @Override
- public void close() {
- listenerRegistration.close();
- }
-}
package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.manager;
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.ListenableFuture;
+import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction.In;
+import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction.Out;
+import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation.CONSUMER;
+import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation.PROVIDER;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.groupbasedpolicy.api.sf.AllowActionDefinition;
import org.opendaylight.groupbasedpolicy.api.sf.ChainActionDefinition;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.cache.DSTreeBasedCache;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.manager.PolicyManager;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache.EpPolicyTemplateCacheKey;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.PolicyManagerUtil;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.PolicyWriter;
import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util.RendererPolicyUtil;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.actions.Action;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.classifiers.Classifier;
import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.has.resolved.rules.ResolvedRule;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.AddressEndpointWithLocationAug;
import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
-import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction.In;
-import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.HasDirection.Direction.Out;
-import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation.CONSUMER;
-import static org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.EndpointPolicyParticipation.PROVIDER;
+import com.google.common.base.Preconditions;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
public class PolicyManagerImpl implements PolicyManager {
private static final Logger LOG = LoggerFactory.getLogger(PolicyMapper.class);
private final DataBroker dataBroker;
- private DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> epPolicyCache;
private final PolicyMapper mapper;
private final String policyMapName = "service-chains";
private Map<DataBroker, PolicyWriter> perDeviceWriterCache = new HashMap<>();
public enum ActionCase { ALLOW, CHAIN }
- public PolicyManagerImpl(final DataBroker dataBroker,
- final DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> epPolicyCache) {
+ public PolicyManagerImpl(final DataBroker dataBroker) {
this.dataBroker = Preconditions.checkNotNull(dataBroker);
- this.epPolicyCache = Preconditions.checkNotNull(epPolicyCache);
mapper = new PolicyMapper(dataBroker);
}
}
AddressEndpointWithLocation endpointWithLocation = RendererPolicyUtil.lookupEndpoint(endpointKey,
endpointsWithLocation);
- return epPolicyCache.lookupValue(new EpPolicyTemplateCacheKey(endpointWithLocation));
+ AddressEndpointWithLocationAug augmentation = endpointWithLocation.getAugmentation(AddressEndpointWithLocationAug.class);
+ if (augmentation == null) {
+ return null;
+ }
+
+ return augmentation.getSgt();
}
private List<Class> resolveChainAction(final PeerEndpointWithPolicy peerEndpoint, final Sgt sourceSgt,
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache;
-
-import java.util.ArrayList;
-import java.util.List;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocationBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgtBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
-
-/**
- * Test for {@link EpPolicyCacheImpl}.
- */
-public class EpPolicyCacheImplTest {
-
- private static final int SGT1 = 1;
- private static final int SGT2 = 2;
- private static final String TENANT1 = "tenant1";
- private static final String TENANT2 = "tenant2";
-
- private static final EpPolicyTemplateCacheKey KEY_1 = createKey(TENANT1, new String[]{"n1", "n2"});
- private static final EpPolicyTemplateCacheKey KEY_2 = createKey(TENANT2, new String[]{"n3"});
-
- private static final EndpointPolicyTemplateBySgt TEMPLATE_1 = createTemplate(SGT1, TENANT1, new String[]{"n1", "n2"});
- private static final EndpointPolicyTemplateBySgt TEMPLATE_2 = createTemplate(SGT2, TENANT2, new String[]{"n3"});
-
- private EpPolicyCacheImpl cache;
-
- @Before
- public void setUp() throws Exception {
- cache = new EpPolicyCacheImpl();
- }
-
- @Test
- public void testInvalidate() throws Exception {
- cache.add(TEMPLATE_1);
- cache.add(TEMPLATE_2);
-
- checkValuePresence(KEY_1, SGT1);
-
- checkValuePresence(KEY_2, SGT2);
-
- cache.invalidate(TEMPLATE_1);
- Assert.assertNull(cache.lookupValue(KEY_1));
- Assert.assertNotNull(cache.lookupValue(KEY_2));
- }
-
- @Test
- public void testAdd() throws Exception {
- cache.add(TEMPLATE_1);
- checkValuePresence(KEY_1, SGT1);
- }
-
- @Test
- public void testUpdate() throws Exception {
- cache.add(TEMPLATE_1);
- checkValuePresence(KEY_1, SGT1);
-
- cache.update(TEMPLATE_1, TEMPLATE_2);
-
- Assert.assertNull(cache.lookupValue(KEY_1));
- checkValuePresence(KEY_2, SGT2);
- }
-
- @Test
- public void testLookupValue() throws Exception {
- cache.add(TEMPLATE_1);
- checkValuePresence(KEY_1, SGT1);
- }
-
- private void checkValuePresence(final EpPolicyTemplateCacheKey key, final int expectedSgt) {
- final Sgt sgt = cache.lookupValue(key);
- Assert.assertNotNull(sgt);
- Assert.assertEquals(expectedSgt, sgt.getValue().intValue());
- }
-
- @Test
- public void testLookupValue_withChangedOrder() throws Exception {
- Assert.assertNull(cache.lookupValue(KEY_1));
- cache.add(TEMPLATE_1);
- checkValuePresence(KEY_1, SGT1);
-
- final EpPolicyTemplateCacheKey twistedKey1 = createKey(TENANT1, new String[]{"n1", "n2"}, new String[]{"n2", "n1"});
- checkValuePresence(twistedKey1, SGT1);
-
- final EpPolicyTemplateCacheKey twistedKey2 = createKey(TENANT1, new String[]{"n2", "n1"}, new String[]{"n1", "n2"});
- checkValuePresence(twistedKey2, SGT1);
- }
-
- @Test
- public void testInvalidateAll() throws Exception {
- cache.add(TEMPLATE_1);
- cache.add(TEMPLATE_2);
- checkValuePresence(KEY_1, SGT1);
-
- cache.invalidateAll();
- Assert.assertNull(cache.lookupValue(KEY_1));
- Assert.assertNull(cache.lookupValue(KEY_2));
- }
-
- private static EndpointPolicyTemplateBySgt createTemplate(final int sgt, final String tenant, final String[] names) {
- final List<ConditionName> conditions = buildConditions(names);
- final List<EndpointGroupId> endpointGroupIds = buildEndpointGroupIds(names);
-
- return new EndpointPolicyTemplateBySgtBuilder()
- .setSgt(new Sgt(sgt))
- .setTenant(new TenantId(tenant))
- .setConditions(conditions)
- .setEndpointGroups(endpointGroupIds)
- .build();
- }
-
- private static List<EndpointGroupId> buildEndpointGroupIds(final String[] names) {
- final List<EndpointGroupId> endpointGroupIds = new ArrayList<>();
- for (String epgId : names) {
- endpointGroupIds.add(new EndpointGroupId(epgId));
- }
- return endpointGroupIds;
- }
-
- private static List<ConditionName> buildConditions(final String[] names) {
- final List<ConditionName> conditions = new ArrayList<>();
- for (String condition : names) {
- conditions.add(new ConditionName(condition));
- }
- return conditions;
- }
-
- private static EpPolicyTemplateCacheKey createKey(final String tenant, final String[] names) {
- return createKey(tenant, names, names);
- }
-
- private static EpPolicyTemplateCacheKey createKey(final String tenant, final String[] epgIds, final String[] conditionNames) {
- return new EpPolicyTemplateCacheKey(new AddressEndpointWithLocationBuilder()
- .setTenant(new TenantId(tenant))
- .setEndpointGroup(buildEndpointGroupIds(epgIds))
- .setCondition(buildConditions(conditionNames))
- .build());
- }
-}
\ No newline at end of file
+++ /dev/null
-/*
- * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-
-package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.listener;
-
-import com.google.common.collect.Lists;
-import java.util.Collections;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Matchers;
-import org.mockito.Mock;
-import org.mockito.Mockito;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.api.cache.DSTreeBasedCache;
-import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.cache.EpPolicyTemplateCacheKey;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgt;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.mapper.model.rev160302.sxp.mapper.EndpointPolicyTemplateBySgtBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
-import org.opendaylight.yangtools.concepts.ListenerRegistration;
-
-/**
- * Test for {@link EpPolicyTemplateBySgtListenerImpl}.
- */
-@RunWith(MockitoJUnitRunner.class)
-public class EpPolicyTemplateBySgtListenerImplTest {
-
- private static final Sgt SGT1 = new Sgt(1);
- private static final TenantId TENANT1 = new TenantId("tenant1");
- private static final TenantId TENANT2 = new TenantId("tenant2");
- @Mock
- private DataBroker dataBroker;
- @Mock
- private DSTreeBasedCache<EndpointPolicyTemplateBySgt, EpPolicyTemplateCacheKey, Sgt> cache;
- @Mock
- private ListenerRegistration<EpPolicyTemplateBySgtListenerImpl> listenerRegistration;
- @Mock
- private DataTreeModification<EndpointPolicyTemplateBySgt> dataTreeModification;
- @Mock
- private DataObjectModification<EndpointPolicyTemplateBySgt> rootNode;
-
- private final EndpointPolicyTemplateBySgt template1;
- private final EndpointPolicyTemplateBySgt template2;
-
- private EpPolicyTemplateBySgtListenerImpl listener;
-
- public EpPolicyTemplateBySgtListenerImplTest() {
- this.template1 = new EndpointPolicyTemplateBySgtBuilder()
- .setSgt(SGT1)
- .setTenant(TENANT1)
- .setEndpointGroups(Lists.newArrayList(new EndpointGroupId("epg1"), new EndpointGroupId("epg2")))
- .setConditions(Lists.newArrayList(new ConditionName("condition1"), new ConditionName("condition2")))
- .build();
-
- this.template2 = new EndpointPolicyTemplateBySgtBuilder()
- .setSgt(SGT1)
- .setTenant(TENANT2)
- .setEndpointGroups(Lists.newArrayList(new EndpointGroupId("epg3"), new EndpointGroupId("epg4")))
- .setConditions(Lists.newArrayList(new ConditionName("condition2"), new ConditionName("condition3")))
- .build();
- }
-
- @Before
- public void setUp() throws Exception {
- Mockito.when(dataBroker.registerDataTreeChangeListener(
- Matchers.<DataTreeIdentifier<EndpointPolicyTemplateBySgt>>any(),
- Matchers.<EpPolicyTemplateBySgtListenerImpl>any()))
- .thenReturn(listenerRegistration);
- listener = new EpPolicyTemplateBySgtListenerImpl(dataBroker, cache);
- }
-
- @After
- public void tearDown() throws Exception {
- Mockito.verifyNoMoreInteractions(cache);
- }
-
- @Test
- public void testOnDataTreeChanged_add() throws Exception {
- Mockito.when(rootNode.getDataAfter()).thenReturn(template1);
- Mockito.when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.WRITE);
- Mockito.when(dataTreeModification.getRootNode()).thenReturn(rootNode);
-
- listener.onDataTreeChanged(Collections.singleton(dataTreeModification));
- Mockito.verify(cache).add(template1);
- }
-
- @Test
- public void testOnDataTreeChanged_remove() throws Exception {
- Mockito.when(rootNode.getDataBefore()).thenReturn(template1);
- Mockito.when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.DELETE);
- Mockito.when(dataTreeModification.getRootNode()).thenReturn(rootNode);
-
- listener.onDataTreeChanged(Collections.singleton(dataTreeModification));
- Mockito.verify(cache).invalidate(template1);
- }
-
- @Test
- public void testOnDataTreeChanged_update() throws Exception {
- Mockito.when(rootNode.getDataBefore()).thenReturn(template1);
- Mockito.when(rootNode.getDataAfter()).thenReturn(template2);
- Mockito.when(rootNode.getModificationType()).thenReturn(DataObjectModification.ModificationType.SUBTREE_MODIFIED);
- Mockito.when(dataTreeModification.getRootNode()).thenReturn(rootNode);
-
- listener.onDataTreeChanged(Collections.singleton(dataTreeModification));
- Mockito.verify(cache).update(template1, template2);
- }
-
- @Test
- public void testClose() throws Exception {
- Mockito.verify(listenerRegistration, Mockito.never()).close();
- listener.close();
- Mockito.verify(listenerRegistration).close();
- listener.close();
- Mockito.verify(listenerRegistration, Mockito.times(2)).close();
- }
-}
\ No newline at end of file