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.dto;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.List;
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.Immutable;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpPrefix;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.has.endpoint.identification.constraints.endpoint.identification.constraints.l3.endpoint.identification.constraints.PrefixConstraint;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.Tenant;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.Contract;
27 import com.google.common.collect.ImmutableTable;
28 import com.google.common.collect.Table;
29 import com.google.common.collect.Table.Cell;
32 * Represent the policy that applies to a single pair of endpoint groups
33 * The policy is represented as a list of {@link RuleGroup} objects. A {@link RuleGroup} references
34 * ordered lists of rules from the policy,
35 * along with the associated {@link Tenant}, {@link Contract}, and {@link SubjectName}.
36 * A {@link RuleGroup} applies to a particular endpoint based on the set of
37 * endpoint constraints that are active for that endpoint. All rule groups associated
38 * with matching {@link EndpointConstraint}s apply.
44 * Policy where {@link #getRuleMap()} returns empty table
46 public static final Policy EMPTY = new Policy(ImmutableTable.<EndpointConstraint, EndpointConstraint, List<RuleGroup>>of());
48 private final Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> ruleMap;
51 * @param ruleMap {@code null} means that created {@link Policy} equals {@link Policy#EMPTY}
53 public Policy(@Nullable Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> ruleMap) {
54 if (ruleMap == null) {
55 this.ruleMap = EMPTY.getRuleMap();
57 this.ruleMap = ImmutableTable.copyOf(ruleMap);
61 public @Nonnull Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> getRuleMap() {
66 * Get the rules that apply to a particular pair of condition groups
68 * @param fromCg the condition group that applies to the origin endpoint
69 * @param toCg the condition group that applies to the destination endpoint
70 * @return sorted {@link RuleGroup} list
72 public List<RuleGroup> getRules(ConditionGroup fromCg, ConditionGroup toCg) {
73 List<RuleGroup> rules = new ArrayList<>();
74 for (Cell<EndpointConstraint, EndpointConstraint, List<RuleGroup>> cell : ruleMap.cellSet()) {
75 if (fromCg.contains(cell.getRowKey().getConditionSet()) && toCg.contains(cell.getColumnKey().getConditionSet()))
76 rules.addAll(cell.getValue());
78 Collections.sort(rules);
82 public static Set<IpPrefix> getIpPrefixesFrom(Set<PrefixConstraint> prefixConstraints) {
83 Set<IpPrefix> ipPrefixes = new HashSet<>();
84 for (PrefixConstraint prefixConstraint : prefixConstraints) {
85 ipPrefixes.add(prefixConstraint.getIpPrefix());
91 public int hashCode() {
94 result = prime * result + ((ruleMap == null) ? 0 : ruleMap.hashCode());
99 public boolean equals(Object obj) {
104 if (getClass() != obj.getClass())
106 Policy other = (Policy) obj;
107 if (ruleMap == null) {
108 if (other.ruleMap != null)
110 } else if (!ruleMap.equals(other.ruleMap))
116 public String toString() {
117 return "Policy [ruleMap=" + ruleMap + "]";