Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.net.pi.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import org.apache.commons.lang3.tuple.Pair; |
| 21 | import org.onlab.util.ImmutableByteSequence; |
| 22 | import org.onosproject.net.flow.criteria.Criterion; |
| 23 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 24 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 25 | import org.onosproject.net.flow.criteria.IPCriterion; |
| 26 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 27 | import org.onosproject.net.pi.impl.CriterionTranslators.EthCriterionTranslator; |
| 28 | import org.onosproject.net.pi.impl.CriterionTranslators.EthTypeCriterionTranslator; |
| 29 | import org.onosproject.net.pi.impl.CriterionTranslators.IpCriterionTranslator; |
| 30 | import org.onosproject.net.pi.impl.CriterionTranslators.PortCriterionTranslator; |
| 31 | import org.onosproject.net.pi.model.PiMatchType; |
| 32 | import org.onosproject.net.pi.runtime.PiExactFieldMatch; |
| 33 | import org.onosproject.net.pi.runtime.PiFieldMatch; |
| 34 | import org.onosproject.net.pi.runtime.PiHeaderFieldId; |
| 35 | import org.onosproject.net.pi.runtime.PiLpmFieldMatch; |
| 36 | import org.onosproject.net.pi.runtime.PiTernaryFieldMatch; |
| 37 | |
| 38 | import java.util.Map; |
| 39 | |
| 40 | import static java.lang.String.format; |
| 41 | import static org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException; |
| 42 | import static org.onosproject.net.pi.impl.CriterionTranslator.CriterionTranslatorException; |
| 43 | import static org.onosproject.net.pi.runtime.PiFlowRuleTranslationService.PiFlowRuleTranslationException; |
| 44 | |
| 45 | /** |
| 46 | * Helper class to translate criterion instances to PI field matches. |
| 47 | */ |
| 48 | final class CriterionTranslatorHelper { |
| 49 | |
zhiyong ke | 1667fbb | 2017-07-04 09:51:24 +0800 | [diff] [blame] | 50 | private static final Map<Class<? extends Criterion>, CriterionTranslator> TRANSLATORS = |
Carmelo Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 51 | // Add here new CriterionTranslator implementations. |
zhiyong ke | 1667fbb | 2017-07-04 09:51:24 +0800 | [diff] [blame] | 52 | 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 Cascone | 00a5996 | 2017-06-16 17:51:49 +0900 | [diff] [blame] | 58 | |
| 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 | } |