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; |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 29 | import org.onosproject.net.pi.model.PiRegisterId; |
| 30 | import org.onosproject.net.pi.model.PiRegisterModel; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiTableId; |
| 32 | import org.onosproject.net.pi.model.PiTableModel; |
| 33 | |
| 34 | import java.util.Collection; |
| 35 | import java.util.Objects; |
| 36 | import java.util.Optional; |
| 37 | |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 38 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 39 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Implementation of PiPipelineModel for P4Runtime. |
| 42 | */ |
| 43 | final class P4PipelineModel implements PiPipelineModel { |
| 44 | |
| 45 | private final ImmutableMap<PiTableId, PiTableModel> tables; |
| 46 | private final ImmutableMap<PiCounterId, PiCounterModel> counters; |
| 47 | private final ImmutableMap<PiMeterId, PiMeterModel> meters; |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 48 | private final ImmutableMap<PiRegisterId, PiRegisterModel> registers; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 49 | private final ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles; |
| 50 | private final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations; |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 51 | private final int fingerprint; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 52 | |
| 53 | P4PipelineModel( |
| 54 | ImmutableMap<PiTableId, PiTableModel> tables, |
| 55 | ImmutableMap<PiCounterId, PiCounterModel> counters, |
| 56 | ImmutableMap<PiMeterId, PiMeterModel> meters, |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 57 | ImmutableMap<PiRegisterId, PiRegisterModel> registers, |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 58 | ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles, |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 59 | ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations, |
| 60 | int fingerprint) { |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 61 | this.tables = tables; |
| 62 | this.counters = counters; |
| 63 | this.meters = meters; |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 64 | this.registers = registers; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 65 | this.actionProfiles = actionProfiles; |
| 66 | this.packetOperations = packetOperations; |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 67 | this.fingerprint = fingerprint; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Optional<PiTableModel> table(PiTableId tableId) { |
| 72 | return Optional.ofNullable(tables.get(tableId)); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Collection<PiTableModel> tables() { |
| 77 | return tables.values(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Optional<PiCounterModel> counter(PiCounterId counterId) { |
| 82 | return Optional.ofNullable(counters.get(counterId)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Collection<PiCounterModel> counters() { |
| 87 | return counters.values(); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Optional<PiMeterModel> meter(PiMeterId meterId) { |
| 92 | return Optional.ofNullable(meters.get(meterId)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Collection<PiMeterModel> meters() { |
| 97 | return meters.values(); |
| 98 | } |
| 99 | |
| 100 | @Override |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 101 | public Optional<PiRegisterModel> register(PiRegisterId registerId) { |
| 102 | return Optional.ofNullable(registers.get(registerId)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Collection<PiRegisterModel> registers() { |
| 107 | return registers.values(); |
| 108 | } |
| 109 | |
| 110 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 111 | public Optional<PiActionProfileModel> actionProfiles(PiActionProfileId actionProfileId) { |
| 112 | return Optional.ofNullable(actionProfiles.get(actionProfileId)); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Collection<PiActionProfileModel> actionProfiles() { |
| 117 | return actionProfiles.values(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Optional<PiPacketOperationModel> packetOperationModel(PiPacketOperationType type) { |
| 122 | return Optional.ofNullable(packetOperations.get(type)); |
| 123 | |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public int hashCode() { |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 128 | // NOTE: that the fingerprint is derived by hashing the p4Info file |
| 129 | // this because the hashcode is not deterministic across multiple |
| 130 | // JVMs instance. This hashcode is also used to derive the fingerprint |
| 131 | // of the pipeconf. |
| 132 | return fingerprint; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public boolean equals(Object obj) { |
| 137 | if (this == obj) { |
| 138 | return true; |
| 139 | } |
| 140 | if (obj == null || getClass() != obj.getClass()) { |
| 141 | return false; |
| 142 | } |
| 143 | final P4PipelineModel other = (P4PipelineModel) obj; |
| 144 | return Objects.equals(this.tables, other.tables) |
| 145 | && Objects.equals(this.counters, other.counters) |
| 146 | && Objects.equals(this.meters, other.meters) |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 147 | && Objects.equals(this.registers, other.registers) |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 148 | && Objects.equals(this.actionProfiles, other.actionProfiles) |
pierventre | 4d2a542 | 2021-01-10 17:29:03 -0800 | [diff] [blame^] | 149 | && Objects.equals(this.packetOperations, other.packetOperations) |
| 150 | && this.fingerprint == other.fingerprint; |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public String toString() { |
| 155 | return toStringHelper(this) |
| 156 | .add("tables", tables.values()) |
| 157 | .add("counters", counters.values()) |
| 158 | .add("meters", meters.values()) |
| 159 | .add("registers", registers.values()) |
| 160 | .add("actionProfiles", actionProfiles.values()) |
| 161 | .add("packetOperations", packetOperations.values()) |
| 162 | .add("fingerprint", fingerprint) |
| 163 | .toString(); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 164 | } |
| 165 | } |