blob: 3069e271a5d03845ad3d493ee48a2b768043e5fa [file] [log] [blame]
Carmelo Cascone00a59962017-06-16 17:51:49 +09001/*
2 * Copyright 2017-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.net.pi.impl;
18
19import com.google.common.collect.ImmutableMap;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onlab.util.ImmutableByteSequence;
22import org.onosproject.net.flow.criteria.Criterion;
23import org.onosproject.net.flow.criteria.EthCriterion;
24import org.onosproject.net.flow.criteria.EthTypeCriterion;
25import org.onosproject.net.flow.criteria.IPCriterion;
26import org.onosproject.net.flow.criteria.PortCriterion;
27import org.onosproject.net.pi.impl.CriterionTranslators.EthCriterionTranslator;
28import org.onosproject.net.pi.impl.CriterionTranslators.EthTypeCriterionTranslator;
29import org.onosproject.net.pi.impl.CriterionTranslators.IpCriterionTranslator;
30import org.onosproject.net.pi.impl.CriterionTranslators.PortCriterionTranslator;
31import org.onosproject.net.pi.model.PiMatchType;
32import org.onosproject.net.pi.runtime.PiExactFieldMatch;
33import org.onosproject.net.pi.runtime.PiFieldMatch;
34import org.onosproject.net.pi.runtime.PiHeaderFieldId;
35import org.onosproject.net.pi.runtime.PiLpmFieldMatch;
36import org.onosproject.net.pi.runtime.PiTernaryFieldMatch;
37
38import java.util.Map;
39
40import static java.lang.String.format;
41import static org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException;
42import static org.onosproject.net.pi.impl.CriterionTranslator.CriterionTranslatorException;
43import static org.onosproject.net.pi.runtime.PiFlowRuleTranslationService.PiFlowRuleTranslationException;
44
45/**
46 * Helper class to translate criterion instances to PI field matches.
47 */
48final class CriterionTranslatorHelper {
49
zhiyong ke1667fbb2017-07-04 09:51:24 +080050 private static final Map<Class<? extends Criterion>, CriterionTranslator> TRANSLATORS =
Carmelo Cascone00a59962017-06-16 17:51:49 +090051 // Add here new CriterionTranslator implementations.
zhiyong ke1667fbb2017-07-04 09:51:24 +080052 new ImmutableMap.Builder<Class<? extends Criterion>, CriterionTranslator>()
53 .put(PortCriterion.class, new PortCriterionTranslator())
54 .put(EthCriterion.class, new EthCriterionTranslator())
55 .put(EthTypeCriterion.class, new EthTypeCriterionTranslator())
56 .put(IPCriterion.class, new IpCriterionTranslator())
57 .build();
Carmelo Cascone00a59962017-06-16 17:51:49 +090058
59 private CriterionTranslatorHelper() {
60 // Hides constructor.
61 }
62
63 /**
64 * Translates a given criterion instance to a PiFieldMatch with the given id, match type, and bit-width.
65 *
66 * @param fieldId PI header field identifier
67 * @param criterion criterion
68 * @param matchType match type
69 * @param bitWidth size of the field match in bits
70 * @return a PI field match
71 * @throws PiFlowRuleTranslationException if the criterion cannot be translated (see exception message)
72 */
73 static PiFieldMatch translateCriterion(Criterion criterion, PiHeaderFieldId fieldId, PiMatchType matchType,
74 int bitWidth)
75 throws PiFlowRuleTranslationException {
76
77 if (!TRANSLATORS.containsKey(criterion.getClass())) {
78 throw new PiFlowRuleTranslationException(format(
79 "Translation of criterion class %s is not implemented.",
80 criterion.getClass().getSimpleName()));
81 }
82
83 CriterionTranslator translator = TRANSLATORS.get(criterion.getClass());
84
85 try {
86 translator.init(criterion, bitWidth);
87 switch (matchType) {
88 case EXACT:
89 return new PiExactFieldMatch(fieldId, translator.exactMatch());
90 case TERNARY:
91 Pair<ImmutableByteSequence, ImmutableByteSequence> tp = translator.ternaryMatch();
92 return new PiTernaryFieldMatch(fieldId, tp.getLeft(), tp.getRight());
93 case LPM:
94 Pair<ImmutableByteSequence, Integer> lp = translator.lpmMatch();
95 return new PiLpmFieldMatch(fieldId, lp.getLeft(), lp.getRight());
96 default:
97 throw new PiFlowRuleTranslationException(format(
98 "Translation of criterion %s (%s class) to match type %s is not implemented.",
99 criterion.type().name(), criterion.getClass().getSimpleName(), matchType.name()));
100 }
101 } catch (ByteSequenceTrimException e) {
102 throw new PiFlowRuleTranslationException(format(
103 "Size mismatch for criterion %s: %s", criterion.type(), e.getMessage()));
104 } catch (CriterionTranslatorException e) {
105 throw new PiFlowRuleTranslationException(format(
106 "Unable to translate criterion %s: %s", criterion.type(), e.getMessage()));
107 }
108 }
109}