blob: 2eed1bb04332a6cec792c1229c3643cf0309f566 [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.PiActionId;
21import org.onosproject.net.pi.model.PiActionModel;
22import org.onosproject.net.pi.model.PiActionProfileModel;
23import org.onosproject.net.pi.model.PiCounterId;
24import org.onosproject.net.pi.model.PiCounterModel;
25import org.onosproject.net.pi.model.PiMatchFieldId;
26import org.onosproject.net.pi.model.PiMatchFieldModel;
27import org.onosproject.net.pi.model.PiMeterId;
28import org.onosproject.net.pi.model.PiMeterModel;
29import org.onosproject.net.pi.model.PiTableId;
30import org.onosproject.net.pi.model.PiTableModel;
31import org.onosproject.net.pi.model.PiTableType;
32
33import java.util.Collection;
34import java.util.Objects;
35import java.util.Optional;
36
37/**
38 * Implementation of PiTableModel for P4Runtime.
39 */
40final class P4TableModel implements PiTableModel {
41
42 private final PiTableId id;
43 private final PiTableType tableType;
44 private final PiActionProfileModel actionProfile;
45 private final long maxSize;
46 private final ImmutableMap<PiCounterId, PiCounterModel> counters;
47 private final ImmutableMap<PiMeterId, PiMeterModel> meters;
48 private final boolean supportAging;
49 private final ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields;
50 private final ImmutableMap<PiActionId, PiActionModel> actions;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070051 private final PiActionModel constDefaultAction;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070052 private final boolean isConstTable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080053
54 P4TableModel(PiTableId id, PiTableType tableType,
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070055 PiActionProfileModel actionProfile, long maxSize,
56 ImmutableMap<PiCounterId, PiCounterModel> counters,
57 ImmutableMap<PiMeterId, PiMeterModel> meters, boolean supportAging,
58 ImmutableMap<PiMatchFieldId, PiMatchFieldModel> matchFields,
59 ImmutableMap<PiActionId, PiActionModel> actions,
Carmelo Casconee45902b2018-12-18 13:30:45 -080060 PiActionModel constDefaultAction,
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070061 boolean isConstTable) {
Carmelo Cascone87892e22017-11-13 16:01:29 -080062 this.id = id;
63 this.tableType = tableType;
64 this.actionProfile = actionProfile;
65 this.maxSize = maxSize;
66 this.counters = counters;
67 this.meters = meters;
68 this.supportAging = supportAging;
69 this.matchFields = matchFields;
70 this.actions = actions;
Carmelo Cascone50d195f2018-09-11 13:26:38 -070071 this.constDefaultAction = constDefaultAction;
Carmelo Cascone33b27bc2018-09-09 22:56:14 -070072 this.isConstTable = isConstTable;
Carmelo Cascone87892e22017-11-13 16:01:29 -080073 }
74
75 @Override
76 public PiTableId id() {
77 return id;
78 }
79
80 @Override
81 public PiTableType tableType() {
82 return tableType;
83 }
84
85 @Override
86 public PiActionProfileModel actionProfile() {
87 return actionProfile;
88 }
89
90 @Override
91 public long maxSize() {
92 return maxSize;
93 }
94
95 @Override
96 public Collection<PiCounterModel> counters() {
97 return counters.values();
98 }
99
100 @Override
101 public Collection<PiMeterModel> meters() {
102 return meters.values();
103 }
104
105 @Override
106 public boolean supportsAging() {
107 return supportAging;
108 }
109
110 @Override
111 public Collection<PiMatchFieldModel> matchFields() {
112 return matchFields.values();
113 }
114
115 @Override
116 public Collection<PiActionModel> actions() {
117 return actions.values();
118 }
119
120 @Override
Carmelo Cascone50d195f2018-09-11 13:26:38 -0700121 public Optional<PiActionModel> constDefaultAction() {
122 return Optional.ofNullable(constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800123 }
124
125 @Override
Carmelo Cascone33b27bc2018-09-09 22:56:14 -0700126 public boolean isConstantTable() {
127 return isConstTable;
128 }
129
130 @Override
Carmelo Cascone87892e22017-11-13 16:01:29 -0800131 public Optional<PiActionModel> action(PiActionId actionId) {
132 return Optional.ofNullable(actions.get(actionId));
133 }
134
135 @Override
136 public Optional<PiMatchFieldModel> matchField(PiMatchFieldId matchFieldId) {
137 return Optional.ofNullable(matchFields.get(matchFieldId));
138 }
139
140 @Override
141 public int hashCode() {
142 return Objects.hash(id, tableType, actionProfile, maxSize, counters,
143 meters, supportAging, matchFields, actions,
Carmelo Casconee45902b2018-12-18 13:30:45 -0800144 constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800145 }
146
147 @Override
148 public boolean equals(Object obj) {
149 if (this == obj) {
150 return true;
151 }
152 if (obj == null || getClass() != obj.getClass()) {
153 return false;
154 }
155 final P4TableModel other = (P4TableModel) obj;
156 return Objects.equals(this.id, other.id)
157 && Objects.equals(this.tableType, other.tableType)
158 && Objects.equals(this.actionProfile, other.actionProfile)
159 && Objects.equals(this.maxSize, other.maxSize)
160 && Objects.equals(this.counters, other.counters)
161 && Objects.equals(this.meters, other.meters)
162 && Objects.equals(this.supportAging, other.supportAging)
163 && Objects.equals(this.matchFields, other.matchFields)
164 && Objects.equals(this.actions, other.actions)
Carmelo Casconee45902b2018-12-18 13:30:45 -0800165 && Objects.equals(this.constDefaultAction, other.constDefaultAction);
Carmelo Cascone87892e22017-11-13 16:01:29 -0800166 }
167}