Yi Tseng | f33c077 | 2017-06-06 14:56:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.bmv2.model; |
| 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.ImmutableMap; |
| 23 | import org.onosproject.net.pi.model.PiActionModel; |
| 24 | import org.onosproject.net.pi.model.PiActionParamModel; |
| 25 | |
| 26 | import java.util.List; |
| 27 | import java.util.Objects; |
| 28 | import java.util.Optional; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * BMv2 action model. |
| 34 | */ |
| 35 | @Beta |
| 36 | public final class Bmv2ActionModel implements PiActionModel { |
| 37 | private final String name; |
| 38 | private final int id; |
| 39 | private final ImmutableMap<String, PiActionParamModel> params; |
| 40 | |
| 41 | /** |
| 42 | * Builds BMv2 action model with given information. |
| 43 | * |
| 44 | * @param name the model name |
| 45 | * @param id the model id |
| 46 | * @param params the action parameters |
| 47 | */ |
| 48 | public Bmv2ActionModel(String name, int id, List<Bmv2ActionParamModel> params) { |
| 49 | checkNotNull(name, "Model name can't be null"); |
| 50 | checkNotNull(params, "Action parameters can't be null"); |
| 51 | |
| 52 | this.name = name; |
| 53 | this.id = id; |
| 54 | ImmutableMap.Builder<String, PiActionParamModel> mapBuilder = ImmutableMap.builder(); |
| 55 | params.forEach(param -> mapBuilder.put(param.name(), param)); |
| 56 | this.params = mapBuilder.build(); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Gets id from this action model. |
| 61 | * |
| 62 | * @return the model id |
| 63 | */ |
| 64 | public int id() { |
| 65 | return id; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String name() { |
| 70 | return name; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public Optional<PiActionParamModel> param(String name) { |
| 75 | checkNotNull(name, "Parameter name can't be null"); |
| 76 | return Optional.ofNullable(params.get(name)); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public List<PiActionParamModel> params() { |
| 81 | return (ImmutableList<PiActionParamModel>) params.values(); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hash(name, params); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (this == obj) { |
| 92 | return true; |
| 93 | } |
| 94 | if (!(obj instanceof Bmv2ActionModel)) { |
| 95 | return false; |
| 96 | } |
| 97 | Bmv2ActionModel that = (Bmv2ActionModel) obj; |
| 98 | return Objects.equals(this.name, that.name) && |
| 99 | Objects.equals(this.params, that.params); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return MoreObjects.toStringHelper(getClass()) |
| 105 | .add("name", name) |
| 106 | .add("params", params) |
| 107 | .toString(); |
| 108 | } |
| 109 | } |