Yi Tseng | 82512da | 2017-08-16 19:46:36 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-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.p4runtime.ctl; |
| 18 | |
| 19 | import com.google.common.collect.Lists; |
| 20 | import io.grpc.stub.StreamObserver; |
| 21 | import p4.P4RuntimeGrpc; |
| 22 | import p4.P4RuntimeOuterClass; |
| 23 | import p4.P4RuntimeOuterClass.GetForwardingPipelineConfigRequest; |
| 24 | import p4.P4RuntimeOuterClass.GetForwardingPipelineConfigResponse; |
| 25 | import p4.P4RuntimeOuterClass.ReadRequest; |
| 26 | import p4.P4RuntimeOuterClass.ReadResponse; |
| 27 | import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigRequest; |
| 28 | import p4.P4RuntimeOuterClass.SetForwardingPipelineConfigResponse; |
| 29 | import p4.P4RuntimeOuterClass.StreamMessageResponse; |
| 30 | import p4.P4RuntimeOuterClass.WriteRequest; |
| 31 | import p4.P4RuntimeOuterClass.WriteResponse; |
| 32 | |
| 33 | import java.util.Collection; |
| 34 | import java.util.List; |
| 35 | import java.util.concurrent.CompletableFuture; |
| 36 | import java.util.concurrent.atomic.AtomicLong; |
| 37 | |
| 38 | public class MockP4RuntimeServer extends P4RuntimeGrpc.P4RuntimeImplBase { |
| 39 | private CompletableFuture<Void> completeLock; |
| 40 | private AtomicLong counter; |
| 41 | |
| 42 | // Requests |
| 43 | private List<WriteRequest> writeReqs; |
| 44 | private List<ReadRequest> readReqs; |
| 45 | private List<ReadResponse> readResps; |
| 46 | |
| 47 | /** |
| 48 | * Expect N times request sent by client. |
| 49 | * |
| 50 | * @param times the number of request will sent by client. |
| 51 | * @return a completable future object, wll complete after client send N times requests. |
| 52 | */ |
| 53 | public CompletableFuture<Void> expectRequests(long times) { |
| 54 | counter = new AtomicLong(times); |
| 55 | completeLock = new CompletableFuture<>(); |
| 56 | readReqs = Lists.newArrayList(); |
| 57 | writeReqs = Lists.newArrayList(); |
| 58 | return completeLock; |
| 59 | } |
| 60 | |
| 61 | private void complete() { |
| 62 | if (counter.decrementAndGet() == 0) { |
| 63 | completeLock.complete(null); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | public void willReturnReadResult(Collection<ReadResponse> readResps) { |
| 68 | this.readResps = Lists.newArrayList(readResps); |
| 69 | } |
| 70 | |
| 71 | public List<WriteRequest> getWriteReqs() { |
| 72 | return writeReqs; |
| 73 | } |
| 74 | |
| 75 | public List<ReadRequest> getReadReqs() { |
| 76 | return readReqs; |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void write(WriteRequest request, StreamObserver<WriteResponse> responseObserver) { |
| 81 | writeReqs.add(request); |
| 82 | complete(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void read(ReadRequest request, StreamObserver<ReadResponse> responseObserver) { |
| 87 | readReqs.add(request); |
| 88 | if (readResps != null && !readResps.isEmpty()) { |
| 89 | ReadResponse readResp = readResps.remove(0); // get first response |
| 90 | responseObserver.onNext(readResp); |
| 91 | responseObserver.onCompleted(); |
| 92 | } |
| 93 | complete(); |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void setForwardingPipelineConfig(SetForwardingPipelineConfigRequest request, |
| 98 | StreamObserver<SetForwardingPipelineConfigResponse> responseObserver) { |
| 99 | throw new UnsupportedOperationException("Not implement yet"); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void getForwardingPipelineConfig(GetForwardingPipelineConfigRequest request, |
| 104 | StreamObserver<GetForwardingPipelineConfigResponse> responseObserver) { |
| 105 | throw new UnsupportedOperationException("Not implement yet"); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public StreamObserver<P4RuntimeOuterClass.StreamMessageRequest> |
| 110 | streamChannel(StreamObserver<StreamMessageResponse> responseObserver) { |
| 111 | // TODO: not implement yet |
| 112 | return null; |
| 113 | } |
| 114 | } |