blob: 14e698d6dd42de3ab4c4de62db3bf87aadc3e519 [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.ImmutableBiMap;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020020import com.google.common.collect.ImmutableList;
Carmelo Cascone00a59962017-06-16 17:51:49 +090021import org.onlab.util.ImmutableByteSequence;
22import org.onosproject.net.PortNumber;
23import org.onosproject.net.driver.AbstractHandlerBehaviour;
24import org.onosproject.net.flow.TrafficTreatment;
25import org.onosproject.net.flow.criteria.Criterion;
26import org.onosproject.net.flow.instructions.Instruction;
Andrea Campanella432f7182017-07-14 18:43:27 +020027import org.onosproject.net.packet.OutboundPacket;
Carmelo Cascone00a59962017-06-16 17:51:49 +090028import org.onosproject.net.pi.model.PiPipeconf;
29import org.onosproject.net.pi.model.PiPipelineInterpreter;
30import org.onosproject.net.pi.runtime.PiAction;
31import org.onosproject.net.pi.runtime.PiActionId;
32import org.onosproject.net.pi.runtime.PiActionParam;
33import org.onosproject.net.pi.runtime.PiActionParamId;
34import org.onosproject.net.pi.runtime.PiHeaderFieldId;
Andrea Campanellafc1d34c2017-07-18 17:01:41 +020035import org.onosproject.net.pi.runtime.PiPacketOperation;
Carmelo Cascone00a59962017-06-16 17:51:49 +090036import org.onosproject.net.pi.runtime.PiTableAction;
37import org.onosproject.net.pi.runtime.PiTableId;
38
Andrea Campanella432f7182017-07-14 18:43:27 +020039import java.util.Collection;
Carmelo Cascone00a59962017-06-16 17:51:49 +090040import java.util.Optional;
41
42import static org.onosproject.net.PortNumber.CONTROLLER;
43import static org.onosproject.net.flow.instructions.Instructions.OutputInstruction;
44
45/**
46 * Mock interpreter implementation.
47 */
48public class MockInterpreter extends AbstractHandlerBehaviour implements PiPipelineInterpreter {
49
50 static final String TABLE0 = "table0";
51 static final String SEND_TO_CPU = "send_to_cpu_0";
52 static final String PORT = "port";
53 static final String DROP = "_drop_0";
54 static final String SET_EGRESS_PORT = "set_egress_port_0";
55
56 static final PiHeaderFieldId IN_PORT_ID = PiHeaderFieldId.of("standard_metadata", "ingress_port");
57 static final PiHeaderFieldId ETH_DST_ID = PiHeaderFieldId.of("ethernet_t", "dstAddr");
58 static final PiHeaderFieldId ETH_SRC_ID = PiHeaderFieldId.of("ethernet_t", "srcAddr");
59 static final PiHeaderFieldId ETH_TYPE_ID = PiHeaderFieldId.of("ethernet_t", "etherType");
60
61 private static final ImmutableBiMap<Criterion.Type, PiHeaderFieldId> CRITERION_MAP = ImmutableBiMap.of(
62 Criterion.Type.IN_PORT, IN_PORT_ID,
63 Criterion.Type.ETH_DST, ETH_DST_ID,
64 Criterion.Type.ETH_SRC, ETH_SRC_ID,
65 Criterion.Type.ETH_TYPE, ETH_TYPE_ID);
66
67 private static final ImmutableBiMap<Integer, PiTableId> TABLE_MAP = ImmutableBiMap.of(
68 0, PiTableId.of(TABLE0));
69
70 @Override
71 public PiTableAction mapTreatment(TrafficTreatment treatment, PiPipeconf pipeconf) throws PiInterpreterException {
72
73 if (treatment.allInstructions().size() == 0) {
74 // No instructions means drop for us.
75 return actionWithName(DROP);
76 } else if (treatment.allInstructions().size() > 1) {
77 // Otherwise, we understand treatments with only 1 instruction.
78 throw new PiPipelineInterpreter.PiInterpreterException("Treatment has multiple instructions");
79 }
80
81 Instruction instruction = treatment.allInstructions().get(0);
82
83 switch (instruction.type()) {
84 case OUTPUT:
85 OutputInstruction outInstruction = (OutputInstruction) instruction;
86 PortNumber port = outInstruction.port();
87 if (!port.isLogical()) {
88 PiAction.builder()
89 .withId(PiActionId.of(SET_EGRESS_PORT))
90 .withParameter(new PiActionParam(PiActionParamId.of(PORT),
91 ImmutableByteSequence.copyFrom(port.toLong())))
92 .build();
93 } else if (port.equals(CONTROLLER)) {
94 return actionWithName(SEND_TO_CPU);
95 } else {
96 throw new PiInterpreterException("Egress on logical port not supported: " + port);
97 }
98 case NOACTION:
99 return actionWithName(DROP);
100 default:
101 throw new PiInterpreterException("Instruction type not supported: " + instruction.type().name());
102 }
103 }
104
Andrea Campanella432f7182017-07-14 18:43:27 +0200105 @Override
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200106 public Collection<PiPacketOperation> mapOutboundPacket(OutboundPacket packet, PiPipeconf pipeconf)
Andrea Campanella432f7182017-07-14 18:43:27 +0200107 throws PiInterpreterException {
Andrea Campanellafc1d34c2017-07-18 17:01:41 +0200108 return ImmutableList.of();
Andrea Campanella432f7182017-07-14 18:43:27 +0200109 }
110
Carmelo Cascone00a59962017-06-16 17:51:49 +0900111 /**
112 * Returns an action instance with no runtime parameters.
113 */
114 private PiAction actionWithName(String name) {
115 return PiAction.builder().withId(PiActionId.of(name)).build();
116 }
117
118 @Override
119 public Optional<PiHeaderFieldId> mapCriterionType(Criterion.Type type) {
120 return Optional.ofNullable(CRITERION_MAP.get(type));
121 }
122
123 @Override
124 public Optional<Criterion.Type> mapPiHeaderFieldId(PiHeaderFieldId headerFieldId) {
125 return Optional.ofNullable(CRITERION_MAP.inverse().get(headerFieldId));
126 }
127
128 @Override
129 public Optional<PiTableId> mapFlowRuleTableId(int flowRuleTableId) {
130 return Optional.ofNullable(TABLE_MAP.get(flowRuleTableId));
131 }
132
133}