blob: ce9ec131bcb67fb2f3661b7d27704e995109fb1f [file] [log] [blame]
Carmelo Cascone87892e22017-11-13 16:01:29 -08001/*
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
17package org.onosproject.p4runtime.model;
18
19import com.google.common.collect.ImmutableMap;
20import org.onosproject.net.pi.model.PiActionProfileId;
21import org.onosproject.net.pi.model.PiActionProfileModel;
22import org.onosproject.net.pi.model.PiCounterId;
23import org.onosproject.net.pi.model.PiCounterModel;
24import org.onosproject.net.pi.model.PiMeterId;
25import org.onosproject.net.pi.model.PiMeterModel;
26import org.onosproject.net.pi.model.PiPacketOperationModel;
27import org.onosproject.net.pi.model.PiPacketOperationType;
28import org.onosproject.net.pi.model.PiPipelineModel;
FrankWang2674e452018-05-24 17:13:35 +080029import org.onosproject.net.pi.model.PiRegisterId;
30import org.onosproject.net.pi.model.PiRegisterModel;
Carmelo Cascone87892e22017-11-13 16:01:29 -080031import org.onosproject.net.pi.model.PiTableId;
32import org.onosproject.net.pi.model.PiTableModel;
33
34import java.util.Collection;
35import java.util.Objects;
36import java.util.Optional;
37
38/**
39 * Implementation of PiPipelineModel for P4Runtime.
40 */
41final 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;
FrankWang2674e452018-05-24 17:13:35 +080046 private final ImmutableMap<PiRegisterId, PiRegisterModel> registers;
Carmelo Cascone87892e22017-11-13 16:01:29 -080047 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,
FrankWang2674e452018-05-24 17:13:35 +080054 ImmutableMap<PiRegisterId, PiRegisterModel> registers,
Carmelo Cascone87892e22017-11-13 16:01:29 -080055 ImmutableMap<PiActionProfileId, PiActionProfileModel> actionProfiles,
56 ImmutableMap<PiPacketOperationType, PiPacketOperationModel> packetOperations) {
57 this.tables = tables;
58 this.counters = counters;
59 this.meters = meters;
FrankWang2674e452018-05-24 17:13:35 +080060 this.registers = registers;
Carmelo Cascone87892e22017-11-13 16:01:29 -080061 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
FrankWang2674e452018-05-24 17:13:35 +080096 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 Cascone87892e22017-11-13 16:01:29 -0800106 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}