Carmelo Cascone | 5899c13 | 2016-04-06 22:09:08 -0700 | [diff] [blame] | 1 | /* |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Carmelo Cascone | 5899c13 | 2016-04-06 22:09:08 -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 | |
Carmelo Cascone | aa8b629 | 2016-04-13 14:27:06 -0700 | [diff] [blame^] | 17 | package org.onosproject.bmv2.api.model; |
Carmelo Cascone | 5899c13 | 2016-04-06 22:09:08 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Maps; |
| 21 | |
| 22 | import java.util.LinkedHashMap; |
| 23 | import java.util.List; |
| 24 | import java.util.Objects; |
| 25 | |
| 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
| 28 | /** |
| 29 | * BMv2 model action. |
| 30 | */ |
| 31 | public final class Bmv2ModelAction { |
| 32 | |
| 33 | private final String name; |
| 34 | private final int id; |
| 35 | private final LinkedHashMap<String, Bmv2ModelRuntimeData> runtimeDatas = Maps.newLinkedHashMap(); |
| 36 | |
| 37 | /** |
| 38 | * Creates a new action object. |
| 39 | * |
| 40 | * @param name name |
| 41 | * @param id id |
| 42 | * @param runtimeDatas list of runtime data |
| 43 | */ |
| 44 | protected Bmv2ModelAction(String name, int id, List<Bmv2ModelRuntimeData> runtimeDatas) { |
| 45 | this.name = name; |
| 46 | this.id = id; |
| 47 | runtimeDatas.forEach(r -> this.runtimeDatas.put(r.name(), r)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the name of this action. |
| 52 | * |
| 53 | * @return a string value |
| 54 | */ |
| 55 | public String name() { |
| 56 | return name; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the id of this action. |
| 61 | * |
| 62 | * @return an integer value |
| 63 | */ |
| 64 | public int id() { |
| 65 | return id; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns this action's runtime data defined by the passed name, null |
| 70 | * if not present. |
| 71 | * |
| 72 | * @return runtime data or null |
| 73 | */ |
| 74 | public Bmv2ModelRuntimeData runtimeData(String name) { |
| 75 | return runtimeDatas.get(name); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns an immutable list of runtime data for this action. |
| 80 | * The list is ordered according to the values defined in the model. |
| 81 | * |
| 82 | * @return list of runtime data. |
| 83 | */ |
| 84 | public List<Bmv2ModelRuntimeData> runtimeDatas() { |
| 85 | return ImmutableList.copyOf(runtimeDatas.values()); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public int hashCode() { |
| 90 | return Objects.hash(name, id, runtimeDatas); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean equals(Object obj) { |
| 95 | if (this == obj) { |
| 96 | return true; |
| 97 | } |
| 98 | if (obj == null || getClass() != obj.getClass()) { |
| 99 | return false; |
| 100 | } |
| 101 | final Bmv2ModelAction other = (Bmv2ModelAction) obj; |
| 102 | return Objects.equals(this.name, other.name) |
| 103 | && Objects.equals(this.id, other.id) |
| 104 | && Objects.equals(this.runtimeDatas, other.runtimeDatas); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public String toString() { |
| 109 | return toStringHelper(this) |
| 110 | .add("name", name) |
| 111 | .add("id", id) |
| 112 | .add("runtimeDatas", runtimeDatas) |
| 113 | .toString(); |
| 114 | } |
| 115 | |
| 116 | } |