2 * Copyright (c) 2014 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.resolver;
11 import java.util.Collections;
14 import java.util.concurrent.ConcurrentHashMap;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
20 * The policy scope object represents a scope for policy-related information.
21 * A renderer that addresses a particular scope can express this as a
22 * {@link PolicyScope} with an associates {@link PolicyListener} that can
23 * receive relevant updates.
27 public class PolicyScope {
30 * The parent policy resolver
32 private final PolicyResolver resolver;
35 * The listener for this policy scope
37 private final PolicyListener listener;
40 * The set of policy scope elements that we want to listen to.
42 private Set<EgKey> scopeElements;
44 public PolicyScope(PolicyResolver resolver,
45 PolicyListener listener) {
47 this.resolver = resolver;
48 this.listener = listener;
49 Map<EgKey,Boolean> smap = new ConcurrentHashMap<>();
50 scopeElements = Collections.newSetFromMap(smap);
58 * Add the endpoint group from the given tenant and endpoint group to the
60 * @param tenant the tenant for the endpoint group
61 * @param endpointGroup the endpoint group to add. This is the consumer
64 public void addToScope(TenantId tenant, EndpointGroupId endpointGroup) {
66 scopeElements.add(new EgKey(tenant, endpointGroup));
67 resolver.subscribeTenant(tenant);
72 * Add all endpoint groups in the given tenant to the scope of updates
73 * @param tenant the tenant to add.
75 public void addToScope(TenantId tenant) {
76 addToScope(tenant, null);
80 * Remove an endpoint group from the given tenant and endpoint group from
81 * the scope of updates
82 * @param tenant the tenant for the endpoint group
83 * @param endpointGroup the endpoint group to remove. This is the consumer
86 public void removeFromScope(TenantId tenant,
87 EndpointGroupId endpointGroup) {
89 boolean canUnsubscribe = false;
90 scopeElements.remove(new EgKey(tenant, endpointGroup));
91 for (EgKey element : scopeElements) {
92 if (element.getTenantId().equals(tenant)) {
93 canUnsubscribe = false;
98 resolver.unsubscribeTenant(tenant);
104 * Remove an endpoint group from the given tenant from
105 * the scope of updates
106 * @param tenant the tenant for the endpoint group
108 public void removeFromScope(TenantId tenant) {
109 removeFromScope(tenant, null);
113 * Check whether the policy scope applies to the given tenant and endpoint
115 * @param tenant the tenant to look up
116 * @param endpointGroup the endpoint group to look up. May be null,
117 * in which case will only check if the policy scope applies to the entire
119 * @return <code>true</code> if the policy scope applies to the given
120 * tenant and endpoint group.
122 public boolean contains(TenantId tenant, EndpointGroupId endpointGroup) {
123 EgKey pse = new EgKey(tenant, endpointGroup);
124 if (scopeElements.contains(pse)) return true;
125 pse = new EgKey(tenant, null);
126 return scopeElements.contains(pse);
131 * Get the policy listener for this scope
132 * @return the policy listener
134 public PolicyListener getListener() {