blob: 089f80ac4320128a582cff1d33b161d37aaf7773 [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 org.onlab.util.ImmutableByteSequence;
20import org.onosproject.net.flow.criteria.Criterion;
21import org.onosproject.net.flow.criteria.EthCriterion;
22import org.onosproject.net.flow.criteria.EthTypeCriterion;
23import org.onosproject.net.flow.criteria.IPCriterion;
24import org.onosproject.net.flow.criteria.PortCriterion;
25
26import static org.onlab.util.ImmutableByteSequence.ByteSequenceTrimException;
27import static org.onlab.util.ImmutableByteSequence.copyFrom;
28
29/**
30 * Factory class of criterion translator implementations.
31 */
32final class CriterionTranslators {
33
34 /**
35 * Translator of PortCriterion.
36 */
37 static final class PortCriterionTranslator extends AbstractCriterionTranslator {
38 @Override
39 public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException {
40 PortCriterion c = (PortCriterion) criterion;
41 initAsExactMatch(copyFrom(c.port().toLong()), bitWidth);
42 }
43 }
44
45 /**
46 * Translator of EthTypeCriterion.
47 */
48 static final class EthTypeCriterionTranslator extends AbstractCriterionTranslator {
49 @Override
50 public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException {
51 EthTypeCriterion c = (EthTypeCriterion) criterion;
52 initAsExactMatch(copyFrom(c.ethType().toShort()), bitWidth);
53 }
54 }
55
56 /**
57 * Translator of EthCriterion.
58 */
59 static final class EthCriterionTranslator extends AbstractCriterionTranslator {
60 @Override
61 public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException {
62 EthCriterion c = (EthCriterion) criterion;
63 ImmutableByteSequence value = copyFrom(c.mac().toBytes());
64 if (c.mask() == null) {
65 initAsExactMatch(value, bitWidth);
66 } else {
67 ImmutableByteSequence mask = copyFrom(c.mask().toBytes());
68 initAsTernaryMatch(value, mask, bitWidth);
69 }
70 }
71 }
72
73 /**
74 * Translator of IpCriterion.
75 */
76 static final class IpCriterionTranslator extends AbstractCriterionTranslator {
77 public void init(Criterion criterion, int bitWidth) throws ByteSequenceTrimException {
78 IPCriterion c = (IPCriterion) criterion;
79 initAsLpm(copyFrom(c.ip().address().toOctets()), c.ip().prefixLength(), bitWidth);
80 }
81 }
82}