Convert DataChangeListeners to DataTreeChangeListeners
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / MockFaasPolicyManager.java
1 /*
2  * Copyright (c) 2016 Huawei Technologies 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.concurrent.Executor;
17 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
18 import org.opendaylight.faas.uln.datastore.api.Pair;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2FloodDomainId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.ServiceCommunicationLayer;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2BridgeDomain;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L2FloodDomain;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.L3Context;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.forwarding.context.Subnet;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.EndpointGroup;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.resolved.policies.ResolvedPolicy.ExternalImplicitGroup;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35
36 public class MockFaasPolicyManager extends FaasPolicyManager {
37
38     private final Map<EndpointGroupId, EndpointGroup> testEndpointGroups = new HashMap<>();
39     private final Map<SubnetId, Subnet> testSubnets = new HashMap<>();
40     private final Map<L2FloodDomainId, L2FloodDomain> testL2FloodDomains = new HashMap<>();
41     private final Map<L2BridgeDomainId, L2BridgeDomain> testL2BridgeDomains = new HashMap<>();
42     private final Map<L3ContextId, L3Context> testL3Contextes = new HashMap<>();
43     private final Map<ContractId, Uuid> testSecIdPerContract = new HashMap<>();
44     private final Map<TenantId, Uuid> testFaasTenantId = new HashMap<>();
45     private ServiceCommunicationLayer comLayer;
46     private ExternalImplicitGroup externalImplicitGroup;
47
48     public MockFaasPolicyManager(DataBroker dataBroker, Executor executor) {
49         super(dataBroker, executor);
50     }
51
52     // *******************************************************
53     // Test Stubs
54     // *******************************************************
55     @Override
56     public void registerTenant(TenantId tenantId, EndpointGroupId epgId) {
57         assertTrue("FaasPolicyManager.registerTenant: epgId != null", epgId != null);
58         assertTrue("FaasPolicyManager.registerTenant: tenantId != null", tenantId != null);
59
60         assertTrue("FaasPolicyManager.registerTenant: testEndpointGroups.containsKey(epgId)",
61                 testEndpointGroups.containsKey(epgId));
62
63         registeredTenants.putIfAbsent(tenantId, new ArrayList<ListenerRegistration<?>>());
64     }
65
66     @Override
67     public void registerSubnetWithEpg(EndpointGroupId epgId, TenantId tenantId, SubnetId subnetId) {
68         assertTrue("FaasPolicyManager.registerTenant: epgId != null", epgId != null);
69         assertTrue("FaasPolicyManager.registerTenant: tenantId != null", tenantId != null);
70         assertTrue("FaasPolicyManager.registerTenant: subnetId != null", subnetId != null);
71
72         assertTrue("FaasPolicyManager.registerTenant: testEndpointGroups.containsKey(epgId)",
73                 testEndpointGroups.containsKey(epgId));
74         assertTrue("FaasPolicyManager.registerTenant: registeredTenants.containsKey(tenantId)",
75                 registeredTenants.containsKey(tenantId));
76         assertTrue("FaasPolicyManager.registerTenant: testSubnets.containsKey(subnetId)",
77                 testSubnets.containsKey(subnetId));
78         List<SubnetId> subnets = epgSubnetsMap.get(new Pair<>(epgId, tenantId));
79         if (subnets == null) {
80             subnets = new ArrayList<>();
81         }
82         for (SubnetId id : subnets) {
83             if (id.equals(subnetId)) {
84                 return;
85             }
86         }
87         subnets.add(subnetId);
88         epgSubnetsMap.put(new Pair<>(epgId, tenantId), subnets);
89     }
90
91     @Override
92     public EndpointGroup readEndpointGroup(EndpointGroupId epgId, TenantId tenantId) {
93         return testEndpointGroups.get(epgId);
94     }
95
96     @Override
97     public Subnet readSubnet(SubnetId subnetId, TenantId tenantId) {
98         return testSubnets.get(subnetId);
99     }
100
101     @Override
102     protected L2FloodDomain readL2FloodDomain(L2FloodDomainId l2fId, TenantId tenantId) {
103         return testL2FloodDomains.get(l2fId);
104     }
105
106     @Override
107     protected L2BridgeDomain readL2BridgeDomainInstance(TenantId tenantId, L2BridgeDomainId l2bId) {
108         return testL2BridgeDomains.get(l2bId);
109     }
110
111     @Override
112     protected L3Context readL3ContextInstance(TenantId tenantId, L3ContextId l3cId) {
113         return testL3Contextes.get(l3cId);
114     }
115
116     @Override
117     protected boolean needToCreateLogicalNetwork(ServiceCommunicationLayer comLayer, List<SubnetId> consSubnetIds,
118             List<SubnetId> provSubnetIds, TenantId tenantId, ContractId contractId, EndpointGroup providerEpg,
119             EndpointGroup consumerEpg, ExternalImplicitGroup externalImplicitGroup) {
120         return true;
121     }
122
123     @Override
124     protected Uuid getFaasSecRulesId(ContractId contractId, TenantId gbpTenantId) {
125         return testSecIdPerContract.get(contractId);
126     }
127
128     @Override
129     public Uuid getFaasTenantId(TenantId tenantId) {
130         return testFaasTenantId.get(tenantId);
131     }
132
133     @Override
134     protected void createLayer3LogicalNetwork(EndpointGroup consEpg, ContractId contractId, EndpointGroup provEpg,
135             TenantId gbpTenantId, ServiceCommunicationLayer comLayer, ExternalImplicitGroup externalImplicitGroup) {
136         this.comLayer = comLayer;
137         this.externalImplicitGroup = externalImplicitGroup;
138         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", testEndpointGroups.containsKey(consEpg.getId()));
139         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", testEndpointGroups.containsKey(provEpg.getId()));
140         assertTrue("FaasPolicyManager.createLayer3LogicalNetwork", registeredTenants.containsKey(gbpTenantId));
141
142     }
143
144     @Override
145     protected void createLayer2LogicalNetwork(EndpointGroup consEpg, ContractId contractId, EndpointGroup provEpg,
146             TenantId gbpTenantId, ServiceCommunicationLayer comLayer, ExternalImplicitGroup externalImplicitGroup) {
147         this.comLayer = comLayer;
148         this.externalImplicitGroup = externalImplicitGroup;
149         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", testEndpointGroups.containsKey(consEpg.getId()));
150         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", testEndpointGroups.containsKey(provEpg.getId()));
151         assertTrue("FaasPolicyManager.createLayer2LogicalNetwork", registeredTenants.containsKey(gbpTenantId));
152     }
153
154     // *******************************************************
155     // The following Methods are to input test data
156     // *******************************************************
157
158     public void storeTestFaasTenantId(TenantId gbpTenantId, Uuid faasTenantId) {
159         testFaasTenantId.put(gbpTenantId, faasTenantId);
160     }
161
162     public void storeTestSecIdPerContract(ContractId contractId, Uuid secId) {
163         testSecIdPerContract.put(contractId, secId);
164     }
165
166     public void storeTestL2BridgeDomain(L2BridgeDomain brdg) {
167         if (brdg.getId() != null) {
168             testL2BridgeDomains.put(brdg.getId(), brdg);
169         }
170     }
171
172     public void storeTestL3Contextes(L3Context l3Context) {
173         if (l3Context.getId() != null) {
174             testL3Contextes.put(l3Context.getId(), l3Context);
175         }
176     }
177
178     public void storeTestL2FloodDomain(L2FloodDomain fld) {
179         if (fld.getId() != null) {
180             testL2FloodDomains.put(fld.getId(), fld);
181         }
182     }
183
184     public void storeTestSubnet(Subnet subnet) {
185         if (subnet.getId() != null) {
186             testSubnets.put(subnet.getId(), subnet);
187         }
188     }
189
190     public void storeTestEpg(EndpointGroup epg) {
191         if (epg.getId() != null) {
192             testEndpointGroups.put(epg.getId(), epg);
193         }
194     }
195
196     public ServiceCommunicationLayer getComLayer() {
197         return comLayer;
198     }
199
200     public ExternalImplicitGroup getExternalImplicitGroup() {
201         return externalImplicitGroup;
202     }
203 }