Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [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.model; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onosproject.net.pi.model.PiActionProfileId; |
| 21 | import org.onosproject.net.pi.model.PiActionProfileModel; |
| 22 | import org.onosproject.net.pi.model.PiCounterId; |
| 23 | import org.onosproject.net.pi.model.PiCounterModel; |
| 24 | import org.onosproject.net.pi.model.PiMeterId; |
| 25 | import org.onosproject.net.pi.model.PiMeterModel; |
| 26 | import org.onosproject.net.pi.model.PiPacketOperationModel; |
| 27 | import org.onosproject.net.pi.model.PiPacketOperationType; |
| 28 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 29 | import org.onosproject.net.pi.model.PiTableId; |
| 30 | import org.onosproject.net.pi.model.PiTableModel; |
| 31 | |
| 32 | import java.util.Collection; |
| 33 | import java.util.Objects; |
| 34 | import java.util.Optional; |
| 35 | |
| 36 | /** |
| 37 | * Implementation of PiPipelineModel for P4Runtime. |
| 38 | */ |
| 39 | final class P4PipelineModel implements PiPipelineModel { |
| 40 | |
| 41 | private final ImmutableMap<PiTableId, PiTableModel> tables; |
| 42 | private final ImmutableMap<PiCounterId, PiCounterModel> counters; |
| 43 | private final ImmutableMap<PiMeterId, PiMeterModel> meters; |
| 44 | private final ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles; |
| 45 | private final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations; |
| 46 | |
| 47 | P4PipelineModel( |
| 48 | ImmutableMap<PiTableId, PiTableModel> tables, |
| 49 | ImmutableMap<PiCounterId, PiCounterModel> counters, |
| 50 | ImmutableMap<PiMeterId, PiMeterModel> meters, |
| 51 | ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles, |
| 52 | ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations) { |
| 53 | this.tables = tables; |
| 54 | this.counters = counters; |
| 55 | this.meters = meters; |
| 56 | this.actionProfiles = actionProfiles; |
| 57 | this.packetOperations = packetOperations; |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public Optional<PiTableModel> table(PiTableId tableId) { |
| 62 | return Optional.ofNullable(tables.get(tableId)); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Collection<PiTableModel> tables() { |
| 67 | return tables.values(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Optional<PiCounterModel> counter(PiCounterId counterId) { |
| 72 | return Optional.ofNullable(counters.get(counterId)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Collection<PiCounterModel> counters() { |
| 77 | return counters.values(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Optional<PiMeterModel> meter(PiMeterId meterId) { |
| 82 | return Optional.ofNullable(meters.get(meterId)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Collection<PiMeterModel> meters() { |
| 87 | return meters.values(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Optional<PiActionProfileModel> actionProfiles(PiActionProfileId actionProfileId) { |
| 92 | return Optional.ofNullable(actionProfiles.get(actionProfileId)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<PiActionProfileModel> actionProfiles() { |
| 97 | return actionProfiles.values(); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Optional<PiPacketOperationModel> packetOperationModel(PiPacketOperationType type) { |
| 102 | return Optional.ofNullable(packetOperations.get(type)); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public int hashCode() { |
| 108 | return Objects.hash(tables, counters, meters, actionProfiles, packetOperations); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public boolean equals(Object obj) { |
| 113 | if (this == obj) { |
| 114 | return true; |
| 115 | } |
| 116 | if (obj == null || getClass() != obj.getClass()) { |
| 117 | return false; |
| 118 | } |
| 119 | final P4PipelineModel other = (P4PipelineModel) obj; |
| 120 | return Objects.equals(this.tables, other.tables) |
| 121 | && Objects.equals(this.counters, other.counters) |
| 122 | && Objects.equals(this.meters, other.meters) |
| 123 | && Objects.equals(this.actionProfiles, other.actionProfiles) |
| 124 | && Objects.equals(this.packetOperations, other.packetOperations); |
| 125 | } |
| 126 | } |