blob: 33592367634330c2da7bae74d4b91a9ca2b212ce [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;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.ImmutableMap;
23import org.onosproject.net.pi.model.PiActionModel;
24import org.onosproject.net.pi.model.PiActionParamModel;
25
26import java.util.List;
27import java.util.Objects;
28import java.util.Optional;
29
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * BMv2 action model.
34 */
35@Beta
36public 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}