Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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.api.runtime; |
| 18 | |
Carmelo Cascone | 9e39e31 | 2016-06-16 14:47:09 -0700 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 20 | import com.google.common.base.MoreObjects; |
| 21 | import com.google.common.base.Objects; |
| 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.net.flow.AbstractExtension; |
| 24 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 25 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 26 | |
| 27 | import static org.onosproject.net.flow.instructions.ExtensionTreatmentType.ExtensionTreatmentTypes.BMV2_ACTION; |
| 28 | |
| 29 | /** |
| 30 | * Extension treatment for BMv2 used as a wrapper for a {@link Bmv2Action}. |
| 31 | */ |
Carmelo Cascone | 9e39e31 | 2016-06-16 14:47:09 -0700 | [diff] [blame] | 32 | @Beta |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 33 | public final class Bmv2ExtensionTreatment extends AbstractExtension implements ExtensionTreatment { |
| 34 | |
| 35 | private final KryoNamespace appKryo = new KryoNamespace.Builder().build(); |
| 36 | private Bmv2Action action; |
| 37 | |
Carmelo Cascone | 9e39e31 | 2016-06-16 14:47:09 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Creates a new extension treatment for the given BMv2 action. |
| 40 | * |
| 41 | * @param action an action |
| 42 | */ |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 43 | public Bmv2ExtensionTreatment(Bmv2Action action) { |
| 44 | this.action = action; |
| 45 | } |
| 46 | |
Carmelo Cascone | 9e39e31 | 2016-06-16 14:47:09 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Returns the action contained by this extension selector. |
| 49 | * |
| 50 | * @return an action |
| 51 | */ |
| 52 | public Bmv2Action action() { |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 53 | return action; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public ExtensionTreatmentType type() { |
| 58 | return BMV2_ACTION.type(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public byte[] serialize() { |
| 63 | return appKryo.serialize(action); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public void deserialize(byte[] data) { |
| 68 | action = appKryo.deserialize(data); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int hashCode() { |
| 73 | return Objects.hashCode(action); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public boolean equals(Object obj) { |
| 78 | if (this == obj) { |
| 79 | return true; |
| 80 | } |
| 81 | if (obj == null || getClass() != obj.getClass()) { |
| 82 | return false; |
| 83 | } |
| 84 | final Bmv2ExtensionTreatment other = (Bmv2ExtensionTreatment) obj; |
| 85 | return Objects.equal(this.action, other.action); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String toString() { |
| 90 | return MoreObjects.toStringHelper(this) |
| 91 | .add("action", action) |
| 92 | .toString(); |
| 93 | } |
| 94 | } |