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.neutron.mapper.mapping.rule;
11 import java.util.HashMap;
15 import javax.annotation.Nullable;
17 import org.opendaylight.neutron.spi.NeutronSecurityRule;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.SetMultimap;
25 public class SecRuleDao {
27 private final SetMultimap<EndpointGroupId, NeutronSecurityRule> secRulesByOwnerSecGrpId = HashMultimap.create();
28 private final SetMultimap<OwnerAndRemoteOfSecRule, NeutronSecurityRule> secRulesByRemoteSecGrpId =
29 HashMultimap.create();
30 private final Map<Uuid, NeutronSecurityRule> secRuleByUuid = new HashMap<>();
32 public void addSecRule(NeutronSecurityRule secRule) {
33 Preconditions.checkNotNull(secRule);
34 EndpointGroupId ownerSecGrp = SecRuleEntityDecoder.getProviderEpgId(secRule);
35 EndpointGroupId remoteSecGrp = SecRuleEntityDecoder.getConsumerEpgId(secRule);
36 secRulesByOwnerSecGrpId.put(ownerSecGrp, secRule);
37 secRulesByRemoteSecGrpId.put(new OwnerAndRemoteOfSecRule(ownerSecGrp, remoteSecGrp), secRule);
38 secRuleByUuid.put(new Uuid(secRule.getID()), secRule);
41 public Set<NeutronSecurityRule> getSecRulesByOwnerSecGrpId(EndpointGroupId secGrpId) {
42 return secRulesByOwnerSecGrpId.get(secGrpId);
45 public Set<NeutronSecurityRule> getSecRulesBySecGrpIdAndRemoteSecGrpId(EndpointGroupId ownerSecGrpId,
46 @Nullable EndpointGroupId remoteSecGrpId) {
47 return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, remoteSecGrpId));
50 public Set<NeutronSecurityRule> getSecRulesWithoutRemoteSecGrpBySecGrpId(EndpointGroupId ownerSecGrpId) {
51 return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, null));
54 public NeutronSecurityRule getSecRuleByUuid(Uuid secRule) {
55 return secRuleByUuid.get(secRule);
58 public Set<EndpointGroupId> getAllOwnerSecGrps() {
59 return secRulesByOwnerSecGrpId.keySet();
62 public void removeSecRule(NeutronSecurityRule secRule) {
63 Preconditions.checkNotNull(secRule);
64 EndpointGroupId ownerSecGrp = SecRuleEntityDecoder.getProviderEpgId(secRule);
65 EndpointGroupId remoteSecGrp = SecRuleEntityDecoder.getConsumerEpgId(secRule);
66 secRulesByOwnerSecGrpId.remove(ownerSecGrp, secRule);
67 secRulesByRemoteSecGrpId.remove(new OwnerAndRemoteOfSecRule(ownerSecGrp, remoteSecGrp), secRule);
68 secRuleByUuid.remove(new Uuid(secRule.getID()));
71 static class OwnerAndRemoteOfSecRule {
73 private final EndpointGroupId owner;
74 private final EndpointGroupId remote;
76 private OwnerAndRemoteOfSecRule(EndpointGroupId owner, EndpointGroupId remote) {
77 this.owner = Preconditions.checkNotNull(owner);
82 public int hashCode() {
85 result = prime * result + ((owner == null) ? 0 : owner.hashCode());
86 result = prime * result + ((remote == null) ? 0 : remote.hashCode());
91 public boolean equals(Object obj) {
96 if (getClass() != obj.getClass())
98 OwnerAndRemoteOfSecRule other = (OwnerAndRemoteOfSecRule) obj;
100 if (other.owner != null)
102 } else if (!owner.equals(other.owner))
104 if (remote == null) {
105 if (other.remote != null)
107 } else if (!remote.equals(other.remote))