Yi Tseng | f33c077 | 2017-06-06 14:56:18 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Yi Tseng | f33c077 | 2017-06-06 14:56:18 -0700 | [diff] [blame] | 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 | package org.onosproject.bmv2.model; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.Maps; |
| 21 | import org.onosproject.net.pi.model.PiActionModel; |
| 22 | import org.onosproject.net.pi.model.PiHeaderModel; |
| 23 | import org.onosproject.net.pi.model.PiHeaderTypeModel; |
| 24 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 25 | import org.onosproject.net.pi.model.PiTableModel; |
| 26 | |
| 27 | import java.util.Collection; |
| 28 | import java.util.List; |
| 29 | import java.util.Map; |
| 30 | import java.util.Objects; |
| 31 | import java.util.Optional; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * BMv2 pipeline model. |
| 37 | */ |
| 38 | @Beta |
| 39 | public 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 Tseng | 2613077 | 2017-09-14 13:28:18 -0700 | [diff] [blame] | 71 | .forEach(hm -> headerModelMap.put(hm.name(), hm)); |
Yi Tseng | f33c077 | 2017-06-06 14:56:18 -0700 | [diff] [blame] | 72 | 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 | } |