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;
12 import java.util.HashSet;
13 import java.util.List;
17 import javax.annotation.Nonnull;
18 import javax.annotation.concurrent.Immutable;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
22 import com.google.common.collect.Sets;
23 import com.google.common.collect.Table;
26 * Represent the current policy snapshot for the set of tenants that are
31 public class PolicyInfo {
32 final Table<EgKey, EgKey, Policy> policyMap;
33 final Map<EgKey, Set<ConditionSet>> egConditions;
35 public PolicyInfo(Table<EgKey, EgKey, Policy> policyMap,
36 Map<EgKey, Set<ConditionSet>> egConditions) {
38 this.policyMap = policyMap;
39 this.egConditions = egConditions;
41 public Table<EgKey, EgKey, Policy> getPolicyMap() {
46 * Get the policy that currently applies between consumer endpoint group and provider endpoint
49 * @param consEgKey the consumer endpoint group
50 * @param provEgKey the provider endpoint group
51 * @return the {@link Policy} that applies. Cannot be null
53 public @Nonnull Policy getPolicy(EgKey consEgKey, EgKey provEgKey) {
54 Policy p = policyMap.get(consEgKey, provEgKey);
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
66 public Set<ConditionSet> getEgConditions(EgKey eg) {
67 return Collections.unmodifiableSet(egConditions.get(eg));
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}
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<>();
87 if (matching == null) return ConditionGroup.EMPTY;
88 return new ConditionGroup(matching);
92 * Get the set of endpoint groups that are peers for the given endpoint
94 * @param eg the endpoint group
95 * @return the set of endpoint groups
97 public Set<EgKey> getPeers(EgKey eg) {
98 return Sets.union(policyMap.row(eg).keySet(),
99 policyMap.column(eg).keySet());