blob: bac9d61f7dd85ea2edd0730af60e12e3778d7af0 [file] [log] [blame]
Carmelo Casconeefc0a922016-06-14 14:32:33 -07001/*
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
17package org.onosproject.bmv2.demo.app.wcmp;
18
19import com.google.common.collect.ImmutableMap;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
22import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector;
23import org.onosproject.bmv2.api.runtime.Bmv2LpmMatchParam;
24import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
25import org.onosproject.net.flow.criteria.ExtensionSelector;
26
27import static com.google.common.base.Preconditions.checkArgument;
28import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
29import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.roundToBytes;
30import static org.onosproject.bmv2.demo.app.wcmp.WcmpFabricApp.WCMP_CONTEXT;
31import static org.onosproject.bmv2.demo.app.wcmp.WcmpInterpreter.*;
32
33/**
34 * Builder of WCMP group table extension selector.
35 */
36public 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}