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.ecmp; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.onlab.util.ImmutableByteSequence; |
| 21 | import org.onosproject.bmv2.api.context.Bmv2HeaderTypeModel; |
| 22 | import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam; |
| 23 | import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector; |
| 24 | import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils; |
| 25 | import org.onosproject.net.flow.criteria.ExtensionSelector; |
| 26 | |
| 27 | import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence; |
| 28 | import static org.onosproject.bmv2.demo.app.ecmp.EcmpFabricApp.ECMP_CONTEXT; |
| 29 | import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.*; |
| 30 | |
| 31 | /** |
| 32 | * Builder of ECMP group table extension selector. |
| 33 | */ |
| 34 | public class EcmpGroupTableSelectorBuilder { |
| 35 | |
| 36 | private int groupId; |
| 37 | private int selector; |
| 38 | |
| 39 | /** |
| 40 | * Sets the ECMP group ID. |
| 41 | * |
| 42 | * @param groupId an integer value |
| 43 | * @return this |
| 44 | */ |
| 45 | public EcmpGroupTableSelectorBuilder withGroupId(int groupId) { |
| 46 | this.groupId = groupId; |
| 47 | return this; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Sets the ECMP selector. |
| 52 | * |
| 53 | * @param selector an integer value |
| 54 | * @return this |
| 55 | */ |
| 56 | public EcmpGroupTableSelectorBuilder withSelector(int selector) { |
| 57 | this.selector = selector; |
| 58 | return this; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Returns a new extension selector. |
| 63 | * |
| 64 | * @return an extension selector |
| 65 | */ |
| 66 | public ExtensionSelector build() { |
| 67 | Bmv2HeaderTypeModel headerTypeModel = ECMP_CONTEXT.configuration().headerType(ECMP_METADATA_T); |
| 68 | int groupIdBitWidth = headerTypeModel.field(GROUP_ID).bitWidth(); |
| 69 | int selectorBitWidth = headerTypeModel.field(SELECTOR).bitWidth(); |
| 70 | |
| 71 | try { |
| 72 | ImmutableByteSequence groupIdBs = fitByteSequence(ImmutableByteSequence.copyFrom(groupId), |
| 73 | groupIdBitWidth); |
| 74 | ImmutableByteSequence selectorBs = fitByteSequence(ImmutableByteSequence.copyFrom(selector), |
| 75 | selectorBitWidth); |
| 76 | |
| 77 | Bmv2ExactMatchParam groupIdMatch = new Bmv2ExactMatchParam(groupIdBs); |
| 78 | Bmv2ExactMatchParam hashMatch = new Bmv2ExactMatchParam(selectorBs); |
| 79 | |
| 80 | return new Bmv2ExtensionSelector(ImmutableMap.of( |
| 81 | ECMP_METADATA + "." + GROUP_ID, groupIdMatch, |
| 82 | ECMP_METADATA + "." + SELECTOR, hashMatch)); |
| 83 | |
| 84 | } catch (Bmv2TranslatorUtils.ByteSequenceFitException e) { |
| 85 | throw new RuntimeException(e); |
| 86 | } |
| 87 | } |
| 88 | } |