Carmelo Cascone | efc0a92 | 2016-06-14 14:32:33 -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.demo.app.wcmp; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam; |
| 22 | import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector; |
| 23 | import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam; |
| 24 | import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils; |
| 25 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
| 26 | |
| 27 | import static com.google.common.base.Preconditions.checkArgument; |
| 28 | import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence; |
| 29 | import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.roundToBytes; |
| 30 | import static org.onosproject.bmv2.demo.app.wcmp.WcmpFabricApp.WCMP_CONTEXT; |
| 31 | import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.*; |
| 32 | |
| 33 | /** |
| 34 | * Builder of WCMP group table extension selector. |
| 35 | */ |
| 36 | public final class WcmpGroupTableSelectorBuilder { |
| 37 | |
| 38 | private int groupId; |
| 39 | private int prefixLength; |
| 40 | |
| 41 | /** |
| 42 | * Sets the WCMP group ID. |
| 43 | * |
| 44 | * @param groupId an integer value |
| 45 | * @return this |
| 46 | */ |
| 47 | public WcmpGroupTableSelectorBuilder withGroupId(int groupId) { |
| 48 | this.groupId = groupId; |
| 49 | return this; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Sets the WCMP selector's prefix length. |
| 54 | * |
| 55 | * @param prefixLength an integer value |
| 56 | * @return this |
| 57 | */ |
| 58 | public WcmpGroupTableSelectorBuilder withPrefixLength(int prefixLength) { |
| 59 | this.prefixLength = prefixLength; |
| 60 | return this; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns a new extension selector. |
| 65 | * |
| 66 | * @return an extension selector |
| 67 | */ |
| 68 | public ExtensionSelector build() { |
| 69 | |
| 70 | final int selectorBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(SELECTOR).bitWidth(); |
| 71 | final int groupIdBitWidth = WCMP_CONTEXT.configuration().headerType(WCMP_META_T).field(GROUP_ID).bitWidth(); |
| 72 | final ImmutableByteSequence ones = ImmutableByteSequence.ofOnes(roundToBytes(selectorBitWidth)); |
| 73 | |
| 74 | checkArgument(prefixLength >= 1 && prefixLength <= selectorBitWidth, |
| 75 | "prefix length must be between 1 and " + selectorBitWidth); |
| 76 | try { |
| 77 | ImmutableByteSequence groupIdBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupId), groupIdBitWidth); |
| 78 | Bmv2ExactMatchParam groupIdMatch = new Bmv2ExactMatchParam(groupIdBs); |
| 79 | Bmv2LpmMatchParam selectorMatch = new Bmv2LpmMatchParam(ones, prefixLength); |
| 80 | |
| 81 | return new Bmv2ExtensionSelector(ImmutableMap.of( |
| 82 | WCMP_META + "." + GROUP_ID, groupIdMatch, |
| 83 | WCMP_META + "." + SELECTOR, selectorMatch)); |
| 84 | |
| 85 | } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) { |
| 86 | throw new RuntimeException(e); |
| 87 | } |
| 88 | } |
| 89 | } |