f9bdc0ebfbfce45cb9cf40a82c71e82d5d191602
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / dto / Policy.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.dto;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Set;
16
17 import javax.annotation.Nonnull;
18 import javax.annotation.Nullable;
19 import javax.annotation.concurrent.Immutable;
20
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;
26
27 import com.google.common.collect.ImmutableTable;
28 import com.google.common.collect.Table;
29 import com.google.common.collect.Table.Cell;
30
31 /**
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.
39  */
40 @Immutable
41 public class Policy {
42
43     /**
44      * Policy where {@link #getRuleMap()} returns empty table
45      */
46     public static final Policy EMPTY = new Policy(ImmutableTable.<EndpointConstraint, EndpointConstraint, List<RuleGroup>>of());
47
48     private final Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> ruleMap;
49
50     /**
51      * @param ruleMap {@code null} means that created {@link Policy} equals {@link Policy#EMPTY}
52      */
53     public Policy(@Nullable Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> ruleMap) {
54         if (ruleMap == null) {
55             this.ruleMap = EMPTY.getRuleMap();
56         } else {
57             this.ruleMap = ImmutableTable.copyOf(ruleMap);
58         }
59     }
60
61     public @Nonnull Table<EndpointConstraint, EndpointConstraint, List<RuleGroup>> getRuleMap() {
62         return ruleMap;
63     }
64
65     /**
66      * Get the rules that apply to a particular pair of condition groups
67      *
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
71      */
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());
77         }
78         Collections.sort(rules);
79         return rules;
80     }
81
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());
86         }
87         return ipPrefixes;
88     }
89
90     @Override
91     public int hashCode() {
92         final int prime = 31;
93         int result = 1;
94         result = prime * result + ((ruleMap == null) ? 0 : ruleMap.hashCode());
95         return result;
96     }
97
98     @Override
99     public boolean equals(Object obj) {
100         if (this == obj)
101             return true;
102         if (obj == null)
103             return false;
104         if (getClass() != obj.getClass())
105             return false;
106         Policy other = (Policy) obj;
107         if (ruleMap == null) {
108             if (other.ruleMap != null)
109                 return false;
110         } else if (!ruleMap.equals(other.ruleMap))
111             return false;
112         return true;
113     }
114
115     @Override
116     public String toString() {
117         return "Policy [ruleMap=" + ruleMap + "]";
118     }
119
120 }