blob: 2ca4732686c18160430f9ff9d23e5dd2b215723b [file] [log] [blame]
Yi Tsengf33c0772017-06-06 14:56:18 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengf33c0772017-06-06 14:56:18 -07003 *
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.bmv2.model;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.MoreObjects;
Carmelo Cascone00a59962017-06-16 17:51:49 +090021import com.google.common.collect.ImmutableMap;
Yi Tsengf33c0772017-06-06 14:56:18 -070022import com.google.common.collect.ImmutableSet;
23import org.onosproject.net.pi.model.PiActionModel;
24import org.onosproject.net.pi.model.PiTableMatchFieldModel;
25import org.onosproject.net.pi.model.PiTableModel;
26
27import java.util.Collection;
Carmelo Cascone00a59962017-06-16 17:51:49 +090028import java.util.Map;
Yi Tsengf33c0772017-06-06 14:56:18 -070029import java.util.Objects;
Carmelo Cascone00a59962017-06-16 17:51:49 +090030import java.util.Optional;
Yi Tsengf33c0772017-06-06 14:56:18 -070031import java.util.Set;
32
33import static com.google.common.base.Preconditions.*;
34
35/**
36 * BMv2 table model.
37 */
38@Beta
39public final class Bmv2TableModel implements PiTableModel {
40 private final String name;
41 private final int id;
42 private final int maxSize;
43 private final boolean hasCounters;
44 private final boolean supportAging;
45 private final Set<PiTableMatchFieldModel> matchFields;
Carmelo Cascone00a59962017-06-16 17:51:49 +090046 private final Map<String, PiActionModel> actions;
Yi Tsengf33c0772017-06-06 14:56:18 -070047
48 /**
49 * Creates new BMv2 table model.
50 *
51 * @param name the name of table model
52 * @param id the table id
53 * @param maxSize the max size of table model
54 * @param hasCounters true if the table model has counter; null otherwise
55 * @param supportAging true if the table model supports aging; null otherwise
56 * @param matchFields the match fields of table model
57 * @param actions the actions of table model
58 */
59 public Bmv2TableModel(String name, int id,
60 int maxSize, boolean hasCounters,
61 boolean supportAging,
62 Set<Bmv2TableMatchFieldModel> matchFields,
63 Set<Bmv2ActionModel> actions) {
64 checkNotNull(name, "Model name can't be null");
65 checkArgument(maxSize >= 0, "Max size should more than 0");
66 checkNotNull(matchFields, "Match fields can't be null");
67 checkNotNull(actions, "Actions can't be null");
68 this.name = name;
69 this.id = id;
70 this.maxSize = maxSize;
71 this.hasCounters = hasCounters;
72 this.supportAging = supportAging;
73 this.matchFields = ImmutableSet.copyOf(matchFields);
Carmelo Cascone00a59962017-06-16 17:51:49 +090074 ImmutableMap.Builder<String, PiActionModel> mapBuilder = ImmutableMap.builder();
75 actions.forEach(a -> mapBuilder.put(a.name(), a));
76 this.actions = mapBuilder.build();
Yi Tsengf33c0772017-06-06 14:56:18 -070077 }
78
79 /**
80 * Gets table model id.
81 *
82 * @return table model id
83 */
84 public int id() {
85 return id;
86 }
87
88 @Override
89 public String name() {
90 return name;
91 }
92
93 @Override
94 public int maxSize() {
95 return maxSize;
96 }
97
98 @Override
99 public boolean hasCounters() {
100 return hasCounters;
101 }
102
103 @Override
104 public boolean supportsAging() {
105 return supportAging;
106 }
107
108 @Override
109 public Collection<PiTableMatchFieldModel> matchFields() {
110 return matchFields;
111 }
112
113 @Override
114 public Collection<PiActionModel> actions() {
Carmelo Cascone00a59962017-06-16 17:51:49 +0900115 return actions.values();
116 }
117
118 @Override
119 public Optional<PiActionModel> action(String name) {
120 return Optional.ofNullable(actions.get(name));
Yi Tsengf33c0772017-06-06 14:56:18 -0700121 }
122
123 @Override
124 public int hashCode() {
125 return Objects.hash(name, id, maxSize, hasCounters, supportAging,
126 matchFields, actions);
127 }
128
129 @Override
130 public boolean equals(Object obj) {
131 if (obj == this) {
132 return true;
133 }
134 if (!(obj instanceof Bmv2TableModel)) {
135 return false;
136 }
137 Bmv2TableModel that = (Bmv2TableModel) obj;
138 return Objects.equals(name, that.name) &&
139 Objects.equals(id, that.id) &&
140 Objects.equals(maxSize, that.maxSize) &&
141 Objects.equals(hasCounters, that.hasCounters) &&
142 Objects.equals(supportAging, that.supportAging) &&
143 Objects.equals(matchFields, that.matchFields) &&
144 Objects.equals(actions, that.actions);
145 }
146
147 @Override
148 public String toString() {
149 return MoreObjects.toStringHelper(getClass())
150 .add("name", name)
151 .add("id", id)
152 .add("maxSize", maxSize)
153 .add("hasCounters", hasCounters)
154 .add("supportAging", supportAging)
155 .add("matchFields", matchFields)
Carmelo Cascone00a59962017-06-16 17:51:49 +0900156 .add("actions", actions.values())
Yi Tsengf33c0772017-06-06 14:56:18 -0700157 .toString();
158 }
159}