Merge "Split resolved-policy to provided and consumed"
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / mapping / rule / SecRuleDao.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.neutron.mapper.mapping.rule;
10
11 import java.util.HashMap;
12 import java.util.Map;
13 import java.util.Set;
14
15 import javax.annotation.Nullable;
16
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;
20
21 import com.google.common.base.Preconditions;
22 import com.google.common.collect.HashMultimap;
23 import com.google.common.collect.SetMultimap;
24
25 public class SecRuleDao {
26
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<>();
31
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);
39     }
40
41     public Set<NeutronSecurityRule> getSecRulesByOwnerSecGrpId(EndpointGroupId secGrpId) {
42         return secRulesByOwnerSecGrpId.get(secGrpId);
43     }
44
45     public Set<NeutronSecurityRule> getSecRulesBySecGrpIdAndRemoteSecGrpId(EndpointGroupId ownerSecGrpId,
46             @Nullable EndpointGroupId remoteSecGrpId) {
47         return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, remoteSecGrpId));
48     }
49
50     public Set<NeutronSecurityRule> getSecRulesWithoutRemoteSecGrpBySecGrpId(EndpointGroupId ownerSecGrpId) {
51         return secRulesByRemoteSecGrpId.get(new OwnerAndRemoteOfSecRule(ownerSecGrpId, null));
52     }
53
54     public NeutronSecurityRule getSecRuleByUuid(Uuid secRule) {
55         return secRuleByUuid.get(secRule);
56     }
57
58     public Set<EndpointGroupId> getAllOwnerSecGrps() {
59         return secRulesByOwnerSecGrpId.keySet();
60     }
61
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()));
69     }
70
71     static class OwnerAndRemoteOfSecRule {
72
73         private final EndpointGroupId owner;
74         private final EndpointGroupId remote;
75
76         private OwnerAndRemoteOfSecRule(EndpointGroupId owner, EndpointGroupId remote) {
77             this.owner = Preconditions.checkNotNull(owner);
78             this.remote = remote;
79         }
80
81         @Override
82         public int hashCode() {
83             final int prime = 31;
84             int result = 1;
85             result = prime * result + ((owner == null) ? 0 : owner.hashCode());
86             result = prime * result + ((remote == null) ? 0 : remote.hashCode());
87             return result;
88         }
89
90         @Override
91         public boolean equals(Object obj) {
92             if (this == obj)
93                 return true;
94             if (obj == null)
95                 return false;
96             if (getClass() != obj.getClass())
97                 return false;
98             OwnerAndRemoteOfSecRule other = (OwnerAndRemoteOfSecRule) obj;
99             if (owner == null) {
100                 if (other.owner != null)
101                     return false;
102             } else if (!owner.equals(other.owner))
103                 return false;
104             if (remote == null) {
105                 if (other.remote != null)
106                     return false;
107             } else if (!remote.equals(other.remote))
108                 return false;
109             return true;
110         }
111
112     }
113
114 }