Bug 4098 - ofoverlay-renderer failure in jdk8
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / PolicyInfo.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.HashSet;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Set;
16
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.Immutable;
19
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
21
22 import com.google.common.collect.Sets;
23 import com.google.common.collect.Table;
24
25 /**
26  * Represent the current policy snapshot for the set of tenants that are
27  * in scope
28  * @author readams
29  */
30 @Immutable
31 public class PolicyInfo {
32     final Table<EgKey, EgKey, Policy> policyMap;
33     final Map<EgKey, Set<ConditionSet>> egConditions;
34
35     public PolicyInfo(Table<EgKey, EgKey, Policy> policyMap,
36                       Map<EgKey, Set<ConditionSet>> egConditions) {
37         super();
38         this.policyMap = policyMap;
39         this.egConditions = egConditions;
40     }
41     public Table<EgKey, EgKey, Policy> getPolicyMap() {
42         return policyMap;
43     }
44
45     /**
46      * Get the policy that currently applies between consumer endpoint group and provider endpoint
47      * group.
48      *
49      * @param consEgKey the consumer endpoint group
50      * @param provEgKey the provider endpoint group
51      * @return the {@link Policy} that applies. Cannot be null
52      */
53     public @Nonnull Policy getPolicy(EgKey consEgKey, EgKey provEgKey) {
54         Policy p = policyMap.get(consEgKey, provEgKey);
55         if (p == null)
56             return Policy.EMPTY;
57         return p;
58     }
59
60     /**
61      * Get the condition sets for a particular endpoint group
62      * @param eg the endpoint group
63      * @return the set of condition sets that could apply to an endpoint
64      * in that endpoint group
65      */
66     public Set<ConditionSet> getEgConditions(EgKey eg) {
67         return Collections.unmodifiableSet(egConditions.get(eg));
68     }
69
70     /**
71      * Get the condition group as it applies to the given list of conditions
72      * @param eg the endpoint group key
73      * @param conditions the list of conditions
74      * @return the {@link ConditionGroup}
75      */
76     public ConditionGroup getEgCondGroup(EgKey eg,
77                                          List<ConditionName> conditions) {
78         Set<ConditionSet> egconds = egConditions.get(eg);
79         if (egconds == null) return ConditionGroup.EMPTY;
80         Set<ConditionSet> matching = null;
81         for (ConditionSet cs : egconds) {
82             if (cs.matches(conditions)) {
83                 if (matching == null) matching = new HashSet<>();
84                 matching.add(cs);
85             }
86         }
87         if (matching == null) return ConditionGroup.EMPTY;
88         return new ConditionGroup(matching);
89     }
90
91     /**
92      * Get the set of endpoint groups that are peers for the given endpoint
93      * group
94      * @param eg the endpoint group
95      * @return the set of endpoint groups
96      */
97     public Set<EgKey> getPeers(EgKey eg) {
98         return Sets.union(policyMap.row(eg).keySet(),
99                           policyMap.column(eg).keySet());
100     }
101 }