BUG-6650: ep-ip/sgt, propose initial template provider api
[groupbasedpolicy.git] / sxp-integration / sxp-ep-provider / src / test / java / org / opendaylight / groupbasedpolicy / sxp / ep / provider / impl / dao / EPPolicyTemplateDaoImplTest.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
9 package org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.dao;
10
11 import com.google.common.base.Optional;
12 import com.google.common.collect.Iterables;
13 import com.google.common.collect.Lists;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.InOrder;
26 import org.mockito.Matchers;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.Spy;
30 import org.mockito.runners.MockitoJUnitRunner;
31 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
32 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
33 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
34 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
35 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.SimpleCachedDao;
36 import org.opendaylight.groupbasedpolicy.sxp.ep.provider.impl.util.EPTemplateUtil;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ConditionName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.SxpEpMapper;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgt;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgtBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.groupbasedpolicy.sxp.integration.sxp.ep.provider.model.rev160302.sxp.ep.mapper.EndpointPolicyTemplateBySgtKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.sxp.database.rev160308.Sgt;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47
48 /**
49  * Test for {@link EPPolicyTemplateDaoImpl}.
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class EPPolicyTemplateDaoImplTest {
53
54     private static final Sgt KEY_1 = new Sgt(1);
55     private final EndpointPolicyTemplateBySgt EP_POLICY_TEMPLATE_VALUE;
56     @Mock
57     private DataBroker dataBroker;
58     @Mock
59     private SimpleCachedDao<Sgt, EndpointPolicyTemplateBySgt> cachedDao;
60     @Mock
61     private ReadOnlyTransaction rTx;
62     @Spy
63     private EpPolicyTemplateValueKeyFactory keyFactory = new EpPolicyTemplateValueKeyFactory(
64             EPTemplateUtil.createEndpointGroupIdOrdering(), EPTemplateUtil.createConditionNameOrdering());
65     @Captor
66     ArgumentCaptor<Sgt> sgtCapt;
67     @Captor
68     ArgumentCaptor<EndpointPolicyTemplateBySgt> epPolicyTemplateCapt;
69
70     private EPPolicyTemplateDaoImpl dao;
71
72     public EPPolicyTemplateDaoImplTest() {
73         EP_POLICY_TEMPLATE_VALUE = new EndpointPolicyTemplateBySgtBuilder()
74                 .setSgt(KEY_1)
75                 .build();
76     }
77
78     @Before
79     public void setUp() throws Exception {
80         dao = new EPPolicyTemplateDaoImpl(dataBroker, cachedDao, keyFactory);
81     }
82
83     @Test
84     public void testRead_absent() throws Exception {
85         Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(Optional.<EndpointPolicyTemplateBySgt>absent());
86         Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
87         Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
88                 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
89                 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
90                         Optional.<EndpointPolicyTemplateBySgt>absent()));
91
92
93         final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
94         Assert.assertTrue(read.isDone());
95         Assert.assertFalse(read.get().isPresent());
96     }
97
98     @Test
99     public void testRead_presentCached() throws Exception {
100         Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(Optional.of(EP_POLICY_TEMPLATE_VALUE));
101
102         final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
103         Assert.assertTrue(read.isDone());
104         Assert.assertTrue(read.get().isPresent());
105         Assert.assertEquals(KEY_1, read.get().get().getSgt());
106     }
107
108     @Test
109     public void testRead_presentDS() throws Exception {
110         Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(
111                 Optional.<EndpointPolicyTemplateBySgt>absent());
112         Mockito.when(cachedDao.isEmpty()).thenReturn(true);
113         Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
114         Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
115                 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
116                 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
117                         Optional.of(EP_POLICY_TEMPLATE_VALUE)));
118         Mockito.doCallRealMethod().when(keyFactory).sortValueKeyLists(Matchers.<EndpointPolicyTemplateBySgt>any());
119
120         final ListenableFuture<Optional<EndpointPolicyTemplateBySgt>> read = dao.read(KEY_1);
121         Assert.assertTrue(read.isDone());
122         Assert.assertTrue(read.get().isPresent());
123         Assert.assertEquals(KEY_1, read.get().get().getSgt());
124
125         final InOrder inOrder = Mockito.inOrder(cachedDao);
126         inOrder.verify(cachedDao).update(KEY_1, EP_POLICY_TEMPLATE_VALUE);
127         inOrder.verifyNoMoreInteractions();
128     }
129
130     @Test
131     public void testBuildReadPath() throws Exception {
132         final KeyedInstanceIdentifier<EndpointPolicyTemplateBySgt, EndpointPolicyTemplateBySgtKey> expectedPath =
133                 InstanceIdentifier.create(SxpEpMapper.class)
134                         .child(EndpointPolicyTemplateBySgt.class, new EndpointPolicyTemplateBySgtKey(KEY_1));
135
136         final InstanceIdentifier<EndpointPolicyTemplateBySgt> readPath = dao.buildReadPath(KEY_1);
137         Assert.assertEquals(expectedPath, readPath);
138     }
139
140     @Test
141     public void testReadBy_single() throws Exception {
142         final EpPolicyTemplateValueKey key = new EpPolicyTemplateValueKey(new TenantId("tn1"),
143                 buildEndpointGroupIds(new String[]{"epg1", "epg2"}),
144                 buildConditions(new String[]{"cn1", "cn2"}));
145
146         Mockito.doCallRealMethod().when(keyFactory).createKeyWithDefaultOrdering(Matchers.<EndpointPolicyTemplateBySgt>any());
147
148         Mockito.when(cachedDao.values()).thenReturn(Lists.newArrayList(
149                 createEpPolicytemplate(new Sgt(1), new String[]{"cn2", "cn1"}, new String[]{"epg1", "epg2"}, "tn1"),
150                 createEpPolicytemplate(new Sgt(2), new String[]{"cn1", "cn2"}, new String[]{"epg2", "epg1"}, "tn1"),
151                 createEpPolicytemplate(new Sgt(3), new String[]{"cn2", "cn1"}, new String[]{"epg2", "epg1"}, "tn1"),
152                 createEpPolicytemplate(new Sgt(4), new String[]{"cn1", "cn2"}, new String[]{"epg1", "epg2"}, "tn1")
153         ));
154
155         final Collection<EndpointPolicyTemplateBySgt> policyTemplates = dao.readBy(key);
156         Assert.assertEquals(1, policyTemplates.size());
157         Assert.assertEquals(4, Iterables.getFirst(policyTemplates, null).getSgt().getValue().intValue());
158     }
159
160     @Test
161     public void testRead_unsortedLists() throws Exception {
162         final EndpointPolicyTemplateBySgt epPolicytemplateUnsorted = createEpPolicytemplate(new Sgt(1),
163                 new String[]{"cn2", "cn1"}, new String[]{"epg2", "epg1"}, "tn1");
164
165         Mockito.doCallRealMethod().when(keyFactory).createKeyWithDefaultOrdering(Matchers.<EndpointPolicyTemplateBySgt>any());
166
167         Mockito.when(cachedDao.find(Matchers.<Sgt>any())).thenReturn(
168                 Optional.<EndpointPolicyTemplateBySgt>absent());
169         Mockito.when(cachedDao.isEmpty()).thenReturn(true);
170         Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
171
172         Mockito.when(rTx.read(Matchers.eq(LogicalDatastoreType.CONFIGURATION),
173                 Matchers.<InstanceIdentifier<EndpointPolicyTemplateBySgt>>any())).thenReturn(
174                 Futures.<Optional<EndpointPolicyTemplateBySgt>, ReadFailedException>immediateCheckedFuture(
175                         Optional.of(epPolicytemplateUnsorted)));
176
177         dao.read(new Sgt(1));
178
179         Mockito.verify(cachedDao).update(sgtCapt.capture(), epPolicyTemplateCapt.capture());
180         Mockito.verify(cachedDao).find(sgtCapt.capture());
181
182         Assert.assertEquals(1, sgtCapt.getValue().getValue().intValue());
183         final EndpointPolicyTemplateBySgt template = epPolicyTemplateCapt.getValue();
184         Assert.assertEquals(1, template.getSgt().getValue().intValue());
185         Assert.assertEquals("tn1", template.getTenant().getValue());
186         Assert.assertEquals(buildEndpointGroupIds(new String[]{"epg1", "epg2"}), template.getEndpointGroups());
187         Assert.assertEquals(buildConditions(new String[]{"cn1", "cn2"}), template.getConditions());
188     }
189
190
191     private EndpointPolicyTemplateBySgt createEpPolicytemplate(final Sgt sgt, final String[] conditionNames,
192                                                                final String[] epgIds, final String tenant) {
193         return new EndpointPolicyTemplateBySgtBuilder()
194                 .setSgt(sgt)
195                 .setEndpointGroups(buildEndpointGroupIds(epgIds))
196                 .setConditions(buildConditions(conditionNames))
197                 .setTenant(new TenantId(tenant))
198                 .build();
199     }
200
201     private static List<EndpointGroupId> buildEndpointGroupIds(final String[] names) {
202         final List<EndpointGroupId> endpointGroupIds = new ArrayList<>();
203         for (String epgId : names) {
204             endpointGroupIds.add(new EndpointGroupId(epgId));
205         }
206         return endpointGroupIds;
207     }
208
209     private static List<ConditionName> buildConditions(final String[] names) {
210         final List<ConditionName> conditions = new ArrayList<>();
211         for (String condition : names) {
212             conditions.add(new ConditionName(condition));
213         }
214         return conditions;
215     }
216 }