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.criteria.ExtensionSelector; |
| 25 | import org.onosproject.net.flow.criteria.ExtensionSelectorType; |
| 26 | |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import static com.google.common.base.Preconditions.checkNotNull; |
| 31 | |
| 32 | /** |
| 33 | * Extension selector for BMv2 used as a wrapper for multiple BMv2 match parameters. |
| 34 | */ |
Carmelo Cascone | 9e39e31 | 2016-06-16 14:47:09 -0700 | [diff] [blame] | 35 | @Beta |
Carmelo Cascone | 17fc9e4 | 2016-05-31 11:29:21 -0700 | [diff] [blame] | 36 | public final class Bmv2ExtensionSelector extends AbstractExtension implements ExtensionSelector { |
| 37 | |
| 38 | private final KryoNamespace appKryo = new KryoNamespace.Builder() |
| 39 | .register(HashMap.class) |
| 40 | .register(Bmv2MatchParam.class) |
| 41 | .register(Bmv2ExactMatchParam.class) |
| 42 | .register(Bmv2TernaryMatchParam.class) |
| 43 | .register(Bmv2LpmMatchParam.class) |
| 44 | .register(Bmv2ValidMatchParam.class) |
| 45 | .build(); |
| 46 | |
| 47 | private Map<String, Bmv2MatchParam> parameterMap; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new BMv2 extension selector for the given match parameters map, where the keys are expected to be field |
| 51 | * names formatted as headerName.fieldMemberName (e.g. ethernet.dstAddr). |
| 52 | * |
| 53 | * @param paramMap a map |
| 54 | */ |
| 55 | public Bmv2ExtensionSelector(Map<String, Bmv2MatchParam> paramMap) { |
| 56 | this.parameterMap = checkNotNull(paramMap, "param map cannot be null"); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Returns the match parameters map of this selector. |
| 61 | * |
| 62 | * @return a match parameter map |
| 63 | */ |
| 64 | public Map<String, Bmv2MatchParam> parameterMap() { |
| 65 | return parameterMap; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | @Override |
| 70 | public ExtensionSelectorType type() { |
| 71 | return ExtensionSelectorType.ExtensionSelectorTypes.BMV2_MATCH_PARAMS.type(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public byte[] serialize() { |
| 76 | return appKryo.serialize(parameterMap); |
| 77 | } |
| 78 | |
| 79 | @Override |
| 80 | public void deserialize(byte[] data) { |
| 81 | this.parameterMap = appKryo.deserialize(data); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public int hashCode() { |
| 86 | return Objects.hashCode(parameterMap); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean equals(Object obj) { |
| 91 | if (this == obj) { |
| 92 | return true; |
| 93 | } |
| 94 | if (obj == null || getClass() != obj.getClass()) { |
| 95 | return false; |
| 96 | } |
| 97 | final Bmv2ExtensionSelector other = (Bmv2ExtensionSelector) obj; |
| 98 | return Objects.equal(this.parameterMap, other.parameterMap); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public String toString() { |
| 103 | return MoreObjects.toStringHelper(this) |
| 104 | .add("parameterMap", parameterMap) |
| 105 | .toString(); |
| 106 | } |
| 107 | } |