Moved util methods from PolicyResolver
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / PolicyScope.java
1 /*
2  * Copyright (c) 2014 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.resolver;
10
11 import java.util.Collections;
12 import java.util.Map;
13 import java.util.Set;
14 import java.util.concurrent.ConcurrentHashMap;
15
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;
18
19 /**
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.
24  * @see PolicyResolver 
25  * @author readams
26  */
27 public class PolicyScope {
28
29     /**
30      * The parent policy resolver
31      */
32     private final PolicyResolver resolver;
33     
34     /**
35      * The listener for this policy scope
36      */
37     private final PolicyListener listener;
38
39     /**
40      * The set of policy scope elements that we want to listen to.
41      */
42     private Set<EgKey> scopeElements;
43     
44     public PolicyScope(PolicyResolver resolver,
45                        PolicyListener listener) {
46         super();
47         this.resolver = resolver;
48         this.listener = listener;
49         Map<EgKey,Boolean> smap = new ConcurrentHashMap<>();
50         scopeElements = Collections.newSetFromMap(smap);
51     }
52
53     // ***********
54     // PolicyScope
55     // ***********
56
57     /**
58      * Add the endpoint group from the given tenant and endpoint group to the
59      * scope of updates
60      * @param tenant the tenant for the endpoint group
61      * @param endpointGroup the endpoint group to add.  This is the consumer
62      * of the contract
63      */
64     public void addToScope(TenantId tenant, EndpointGroupId endpointGroup) {
65         synchronized (this) {
66             scopeElements.add(new EgKey(tenant, endpointGroup));
67             resolver.subscribeTenant(tenant);
68         }
69     }
70
71     /**
72      * Add all endpoint groups in the given tenant to the scope of updates
73      * @param tenant the tenant to add.
74      */
75     public void addToScope(TenantId tenant) {
76         addToScope(tenant, null);
77     }
78
79     /**
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
84      * of the contract
85      */
86     public void removeFromScope(TenantId tenant, 
87                                 EndpointGroupId endpointGroup) {
88         synchronized (this) {
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;
94                     break;
95                 }
96             }
97             if (canUnsubscribe) {
98                 resolver.unsubscribeTenant(tenant);
99             }
100         }
101     }
102
103     /**
104      * Remove an endpoint group from the given tenant from 
105      * the scope of updates
106      * @param tenant the tenant for the endpoint group
107      */
108     public void removeFromScope(TenantId tenant) {
109         removeFromScope(tenant, null);
110     }
111
112     /**
113      * Check whether the policy scope applies to the given tenant and endpoint
114      * group
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
118      * tenant
119      * @return <code>true</code> if the policy scope applies to the given
120      * tenant and endpoint group.
121      */
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);
127                 
128     }
129
130     /**
131      * Get the policy listener for this scope
132      * @return the policy listener
133      */
134     public PolicyListener getListener() {
135         return listener;
136     }
137 }