Daniele Moro | 8d630f1 | 2021-06-15 20:53:22 +0200 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021-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 | |
| 17 | package org.onosproject.pipelines.fabric.impl.behaviour.upf; |
| 18 | |
| 19 | import org.onosproject.net.DeviceId; |
| 20 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 21 | import org.onosproject.net.pi.model.PiCounterId; |
| 22 | import org.onosproject.net.pi.model.PiMeterId; |
| 23 | import org.onosproject.net.pi.model.PiTableId; |
| 24 | import org.onosproject.net.pi.runtime.PiCounterCellHandle; |
| 25 | import org.onosproject.net.pi.runtime.PiCounterCellId; |
| 26 | import org.onosproject.net.pi.runtime.PiHandle; |
| 27 | import org.onosproject.p4runtime.api.P4RuntimeReadClient; |
| 28 | |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.List; |
| 31 | import java.util.concurrent.CompletableFuture; |
| 32 | import java.util.stream.LongStream; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | |
| 36 | /** |
| 37 | * For faking reads to a p4runtime client. Currently only used for testing |
| 38 | * UP4-specific counter reads, because all other P4 entities that UP4 reads can |
| 39 | * be read via other ONOS services. |
| 40 | */ |
| 41 | public class MockReadRequest implements P4RuntimeReadClient.ReadRequest { |
| 42 | List<PiHandle> handles; |
| 43 | DeviceId deviceId; |
| 44 | long packets; |
| 45 | long bytes; |
| 46 | int counterSize; |
| 47 | |
| 48 | public MockReadRequest(DeviceId deviceId, long packets, long bytes, int counterSize) { |
| 49 | this.handles = new ArrayList<>(); |
| 50 | this.deviceId = deviceId; |
| 51 | this.packets = packets; |
| 52 | this.bytes = bytes; |
| 53 | this.counterSize = counterSize; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public CompletableFuture<P4RuntimeReadClient.ReadResponse> submit() { |
| 58 | return CompletableFuture.completedFuture( |
| 59 | new MockReadResponse(this.handles, this.packets, this.bytes)); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public P4RuntimeReadClient.ReadResponse submitSync() { |
| 64 | return new MockReadResponse(this.handles, this.packets, this.bytes); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | @Override |
| 69 | public P4RuntimeReadClient.ReadRequest handle(PiHandle handle) { |
| 70 | this.handles.add(handle); |
| 71 | return this; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public P4RuntimeReadClient.ReadRequest handles(Iterable<? extends PiHandle> handles) { |
| 76 | checkNotNull(handles); |
| 77 | handles.forEach(this::handle); |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public P4RuntimeReadClient.ReadRequest tableEntries(PiTableId tableId) { |
| 83 | return this; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public P4RuntimeReadClient.ReadRequest tableEntries(Iterable<PiTableId> tableIds) { |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public P4RuntimeReadClient.ReadRequest defaultTableEntry(PiTableId tableId) { |
| 93 | return this; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public P4RuntimeReadClient.ReadRequest defaultTableEntry(Iterable<PiTableId> tableIds) { |
| 98 | return this; |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public P4RuntimeReadClient.ReadRequest allTableEntries() { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public P4RuntimeReadClient.ReadRequest allDefaultTableEntries() { |
| 108 | return null; |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public P4RuntimeReadClient.ReadRequest actionProfileGroups(PiActionProfileId actionProfileId) { |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public P4RuntimeReadClient.ReadRequest actionProfileGroups(Iterable<PiActionProfileId> actionProfileIds) { |
| 118 | return this; |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public P4RuntimeReadClient.ReadRequest actionProfileMembers(PiActionProfileId actionProfileId) { |
| 123 | return this; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public P4RuntimeReadClient.ReadRequest actionProfileMembers(Iterable<PiActionProfileId> actionProfileIds) { |
| 128 | return this; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public P4RuntimeReadClient.ReadRequest counterCells(PiCounterId counterId) { |
| 133 | return this; |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public P4RuntimeReadClient.ReadRequest counterCells(Iterable<PiCounterId> counterIds) { |
| 138 | counterIds.forEach(counterId -> { |
| 139 | LongStream.range(0, this.counterSize) |
| 140 | .forEach(index -> { |
| 141 | PiCounterCellId cellId = |
| 142 | PiCounterCellId.ofIndirect(counterId, index); |
| 143 | PiCounterCellHandle handle = |
| 144 | PiCounterCellHandle.of(this.deviceId, cellId); |
| 145 | this.handle(handle); |
| 146 | }); |
| 147 | }); |
| 148 | return this; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public P4RuntimeReadClient.ReadRequest directCounterCells(PiTableId tableId) { |
| 153 | return this; |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public P4RuntimeReadClient.ReadRequest directCounterCells(Iterable<PiTableId> tableIds) { |
| 158 | return this; |
| 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public P4RuntimeReadClient.ReadRequest meterCells(PiMeterId meterId) { |
| 163 | return this; |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public P4RuntimeReadClient.ReadRequest meterCells(Iterable<PiMeterId> meterIds) { |
| 168 | return this; |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public P4RuntimeReadClient.ReadRequest directMeterCells(PiTableId tableId) { |
| 173 | return this; |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public P4RuntimeReadClient.ReadRequest directMeterCells(Iterable<PiTableId> tableIds) { |
| 178 | return this; |
| 179 | } |
| 180 | } |