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.pi.runtime.PiCounterCell; |
| 20 | import org.onosproject.net.pi.runtime.PiCounterCellData; |
| 21 | import org.onosproject.net.pi.runtime.PiCounterCellHandle; |
| 22 | import org.onosproject.net.pi.runtime.PiEntity; |
| 23 | import org.onosproject.net.pi.runtime.PiEntityType; |
| 24 | import org.onosproject.net.pi.runtime.PiHandle; |
| 25 | import org.onosproject.p4runtime.api.P4RuntimeReadClient; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.List; |
| 30 | |
| 31 | import static com.google.common.base.Preconditions.checkNotNull; |
| 32 | |
| 33 | /** |
| 34 | * For faking reads to a p4runtime client. Currently only used for testing |
| 35 | * UP4-specific counter reads, because all other P4 entities that UP4 reads can |
| 36 | * be read via other ONOS services. |
| 37 | */ |
| 38 | public class MockReadResponse implements P4RuntimeReadClient.ReadResponse { |
| 39 | List<PiEntity> entities; |
| 40 | long packets; |
| 41 | long bytes; |
| 42 | |
| 43 | public MockReadResponse(Iterable<? extends PiHandle> handles, long packets, long bytes) { |
| 44 | this.entities = new ArrayList<>(); |
| 45 | this.packets = packets; |
| 46 | this.bytes = bytes; |
| 47 | checkNotNull(handles); |
| 48 | handles.forEach(this::handle); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public boolean isSuccess() { |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | public MockReadResponse handle(PiHandle handle) { |
| 57 | if (handle.entityType().equals(PiEntityType.COUNTER_CELL)) { |
| 58 | PiCounterCellHandle counterHandle = (PiCounterCellHandle) handle; |
| 59 | PiCounterCellData data = |
| 60 | new PiCounterCellData(this.packets, this.bytes); |
| 61 | PiEntity entity = new PiCounterCell(counterHandle.cellId(), data); |
| 62 | this.entities.add(entity); |
| 63 | } |
| 64 | // Only handles counter cell so far |
| 65 | |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public Collection<PiEntity> all() { |
| 71 | return this.entities; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public <E extends PiEntity> Collection<E> all(Class<E> clazz) { |
| 76 | List<E> results = new ArrayList<>(); |
| 77 | this.entities.forEach(ent -> { |
| 78 | if (ent.getClass().equals(clazz)) { |
| 79 | results.add(clazz.cast(ent)); |
| 80 | } |
| 81 | }); |
| 82 | return results; |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String explanation() { |
| 87 | return null; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Throwable throwable() { |
| 92 | return null; |
| 93 | } |
| 94 | } |