1 package org.opendaylight.groupbasedpolicy.neutron.mapper.mapping.rule;
5 import javax.annotation.Nullable;
7 import org.opendaylight.neutron.spi.NeutronSecurityRule;
8 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.HashMultimap;
12 import com.google.common.collect.SetMultimap;
14 public class SecRuleDao {
16 private final SetMultimap<EndpointGroupId, NeutronSecurityRule> secRulesByOwnerSecGrpId = HashMultimap.create();
17 private final SetMultimap<OwnerAndRemoteOfSecRule, NeutronSecurityRule> secRulesByRemoteSecGrpId =
18 HashMultimap.create();
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);
28 public Set<NeutronSecurityRule> getSecRulesByOwnerSecGrpId(EndpointGroupId secGrpId) {
29 return secRulesByOwnerSecGrpId.get(secGrpId);
32 public Set<NeutronSecurityRule> getSecRulesBySecGrpIdAndRemoteSecGrpId(EndpointGroupId ownerSecGrpId,
33 @Nullable EndpointGroupId remoteSecGrpId) {
34 return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, remoteSecGrpId));
37 public Set<NeutronSecurityRule> getSecRulesWithoutRemoteSecGrpBySecGrpId(EndpointGroupId ownerSecGrpId) {
38 return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, null));
41 public Set<EndpointGroupId> getAllOwnerSecGrps() {
42 return secRulesByOwnerSecGrpId.keySet();
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);
53 static class OwnerAndRemoteOfSecRule {
55 private final EndpointGroupId owner;
56 private final EndpointGroupId remote;
58 private OwnerAndRemoteOfSecRule(EndpointGroupId owner, EndpointGroupId remote) {
59 this.owner = Preconditions.checkNotNull(owner);
64 public int hashCode() {
67 result = prime * result + ((owner == null) ? 0 : owner.hashCode());
68 result = prime * result + ((remote == null) ? 0 : remote.hashCode());
73 public boolean equals(Object obj) {
78 if (getClass() != obj.getClass())
80 OwnerAndRemoteOfSecRule other = (OwnerAndRemoteOfSecRule) obj;
82 if (other.owner != null)
84 } else if (!owner.equals(other.owner))
87 if (other.remote != null)
89 } else if (!remote.equals(other.remote))