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