Convert DataChangeListeners to DataTreeChangeListeners
[groupbasedpolicy.git] / renderers / faas / src / test / java / org / opendaylight / groupbasedpolicy / renderer / faas / FaasEndpointManagerListenerCovrgTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.groupbasedpolicy.renderer.faas;
9
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.Matchers.eq;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import com.google.common.base.Optional;
22 import com.google.common.collect.ImmutableList;
23 import com.google.common.util.concurrent.CheckedFuture;
24 import com.google.common.util.concurrent.MoreExecutors;
25 import java.util.Collection;
26 import java.util.Collections;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentCaptor;
30 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
31 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
33 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
34 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
35 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
36 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
39 import org.opendaylight.groupbasedpolicy.util.IidFactory;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContext;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.faas.endpoint.rev151009.FaasEndpointContextBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.faas.logical.faas.common.rev151013.Uuid;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L2BridgeDomainId;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.NetworkDomainId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubnetId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoint.fields.L3AddressBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.faas.rev151009.mapped.tenants.entities.mapped.entity.MappedSubnet;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.resolved.policy.rev150828.ResolvedPolicies;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.yang.binding.DataObject;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57
58 public class FaasEndpointManagerListenerCovrgTest {
59
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");
62
63     private FaasEndpointManagerListener listener;
64     private final TenantId gbpTenantId = new TenantId("gbpTenantId");
65     private final Uuid faasTenantId = new Uuid("b4511aac-ae43-11e5-bf7f-feff819cdc9f");
66     private final DataBroker dataProvider = mock(DataBroker.class);
67     private final FaasPolicyManager faasPolicyManager = mock(FaasPolicyManager.class);
68
69     @SuppressWarnings("unchecked")
70     @Before
71     public void init() {
72         doNothing().when(faasPolicyManager).removeTenantLogicalNetwork(gbpTenantId, faasTenantId);
73         doNothing().when(faasPolicyManager).registerTenant(any(TenantId.class), any(EndpointGroupId.class));
74
75         listener = new FaasEndpointManagerListener(faasPolicyManager, dataProvider, MoreExecutors.directExecutor());
76
77         doReturn(mock(ListenerRegistration.class)).when(dataProvider).registerDataTreeChangeListener(
78                 any(DataTreeIdentifier.class), any(DataTreeChangeListener.class));
79     }
80
81     @SuppressWarnings({ "rawtypes", "unchecked" })
82     @Test
83     public void testOnDataChanged_Endpoint() {
84         ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
85         verify(dataProvider).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
86                 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class))),
87                 dtclCaptor.capture());
88
89         Endpoint endpoint = new EndpointBuilder().setTenant(gbpTenantId)
90             .setL2Context(L_2_BRIDGE_DOMAIN_ID)
91             .setMacAddress(MAC_ADDRESS)
92             .setL3Address(Collections.emptyList())
93             .setEndpointGroup(new EndpointGroupId("test"))
94             .addAugmentation(FaasEndpointContext.class, new FaasEndpointContextBuilder().setFaasPortRefId(
95                     new Uuid("12345678-ae43-11e5-bf7f-feff819cdc9f")).build())
96             .build();
97
98         dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(null, endpoint,
99                 DataObjectModification.ModificationType.WRITE));
100
101         verify(faasPolicyManager).registerTenant(endpoint.getTenant(), endpoint.getEndpointGroup());
102     }
103
104     @Test
105     public void testValidate() {
106         EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
107         FaasEndpointContext faasEndpointContext1 = new FaasEndpointContextBuilder().build();
108         FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
109                 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
110
111         Endpoint ep = new EndpointBuilder()
112                 .build();
113         assertFalse(listener.validate(ep));
114
115         ep = new EndpointBuilder()
116                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
117                 .build();
118         assertFalse(listener.validate(ep));
119
120         ep = new EndpointBuilder()
121                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
122                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
123                 .build();
124         assertFalse(listener.validate(ep));
125
126         ep = new EndpointBuilder()
127                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
128                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
129                 .setMacAddress(MAC_ADDRESS)
130                 .build();
131         assertFalse(listener.validate(ep));
132
133         ep = new EndpointBuilder()
134                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
135                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
136                 .setMacAddress(MAC_ADDRESS)
137                 .setTenant(gbpTenantId)
138                 .build();
139         assertFalse(listener.validate(ep));
140
141         ep = new EndpointBuilder()
142                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
143                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
144                 .setMacAddress(MAC_ADDRESS)
145                 .setTenant(gbpTenantId)
146                 .setEndpointGroup(endpointGroupId)
147                 .build();
148         assertFalse(listener.validate(ep));
149
150         ep = new EndpointBuilder()
151                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
152                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
153                 .setMacAddress(MAC_ADDRESS)
154                 .setTenant(gbpTenantId)
155                 .setEndpointGroup(endpointGroupId)
156                 .addAugmentation(FaasEndpointContext.class, faasEndpointContext1)
157                 .build();
158         assertFalse(listener.validate(ep));
159
160         ep = new EndpointBuilder()
161                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
162                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
163                 .setMacAddress(MAC_ADDRESS)
164                 .setTenant(gbpTenantId)
165                 .setEndpointGroup(endpointGroupId)
166                 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
167                 .build();
168         assertTrue(listener.validate(ep));
169     }
170
171     @Test
172     public void testProcessEndpoint(){
173         EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
174         FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
175                 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
176
177         Endpoint ep = new EndpointBuilder()
178                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
179                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
180                 .setMacAddress(MAC_ADDRESS)
181                 .setTenant(gbpTenantId)
182                 .setEndpointGroup(endpointGroupId)
183                 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
184                 .build();
185
186         listener.processEndpoint(ep);
187     }
188
189     @SuppressWarnings("unchecked")
190     @Test
191     public void testGetFaasSubnetId() throws ReadFailedException{
192         EndpointGroupId endpointGroupId = new EndpointGroupId("epg-1");
193         NetworkDomainId networkDomainId = new NetworkDomainId("network-domain-1");
194         FaasEndpointContext faasEndpointContext2 = new FaasEndpointContextBuilder()
195                 .setFaasPortRefId(new Uuid("c4511aac-ae43-11e5-bf7f-feff819cdc9f")).build();
196         SubnetId subnetId = new SubnetId(networkDomainId);
197         ReadOnlyTransaction roTx1 = mock(ReadOnlyTransaction.class);
198         ReadWriteTransaction rwTx2 = mock(ReadWriteTransaction.class);
199
200         CheckedFuture<Optional<ResolvedPolicies>, ReadFailedException> futureResolvedPolicies = mock(CheckedFuture.class);
201         Optional<ResolvedPolicies> optResolvedPolicies = mock(Optional.class);
202         when(optResolvedPolicies.isPresent()).thenReturn(false);
203         when(futureResolvedPolicies.checkedGet()).thenReturn(optResolvedPolicies);
204         when(roTx1.read(LogicalDatastoreType.OPERATIONAL,
205                 InstanceIdentifier.builder(ResolvedPolicies.class).build())).thenReturn(futureResolvedPolicies);
206
207         CheckedFuture<Optional<MappedSubnet>, ReadFailedException> futureMappedSubnet = mock(CheckedFuture.class);
208         Optional<MappedSubnet> optMappedSubnet = mock(Optional.class);
209         when(optMappedSubnet.isPresent()).thenReturn(false);
210         when(futureMappedSubnet.checkedGet()).thenReturn(optMappedSubnet);
211         when(rwTx2.read(LogicalDatastoreType.OPERATIONAL,
212                 FaasIidFactory.mappedSubnetIid(gbpTenantId, subnetId))).thenReturn(futureMappedSubnet);
213
214         when(dataProvider.newReadOnlyTransaction()).thenReturn(roTx1);
215         when(dataProvider.newReadWriteTransaction()).thenReturn(rwTx2);
216
217         Endpoint ep = new EndpointBuilder()
218                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
219                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
220                 .setMacAddress(MAC_ADDRESS)
221                 .setTenant(gbpTenantId)
222                 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
223                 .build();
224
225         assertNull(listener.getFaasSubnetId(ep));
226
227         ep = new EndpointBuilder()
228                 .setL2Context(L_2_BRIDGE_DOMAIN_ID)
229                 .setL3Address(ImmutableList.of(new L3AddressBuilder().build()))
230                 .setMacAddress(MAC_ADDRESS)
231                 .setTenant(gbpTenantId)
232                 .setEndpointGroup(endpointGroupId)
233                 .setNetworkContainment(networkDomainId)
234                 .addAugmentation(FaasEndpointContext.class, faasEndpointContext2)
235                 .build();
236
237         assertNull(listener.getFaasSubnetId(ep));
238     }
239
240     @SuppressWarnings("unchecked")
241     private static <T extends DataObject> Collection<DataTreeModification<T>> newMockDataTreeModification(T dataBefore,
242             T dataAfter, DataObjectModification.ModificationType type) {
243         DataTreeModification<T> mockDataTreeModification = mock(DataTreeModification.class);
244         DataObjectModification<T> mockModification = mock(DataObjectModification.class);
245         doReturn(type).when(mockModification).getModificationType();
246         doReturn(dataBefore).when(mockModification).getDataBefore();
247         doReturn(dataAfter).when(mockModification).getDataAfter();
248         doReturn(mockModification).when(mockDataTreeModification).getRootNode();
249
250         return Collections.singletonList(mockDataTreeModification);
251     }
252 }