Add yang generated packages in .gitignore
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleDao.java
1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
2
3 import java.util.Set;
4
5 import javax.annotation.Nullable;
6
7 import org.opendaylight.neutron.spi.NeutronSecurityRule;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.SetMultimap;
13
14 public class SecRuleDao {
15
16     private final SetMultimap<EndpointGroupId, NeutronSecurityRule> secRulesByOwnerSecGrpId = HashMultimap.create();
17     private final SetMultimap<OwnerAndRemoteOfSecRule, NeutronSecurityRule> secRulesByRemoteSecGrpId =
18             HashMultimap.create();
19
20     public void addSecRule(NeutronSecurityRule secRule) {
21         Preconditions.checkNotNull(secRule);
22         EndpointGroupId ownerSecGrp = SecRuleEntityDecoder.getProviderEpgId(secRule);
23         EndpointGroupId remoteSecGrp = SecRuleEntityDecoder.getConsumerEpgId(secRule);
24         secRulesByOwnerSecGrpId.put(ownerSecGrp, secRule);
25         secRulesByRemoteSecGrpId.put(new OwnerAndRemoteOfSecRule(ownerSecGrp, remoteSecGrp), secRule);
26     }
27
28     public Set<NeutronSecurityRule> getSecRulesByOwnerSecGrpId(EndpointGroupId secGrpId) {
29         return secRulesByOwnerSecGrpId.get(secGrpId);
30     }
31
32     public Set<NeutronSecurityRule> getSecRulesBySecGrpIdAndRemoteSecGrpId(EndpointGroupId ownerSecGrpId,
33             @Nullable EndpointGroupId remoteSecGrpId) {
34         return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, remoteSecGrpId));
35     }
36
37     public Set<NeutronSecurityRule> getSecRulesWithoutRemoteSecGrpBySecGrpId(EndpointGroupId ownerSecGrpId) {
38         return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, null));
39     }
40
41     public Set<EndpointGroupId> getAllOwnerSecGrps() {
42         return secRulesByOwnerSecGrpId.keySet();
43     }
44
45     public void removeSecRule(NeutronSecurityRule secRule) {
46         Preconditions.checkNotNull(secRule);
47         EndpointGroupId ownerSecGrp = SecRuleEntityDecoder.getProviderEpgId(secRule);
48         EndpointGroupId remoteSecGrp = SecRuleEntityDecoder.getConsumerEpgId(secRule);
49         secRulesByOwnerSecGrpId.remove(ownerSecGrp, secRule);
50         secRulesByRemoteSecGrpId.remove(new OwnerAndRemoteOfSecRule(ownerSecGrp, remoteSecGrp), secRule);
51     }
52
53     static class OwnerAndRemoteOfSecRule {
54
55         private final EndpointGroupId owner;
56         private final EndpointGroupId remote;
57
58         private OwnerAndRemoteOfSecRule(EndpointGroupId owner, EndpointGroupId remote) {
59             this.owner = Preconditions.checkNotNull(owner);
60             this.remote = remote;
61         }
62
63         @Override
64         public int hashCode() {
65             final int prime = 31;
66             int result = 1;
67             result = prime * result + ((owner == null) ? 0 : owner.hashCode());
68             result = prime * result + ((remote == null) ? 0 : remote.hashCode());
69             return result;
70         }
71
72         @Override
73         public boolean equals(Object obj) {
74             if (this == obj)
75                 return true;
76             if (obj == null)
77                 return false;
78             if (getClass() != obj.getClass())
79                 return false;
80             OwnerAndRemoteOfSecRule other = (OwnerAndRemoteOfSecRule) obj;
81             if (owner == null) {
82                 if (other.owner != null)
83                     return false;
84             } else if (!owner.equals(other.owner))
85                 return false;
86             if (remote == null) {
87                 if (other.remote != null)
88                     return false;
89             } else if (!remote.equals(other.remote))
90                 return false;
91             return true;
92         }
93
94     }
95
96 }