2 * Copyright (c) 2015 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
9 package org.opendaylight.groupbasedpolicy.statistics;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.verify;
13 import static org.mockito.Mockito.when;
15 import java.util.ArrayList;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.concurrent.ExecutionException;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
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.WriteTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
32 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
33 import org.opendaylight.groupbasedpolicy.util.IidFactory;
34 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ActionName;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ClassifierName;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContractId;
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.L2BridgeDomainId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.RuleName;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.SubjectName;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.TenantId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.StatRecords;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.StatRecordsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.EpToEpStatisticKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatistic;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.EpEpgToEpEpgStatisticKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.ep.epg.to.ep.epg.statistic.MatchedRuleStatisticBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.rev151215.statistic.records.stat.records.ep.to.ep.statistic.ep.epg.to.ep.epg.statistic.MatchedRuleStatisticKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.RecordId;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.StatisticsStore;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.StatisticsStoreBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.dst.ep.fields.DstEndpointBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.source.ep.fields.SrcEndpointBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.StatisticRecord;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.StatisticRecordBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.StatisticRecordKey;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.statistic.record.StatisticBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.statistics.store.rev151215.statistics.store.statistic.record.StatisticKey;
62 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
64 public class StatisticManagerImplTest {
67 public ExpectedException thrown = ExpectedException.none();
69 private static final String READING_EXCEPTION_MSG = "Reading throws exception";
71 private DataBroker dataBroker;
72 private StatisticsManagerImpl manager;
73 private WriteTransaction wtx;
74 private ReadOnlyTransaction rtx;
75 private MacAddress srcMac;
76 private MacAddress dstMac;
77 private L2BridgeDomainId srcL2C;
78 private L2BridgeDomainId dstL2C;
79 private EndpointGroupId srcEPG;
80 private EndpointGroupId dstEPG;
81 private TenantId srcTenant;
82 private TenantId dstTenant;
83 private ContractId contract;
84 private SubjectName subject;
85 private RuleName rule;
86 private ActionName action;
87 private ClassifierName classifier;
91 srcMac = new MacAddress("00:00:00:00:00:01");
92 dstMac = new MacAddress("00:00:00:00:00:02");
93 srcL2C = new L2BridgeDomainId("srcL2C");
94 dstL2C = new L2BridgeDomainId("dstL2C");
95 srcEPG = new EndpointGroupId("srcEPG");
96 dstEPG = new EndpointGroupId("dstEPG");
97 srcTenant = new TenantId("srcTenant");
98 dstTenant = new TenantId("dstTenant");
99 contract = new ContractId("contract");
100 subject = new SubjectName("subject");
101 rule = new RuleName("rule");
102 action = new ActionName("action");
103 classifier = new ClassifierName("classifier");
105 dataBroker = mock(DataBroker.class);
106 wtx = mock(WriteTransaction.class);
107 rtx = mock(ReadOnlyTransaction.class);
108 when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
109 when(dataBroker.newReadOnlyTransaction()).thenReturn(rtx);
111 manager = new StatisticsManagerImpl(dataBroker);
115 public void testWriteStat() {
116 StatRecords input = inputForWriting();
117 StatisticRecordKey key = new StatisticRecordKey(new RecordId(0L));
118 StatisticRecord output = outputForWriting(key);
120 CheckedFuture<Void, TransactionCommitFailedException> future = mock(CheckedFuture.class);
121 when(wtx.submit()).thenReturn(future);
122 when(dataBroker.newWriteOnlyTransaction()).thenReturn(wtx);
124 manager.writeStat(input);
125 verify(wtx).put(LogicalDatastoreType.OPERATIONAL, IidFactory.statisticRecordIid(key), output, true);
129 public void testReadStats() throws Exception {
130 List<StatisticRecord> stats = inputForReading();
131 StatRecords statRecords = outputForReading();
133 CheckedFuture<Optional<StatisticsStore>, ReadFailedException> future = mock(CheckedFuture.class);
134 when(future.get()).thenReturn(Optional.of(new StatisticsStoreBuilder().setStatisticRecord(stats).build()));
135 when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build()))
138 StatRecords read = manager.readStats();
140 Assert.assertEquals(statRecords, read);
144 public void testReadStats_throwsException() throws ExecutionException, InterruptedException {
145 CheckedFuture<Optional<StatisticsStore>, ReadFailedException> future = mock(CheckedFuture.class);
146 when(future.get()).thenThrow(new RuntimeException(READING_EXCEPTION_MSG));
147 when(rtx.read(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(StatisticsStore.class).build()))
150 thrown.expect(RuntimeException.class);
151 thrown.expectMessage(READING_EXCEPTION_MSG);
155 private List<StatisticRecord> inputForReading() {
156 SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder();
157 DstEndpointBuilder dstBuilder = new DstEndpointBuilder();
158 srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant);
159 dstBuilder.setMacAddress(dstMac).setL2Context(dstL2C).setTenant(dstTenant);
160 srcBuilder.setEndpointGroup(srcEPG);
161 dstBuilder.setEndpointGroup(dstEPG);
162 StatisticBuilder statBuilder = new StatisticBuilder().setKey(new StatisticKey(contract, rule, subject))
163 .setContract(contract)
166 .setClassifier(Collections.singletonList(classifier))
167 .setAction(Collections.singletonList(action))
170 StatisticRecordKey key = new StatisticRecordKey(new RecordId(0L));
171 StatisticRecordBuilder statRecord = new StatisticRecordBuilder().setKey(key)
172 .setStatistic(Collections.singletonList(statBuilder.build()))
173 .setSrcEndpoint(srcBuilder.build())
174 .setDstEndpoint(dstBuilder.build());
175 List<StatisticRecord> stats = new ArrayList<>();
176 stats.add(statRecord.build());
177 statRecord.setKey(new StatisticRecordKey(new RecordId(1L)));
178 stats.add(statRecord.build());
183 private StatRecords outputForReading() {
184 StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
185 EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(50, 10);
186 EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build()));
187 recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
189 return recordsBuilder.build();
192 private StatRecords inputForWriting() {
193 StatRecordsBuilder recordsBuilder = new StatRecordsBuilder();
194 EpEpgToEpEpgStatisticBuilder epgBuilder = newEpEpgToEpEpgStatisticBuilder(25, 5);
195 EpToEpStatisticBuilder epBuilder = newEpToEpStatisticBuilder(Collections.singletonList(epgBuilder.build()));
196 recordsBuilder.setEpToEpStatistic(Collections.singletonList(epBuilder.build()));
198 return recordsBuilder.build();
201 private StatisticRecord outputForWriting(StatisticRecordKey key) {
202 SrcEndpointBuilder srcBuilder = new SrcEndpointBuilder();
203 DstEndpointBuilder dstBuilder = new DstEndpointBuilder();
204 srcBuilder.setMacAddress(srcMac).setL2Context(srcL2C).setTenant(srcTenant);
205 dstBuilder.setMacAddress(dstMac).setL2Context(dstL2C).setTenant(dstTenant);
206 srcBuilder.setEndpointGroup(srcEPG);
207 dstBuilder.setEndpointGroup(dstEPG);
208 StatisticBuilder statBuilder = new StatisticBuilder().setKey(new StatisticKey(contract, rule, subject))
209 .setContract(contract)
212 .setClassifier(Collections.singletonList(classifier))
213 .setAction(Collections.singletonList(action))
216 StatisticRecordBuilder statRecord = new StatisticRecordBuilder().setKey(key)
217 .setStatistic(Collections.singletonList(statBuilder.build()))
218 .setSrcEndpoint(srcBuilder.build())
219 .setDstEndpoint(dstBuilder.build());
221 return statRecord.build();
224 private EpEpgToEpEpgStatisticBuilder newEpEpgToEpEpgStatisticBuilder(long byteCount, long packetCount) {
225 return new EpEpgToEpEpgStatisticBuilder().setSrcEpg(srcEPG)
227 .setKey(new EpEpgToEpEpgStatisticKey(dstEPG, srcEPG))
228 .setMatchedRuleStatistic(Collections.singletonList(
229 new MatchedRuleStatisticBuilder().setKey(new MatchedRuleStatisticKey(contract, rule, subject))
230 .setContract(contract)
232 .setMatchedRule(rule)
233 .setAction(Collections.singletonList(action))
234 .setClassifier(Collections.singletonList(classifier))
235 .setByteCount(byteCount)
236 .setPacketCount(packetCount)
240 private EpToEpStatisticBuilder newEpToEpStatisticBuilder(List<EpEpgToEpEpgStatistic> list) {
241 return new EpToEpStatisticBuilder().setSrcMacAddress(srcMac)
242 .setDstMacAddress(dstMac)
245 .setSrcTenant(srcTenant)
246 .setDstTenant(dstTenant)
247 .setKey(new EpToEpStatisticKey(dstL2C, dstMac, srcL2C, srcMac))
248 .setEpEpgToEpEpgStatistic(list);