blob: b19ef61e44f3cdde394c3c0b8a4fcd09ee65bedc [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 */
16package org.onosproject.bmv2.model;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.Maps;
21import org.onosproject.net.pi.model.PiActionModel;
22import org.onosproject.net.pi.model.PiHeaderModel;
23import org.onosproject.net.pi.model.PiHeaderTypeModel;
24import org.onosproject.net.pi.model.PiPipelineModel;
25import org.onosproject.net.pi.model.PiTableModel;
26
27import java.util.Collection;
28import java.util.List;
29import java.util.Map;
30import java.util.Objects;
31import java.util.Optional;
32
33import static com.google.common.base.Preconditions.checkNotNull;
34
35/**
36 * BMv2 pipeline model.
37 */
38@Beta
39public class Bmv2PipelineModel implements PiPipelineModel {
40 private final Map<String, PiHeaderTypeModel> headerTypeModels;
41 private final Map<String, PiHeaderModel> headerModels;
42 private final Map<String, PiActionModel> actionModels;
43 private final Map<String, PiTableModel> tableModels;
44
45 /**
46 * Constructs a BMv2 pipeline model by given information.
47 *
48 * @param headerTypeModels the header type models for this pipeline model
49 * @param headerModels the header models for this pipeline model
50 * @param actionModels the action models for this pipeline model
51 * @param tableModels the table models for this pipeline model
52 */
53 Bmv2PipelineModel(List<Bmv2HeaderTypeModel> headerTypeModels,
54 List<Bmv2HeaderModel> headerModels,
55 List<Bmv2ActionModel> actionModels,
56 List<Bmv2TableModel> tableModels) {
57 checkNotNull(headerTypeModels, "Header type models can't be null");
58 checkNotNull(headerModels, "Header models can't be null");
59 checkNotNull(actionModels, "Action models can't be null");
60 checkNotNull(tableModels, "Table models can't be null");
61
62 Map<String, PiHeaderTypeModel> headerTypeModelMap = Maps.newHashMap();
63 headerTypeModels.stream()
64 .filter(Objects::nonNull)
65 .forEach(htm -> headerTypeModelMap.put(htm.name(), htm));
66 this.headerTypeModels = ImmutableMap.copyOf(headerTypeModelMap);
67
68 Map<String, PiHeaderModel> headerModelMap = Maps.newHashMap();
69 headerModels.stream()
70 .filter(Objects::nonNull)
Yi Tseng26130772017-09-14 13:28:18 -070071 .forEach(hm -> headerModelMap.put(hm.name(), hm));
Yi Tsengf33c0772017-06-06 14:56:18 -070072 this.headerModels = ImmutableMap.copyOf(headerModelMap);
73
74 Map<String, PiActionModel> actionModelMap = Maps.newHashMap();
75 actionModels.stream()
76 .filter(Objects::nonNull)
77 .forEach(am -> actionModelMap.put(am.name(), am));
78 this.actionModels = ImmutableMap.copyOf(actionModelMap);
79
80 Map<String, PiTableModel> tableModelMap = Maps.newHashMap();
81 tableModels.stream()
82 .filter(Objects::nonNull)
83 .forEach(tm -> tableModelMap.put(tm.name(), tm));
84 this.tableModels = ImmutableMap.copyOf(tableModelMap);
85 }
86
87 @Override
88 public Optional<PiHeaderTypeModel> headerType(String name) {
89 return Optional.ofNullable(headerTypeModels.get(name));
90 }
91
92 @Override
93 public Collection<PiHeaderTypeModel> headerTypes() {
94 return headerTypeModels.values();
95 }
96
97 @Override
98 public Optional<PiHeaderModel> header(String name) {
99 return Optional.ofNullable(headerModels.get(name));
100 }
101
102 @Override
103 public Collection<PiHeaderModel> headers() {
104 return headerModels.values();
105 }
106
107 @Override
108 public Optional<PiActionModel> action(String name) {
109 return Optional.ofNullable(actionModels.get(name));
110 }
111
112 @Override
113 public Collection<PiActionModel> actions() {
114 return actionModels.values();
115 }
116
117 @Override
118 public Optional<PiTableModel> table(String name) {
119 return Optional.ofNullable(tableModels.get(name));
120 }
121
122 @Override
123 public Collection<PiTableModel> tables() {
124 return tableModels.values();
125 }
126
127 @Override
128 public int hashCode() {
129 return Objects.hash(headerTypeModels, headerModels, actionModels, tableModels);
130 }
131
132 @Override
133 public boolean equals(Object obj) {
134 if (obj == this) {
135 return true;
136 }
137 if (!(obj instanceof Bmv2PipelineModel)) {
138 return false;
139 }
140 Bmv2PipelineModel that = (Bmv2PipelineModel) obj;
141 return Objects.equals(this.headerTypeModels, that.headerTypeModels) &&
142 Objects.equals(this.headerModels, that.headerModels) &&
143 Objects.equals(this.actionModels, that.actionModels) &&
144 Objects.equals(this.tableModels, that.tableModels);
145 }
146}