2 * Copyright (c) 2016 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
8 package org.opendaylight.groupbasedpolicy.renderer.faas;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.spy;
17 import static org.mockito.Mockito.when;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ScheduledExecutorService;
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.util.concurrent.CheckedFuture;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
29 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
30 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
31 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
32 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
33 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
34 import org.opendaylight.groupbasedpolicy.renderer.faas.test.DataChangeListenerTester;
35 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContext;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContextBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Builder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3Prefix;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3PrefixBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpoint;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedEndpointKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
58 public class FaasEndpointManagerListenerCovrgTest {
60 private static final L2BridgeDomainId L_2_BRIDGE_DOMAIN_ID = new L2BridgeDomainId("L2BridgeDomainId");
61 private static final MacAddress MAC_ADDRESS = new MacAddress("00:00:00:00:35:02");
63 private InstanceIdentifier<Endpoint> epIid;
64 private FaasEndpointManagerListener listener;
65 private TenantId gbpTenantId = new TenantId("gbpTenantId");
66 private Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
67 private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
68 private DataChangeListenerTester tester;
69 private DataBroker dataProvider;
70 private FaasPolicyManager faasPolicyManager;
72 @SuppressWarnings("unchecked")
74 public void init() throws ReadFailedException {
75 MappedEndpointKey mappedEndpointKey = new MappedEndpointKey(L_2_BRIDGE_DOMAIN_ID, MAC_ADDRESS);
77 dataProvider = mock(DataBroker.class);
79 WriteTransaction woTx = mock(WriteTransaction.class);
80 ReadWriteTransaction rwTx = mock(ReadWriteTransaction.class);
82 CheckedFuture<Void, TransactionCommitFailedException> futureVoid = mock(CheckedFuture.class);
83 when(woTx.submit()).thenReturn(futureVoid);
84 when(rwTx.submit()).thenReturn(futureVoid);
85 doNothing().when(woTx).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class),
86 any(DataObject.class));
88 CheckedFuture<Optional<MappedEndpoint>, ReadFailedException> futureMappedEndpoint = mock(CheckedFuture.class);
89 Optional<MappedEndpoint> optMappedEndpoint = mock(Optional.class);
90 when(optMappedEndpoint.isPresent()).thenReturn(false);
91 when(futureMappedEndpoint.checkedGet()).thenReturn(optMappedEndpoint);
92 when(rwTx.read(LogicalDatastoreType.OPERATIONAL,
93 FaasIidFactory.mappedEndpointIid(gbpTenantId, mappedEndpointKey))).thenReturn(futureMappedEndpoint);
95 when(dataProvider.newWriteOnlyTransaction()).thenReturn(woTx);
96 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx);
98 epIid = mock(InstanceIdentifier.class);
99 faasPolicyManager = spy(new FaasPolicyManager(dataProvider, executor));
100 doNothing().when(faasPolicyManager).removeTenantLogicalNetwork(gbpTenantId, faasTenantId);
101 listener = new FaasEndpointManagerListener(faasPolicyManager, dataProvider, executor);
102 tester = new DataChangeListenerTester(listener);
103 tester.setRemovedPath(epIid);
107 public void testOnDataChanged_Endpoint() {
108 Endpoint ep = new EndpointBuilder().setTenant(gbpTenantId)
109 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
110 .setMacAddress(MAC_ADDRESS)
112 tester.setDataObject(epIid, ep);
113 tester.callOnDataChanged();
114 listener.executeEvent(tester.getChangeMock());
118 public void testValidate() {
119 EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
120 FaasEndpointContext faasEndpointContext1 = new FaasEndpointContextBuilder().build();
121 FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
122 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
124 Endpoint ep = new EndpointBuilder()
126 assertFalse(listener.validate(ep));
128 ep = new EndpointBuilder()
129 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
131 assertFalse(listener.validate(ep));
133 ep = new EndpointBuilder()
134 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
135 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
137 assertFalse(listener.validate(ep));
139 ep = new EndpointBuilder()
140 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
141 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
142 .setMacAddress(MAC_ADDRESS)
144 assertFalse(listener.validate(ep));
146 ep = new EndpointBuilder()
147 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
148 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
149 .setMacAddress(MAC_ADDRESS)
150 .setTenant(gbpTenantId)
152 assertFalse(listener.validate(ep));
154 ep = new EndpointBuilder()
155 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
156 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
157 .setMacAddress(MAC_ADDRESS)
158 .setTenant(gbpTenantId)
159 .setEndpointGroup(endpointGroupId)
161 assertFalse(listener.validate(ep));
163 ep = new EndpointBuilder()
164 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
165 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
166 .setMacAddress(MAC_ADDRESS)
167 .setTenant(gbpTenantId)
168 .setEndpointGroup(endpointGroupId)
169 .addAugmentation(FaasEndpointContext.class, faasEndpointContext1)
171 assertFalse(listener.validate(ep));
173 ep = new EndpointBuilder()
174 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
175 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
176 .setMacAddress(MAC_ADDRESS)
177 .setTenant(gbpTenantId)
178 .setEndpointGroup(endpointGroupId)
179 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
181 assertTrue(listener.validate(ep));
185 public void testProcessEndpoint(){
186 EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
187 FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
188 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
190 Endpoint ep = new EndpointBuilder()
191 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
192 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
193 .setMacAddress(MAC_ADDRESS)
194 .setTenant(gbpTenantId)
195 .setEndpointGroup(endpointGroupId)
196 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
199 listener.processEndpoint(ep);
202 @SuppressWarnings("unchecked")
204 public void testGetFaasSubnetId() throws ReadFailedException{
205 EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
206 NetworkDomainId networkDomainId = new NetworkDomainId("network-domain-1");
207 FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
208 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
209 SubnetId subnetId = new SubnetId(networkDomainId);
210 ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
211 ReadWriteTransaction rwTx2 = mock(ReadWriteTransaction.class);
213 CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies = mock(CheckedFuture.class);
214 Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
215 when(optResolvedPolicies.isPresent()).thenReturn(false);
216 when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
217 when(roTx1.read(LogicalDatastoreType.OPERATIONAL,
218 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
220 CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet = mock(CheckedFuture.class);
221 Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
222 when(optMappedSubnet.isPresent()).thenReturn(false);
223 when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
224 when(rwTx2.read(LogicalDatastoreType.OPERATIONAL,
225 FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId))).thenReturn(futureMappedSubnet);
227 when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
228 when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx2);
230 Endpoint ep = new EndpointBuilder()
231 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
232 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
233 .setMacAddress(MAC_ADDRESS)
234 .setTenant(gbpTenantId)
235 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
238 assertNull(listener.getFaasSubnetId(ep));
240 ep = new EndpointBuilder()
241 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
242 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
243 .setMacAddress(MAC_ADDRESS)
244 .setTenant(gbpTenantId)
245 .setEndpointGroup(endpointGroupId)
246 .setNetworkContainment(networkDomainId)
247 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
250 assertNull(listener.getFaasSubnetId(ep));
254 public void testOnDataChanged_EndpointL3() {
255 EndpointL3 ep = new EndpointL3Builder().setTenant(gbpTenantId)
256 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
257 .setMacAddress(MAC_ADDRESS)
259 tester.setDataObject(epIid, ep);
260 tester.callOnDataChanged();
261 listener.executeEvent(tester.getChangeMock());
265 public void testOnDataChanged_EndpointL3Prefix() {
266 EndpointL3Prefix ep = new EndpointL3PrefixBuilder().setTenant(gbpTenantId).build();
267 tester.setDataObject(epIid, ep);
268 tester.callOnDataChanged();
269 listener.executeEvent(tester.getChangeMock());