blob: 6f8d33a657e796b0ad6118c6181abf54f08b6665 [file] [log] [blame]
Carmelo Cascone87b9b392017-10-02 18:33:20 +02001/*
2 * Copyright 2017-present Open Networking Foundation
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.onosproject.net.Device;
20import org.onosproject.net.flow.IndexTableId;
21import org.onosproject.net.flow.TableId;
22import org.onosproject.net.pi.model.PiPipeconf;
23import org.onosproject.net.pi.model.PiPipelineInterpreter;
Carmelo Cascone87892e22017-11-13 16:01:29 -080024import org.onosproject.net.pi.model.PiTableId;
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080025import org.onosproject.net.pi.service.PiTranslationException;
Carmelo Cascone87b9b392017-10-02 18:33:20 +020026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import static java.lang.String.format;
30
31/**
32 * PI utility class.
33 */
34final class PiUtils {
35
36 private static final Logger log = LoggerFactory.getLogger(PiUtils.class);
37
38 private PiUtils() {
39 // Hides constructor.
40 }
41
42 static PiPipelineInterpreter getInterpreterOrNull(Device device, PiPipeconf pipeconf) {
43 if (device != null) {
44 return device.is(PiPipelineInterpreter.class) ? device.as(PiPipelineInterpreter.class) : null;
45 } else {
46 // The case of device == null should be admitted only during unit testing.
47 // In any other case, the interpreter should be constructed using the device.as() method to make sure that
48 // behaviour's handler/data attributes are correctly populated.
49 // FIXME: modify test class PiFlowRuleTranslatorTest to avoid passing null device
50 // I.e. we need to create a device object that supports is/as method for obtaining the interpreter.
51 log.warn("getInterpreterOrNull() called with device == null, is this a unit test?");
52 try {
53 return (PiPipelineInterpreter) pipeconf.implementation(PiPipelineInterpreter.class)
54 .orElse(null)
55 .newInstance();
56 } catch (InstantiationException | IllegalAccessException e) {
Ray Milkey986a47a2018-01-25 11:38:51 -080057 throw new IllegalArgumentException(format("Unable to instantiate interpreter of pipeconf %s",
58 pipeconf.id()));
Carmelo Cascone87b9b392017-10-02 18:33:20 +020059 }
60 }
61 }
62
63 static PiTableId translateTableId(TableId tableId, PiPipelineInterpreter interpreter)
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080064 throws PiTranslationException {
Carmelo Cascone87b9b392017-10-02 18:33:20 +020065 switch (tableId.type()) {
66 case PIPELINE_INDEPENDENT:
67 return (PiTableId) tableId;
68 case INDEX:
69 IndexTableId indexId = (IndexTableId) tableId;
70 if (interpreter == null) {
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080071 throw new PiTranslationException(format(
Carmelo Cascone87b9b392017-10-02 18:33:20 +020072 "Unable to map table ID '%d' from index to PI: missing interpreter", indexId.id()));
73 } else if (!interpreter.mapFlowRuleTableId(indexId.id()).isPresent()) {
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080074 throw new PiTranslationException(format(
Carmelo Cascone87b9b392017-10-02 18:33:20 +020075 "Unable to map table ID '%d' from index to PI: missing ID in interpreter", indexId.id()));
76 } else {
77 return interpreter.mapFlowRuleTableId(indexId.id()).get();
78 }
79 default:
Carmelo Cascone326ad2d2017-11-28 18:09:13 -080080 throw new PiTranslationException(format(
Carmelo Cascone87b9b392017-10-02 18:33:20 +020081 "Unrecognized table ID type %s", tableId.type().name()));
82 }
83 }
84}