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 | |
| 38 | /** |
| 39 | * Implementation of PiPipelineModel for P4Runtime. |
| 40 | */ |
| 41 | final class P4PipelineModel implements PiPipelineModel { |
| 42 | |
| 43 | private final ImmutableMap<PiTableId, PiTableModel> tables; |
| 44 | private final ImmutableMap<PiCounterId, PiCounterModel> counters; |
| 45 | private final ImmutableMap<PiMeterId, PiMeterModel> meters; |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 46 | private final ImmutableMap<PiRegisterId, PiRegisterModel> registers; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 47 | private final ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles; |
| 48 | private final ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations; |
| 49 | |
| 50 | P4PipelineModel( |
| 51 | ImmutableMap<PiTableId, PiTableModel> tables, |
| 52 | ImmutableMap<PiCounterId, PiCounterModel> counters, |
| 53 | ImmutableMap<PiMeterId, PiMeterModel> meters, |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 54 | ImmutableMap<PiRegisterId, PiRegisterModel> registers, |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 55 | ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles, |
| 56 | ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations) { |
| 57 | this.tables = tables; |
| 58 | this.counters = counters; |
| 59 | this.meters = meters; |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 60 | this.registers = registers; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 61 | this.actionProfiles = actionProfiles; |
| 62 | this.packetOperations = packetOperations; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public Optional<PiTableModel> table(PiTableId tableId) { |
| 67 | return Optional.ofNullable(tables.get(tableId)); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public Collection<PiTableModel> tables() { |
| 72 | return tables.values(); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public Optional<PiCounterModel> counter(PiCounterId counterId) { |
| 77 | return Optional.ofNullable(counters.get(counterId)); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Collection<PiCounterModel> counters() { |
| 82 | return counters.values(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Optional<PiMeterModel> meter(PiMeterId meterId) { |
| 87 | return Optional.ofNullable(meters.get(meterId)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Collection<PiMeterModel> meters() { |
| 92 | return meters.values(); |
| 93 | } |
| 94 | |
| 95 | @Override |
FrankWang | 2674e45 | 2018-05-24 17:13:35 +0800 | [diff] [blame] | 96 | public Optional<PiRegisterModel> register(PiRegisterId registerId) { |
| 97 | return Optional.ofNullable(registers.get(registerId)); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Collection<PiRegisterModel> registers() { |
| 102 | return registers.values(); |
| 103 | } |
| 104 | |
| 105 | @Override |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 106 | public Optional<PiActionProfileModel> actionProfiles(PiActionProfileId actionProfileId) { |
| 107 | return Optional.ofNullable(actionProfiles.get(actionProfileId)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Collection<PiActionProfileModel> actionProfiles() { |
| 112 | return actionProfiles.values(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public Optional<PiPacketOperationModel> packetOperationModel(PiPacketOperationType type) { |
| 117 | return Optional.ofNullable(packetOperations.get(type)); |
| 118 | |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int hashCode() { |
| 123 | return Objects.hash(tables, counters, meters, actionProfiles, packetOperations); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public boolean equals(Object obj) { |
| 128 | if (this == obj) { |
| 129 | return true; |
| 130 | } |
| 131 | if (obj == null || getClass() != obj.getClass()) { |
| 132 | return false; |
| 133 | } |
| 134 | final P4PipelineModel other = (P4PipelineModel) obj; |
| 135 | return Objects.equals(this.tables, other.tables) |
| 136 | && Objects.equals(this.counters, other.counters) |
| 137 | && Objects.equals(this.meters, other.meters) |
| 138 | && Objects.equals(this.actionProfiles, other.actionProfiles) |
| 139 | && Objects.equals(this.packetOperations, other.packetOperations); |
| 140 | } |
| 141 | } |