Andrea Campanella | cbbb795 | 2019-11-25 06:38:41 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.opencord.olt.impl; |
| 17 | |
| 18 | import com.google.common.collect.HashMultimap; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Multimaps; |
| 21 | import com.google.common.collect.Sets; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.net.DeviceId; |
| 25 | import org.onosproject.net.meter.DefaultMeter; |
| 26 | import org.onosproject.net.meter.Meter; |
| 27 | import org.onosproject.net.meter.MeterId; |
| 28 | import org.onosproject.net.meter.MeterKey; |
| 29 | import org.onosproject.net.meter.MeterListener; |
| 30 | import org.onosproject.net.meter.MeterRequest; |
| 31 | import org.opencord.sadis.BandwidthProfileInformation; |
| 32 | |
| 33 | import java.util.Collection; |
| 34 | import java.util.concurrent.CompletableFuture; |
| 35 | |
| 36 | public class OltMeterTest extends TestBase { |
| 37 | private OltMeterService oltMeterService; |
| 38 | |
| 39 | private BandwidthProfileInformation bandwidthProfileInformation = new BandwidthProfileInformation(); |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() { |
| 43 | oltMeterService = new OltMeterService(); |
| 44 | oltMeterService.bpInfoToMeter = Multimaps.synchronizedSetMultimap(HashMultimap.create()); |
| 45 | oltMeterService.programmedMeters = Sets.newConcurrentHashSet(); |
| 46 | oltMeterService.meterService = new MockMeterService(); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void testAddAndGetMeterIdToBpMapping() { |
| 51 | oltMeterService.addMeterIdToBpMapping(DEVICE_ID_1, usMeterId, usBpId); |
| 52 | MeterId usMeterId = oltMeterService.getMeterIdFromBpMapping(DEVICE_ID_1, usBpId); |
| 53 | assert usMeterId.equals(this.usMeterId); |
| 54 | |
| 55 | oltMeterService.addMeterIdToBpMapping(DEVICE_ID_1, dsMeterId, dsBpId); |
| 56 | MeterId dsMeterId = oltMeterService.getMeterIdFromBpMapping(DEVICE_ID_1, dsBpId); |
| 57 | assert dsMeterId.equals(this.dsMeterId); |
| 58 | |
| 59 | ImmutableMap<String, Collection<MeterKey>> meterMappings = oltMeterService.getBpMeterMappings(); |
| 60 | assert meterMappings.size() == 2; |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testCreateMeter() { |
| 65 | //with provided bandwidth profile information |
| 66 | bandwidthProfileInformation.setId(usBpId); |
| 67 | bandwidthProfileInformation.setExceededInformationRate(10000); |
| 68 | bandwidthProfileInformation.setExceededBurstSize(10000L); |
| 69 | bandwidthProfileInformation.setCommittedBurstSize(10000L); |
| 70 | bandwidthProfileInformation.setCommittedInformationRate(10000); |
| 71 | |
| 72 | oltMeterService.addMeterIdToBpMapping(DEVICE_ID_1, usMeterId, usBpId); |
| 73 | |
| 74 | |
| 75 | MeterId meterId = |
| 76 | oltMeterService.createMeter(DEVICE_ID_1, bandwidthProfileInformation, new CompletableFuture<>()); |
| 77 | assert meterId != null; |
| 78 | |
| 79 | //with null bandwidth profile information |
| 80 | meterId = oltMeterService.createMeter(DEVICE_ID_1, null, new CompletableFuture<>()); |
| 81 | assert meterId == null; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | private class MockMeterService implements org.onosproject.net.meter.MeterService { |
| 86 | @Override |
| 87 | public Meter submit(MeterRequest meterRequest) { |
| 88 | return DefaultMeter.builder() |
| 89 | .forDevice(DEVICE_ID_1) |
| 90 | .fromApp(appId) |
| 91 | .withId(usMeterId) |
| 92 | .build(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void withdraw(MeterRequest meterRequest, MeterId meterId) { |
| 97 | |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Meter getMeter(DeviceId deviceId, MeterId meterId) { |
| 102 | return null; |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Collection<Meter> getAllMeters() { |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Collection<Meter> getMeters(DeviceId deviceId) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public MeterId allocateMeterId(DeviceId deviceId) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void freeMeterId(DeviceId deviceId, MeterId meterId) { |
| 122 | |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void addListener(MeterListener meterListener) { |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void removeListener(MeterListener meterListener) { |
| 132 | |
| 133 | } |
| 134 | } |
| 135 | } |