blob: e9ce0cf0d9575293222798097abadcb512631b82 [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.ecmp;
18
19import com.google.common.collect.ImmutableMap;
20import org.onlab.util.ImmutableByteSequence;
21import org.onosproject.bmv2.api.context.Bmv2HeaderTypeModel;
22import org.onosproject.bmv2.api.runtime.Bmv2ExactMatchParam;
23import org.onosproject.bmv2.api.runtime.Bmv2ExtensionSelector;
24import org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils;
25import org.onosproject.net.flow.criteria.ExtensionSelector;
26
27import static org.onosproject.bmv2.api.utils.Bmv2TranslatorUtils.fitByteSequence;
28import static org.onosproject.bmv2.demo.app.ecmp.EcmpFabricApp.ECMP_CONTEXT;
29import static org.onosproject.bmv2.demo.app.ecmp.EcmpInterpreter.*;
30
31/**
32 * Builder of ECMP group table extension selector.
33 */
34public 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}