2 * Copyright (c) 2015 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.util;
11 import java.util.HashMap;
12 import java.util.List;
16 import org.opendaylight.groupbasedpolicy.dto.ConditionSet;
17 import org.opendaylight.groupbasedpolicy.dto.EgKey;
18 import org.opendaylight.groupbasedpolicy.dto.IndexedTenant;
19 import org.opendaylight.groupbasedpolicy.dto.Policy;
20 import org.opendaylight.groupbasedpolicy.dto.PolicyInfo;
21 import org.opendaylight.groupbasedpolicy.util.ContractResolverUtils.ContractMatch;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.Table;
26 public class PolicyResolverUtils {
28 private PolicyResolverUtils() {
29 throw new UnsupportedOperationException("Cannot create an instance");
33 * Resolve the policy in three phases: <br>
34 * (1) select contracts that in scope based on contract selectors. <br>
35 * (2) select subjects that are in scope for each contract based on matchers in clauses <br>
36 * (3) resolve the set of in-scope contracts into a list of subjects that apply for each pair of
37 * endpoint groups and the conditions that can apply for for each endpoint in those groups.
39 public static Table<EgKey, EgKey, Policy> resolvePolicy(Set<IndexedTenant> tenants) {
40 return resolvePolicy(tenants, new HashMap<EgKey, Set<ConditionSet>>());
44 * Resolve the policy in three phases: <br>
45 * (1) select contracts that in scope based on contract selectors. <br>
46 * (2) select subjects that are in scope for each contract based on matchers in clauses <br>
47 * (3) resolve the set of in-scope contracts into a list of subjects that apply for each pair of
48 * endpoint groups and the conditions that can apply for for each endpoint in those groups.
50 public static PolicyInfo resolvePolicyInfo(Set<IndexedTenant> tenants) {
51 Map<EgKey, Set<ConditionSet>> egConditions = new HashMap<>();
52 Table<EgKey, EgKey, Policy> resolvedPolicy = resolvePolicy(tenants, egConditions);
53 return new PolicyInfo(resolvedPolicy, egConditions);
56 private static Table<EgKey, EgKey, Policy> resolvePolicy(Set<IndexedTenant> tenants,
57 Map<EgKey, Set<ConditionSet>> egConditions) {
58 Preconditions.checkNotNull(tenants);
59 Preconditions.checkNotNull(egConditions);
60 // select contracts that apply for the given tenant
61 Table<EgKey, EgKey, List<ContractMatch>> contractMatches = ContractResolverUtils.selectContracts(tenants);
63 // select subjects for the matching contracts and resolve the policy
64 // for endpoint group pairs. This does phase (2) and (3) as one step
65 return SubjectResolverUtils.selectSubjects(contractMatches, egConditions);