Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.pipelines.basic; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 25 | import org.onosproject.driver.pipeline.DefaultSingleTablePipeline; |
| 26 | import org.onosproject.net.behaviour.Pipeliner; |
| 27 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 28 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 29 | import org.onosproject.net.pi.model.PiPipeconf; |
| 30 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 31 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 32 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 33 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 34 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 35 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 36 | |
| 37 | import java.net.URL; |
| 38 | import java.util.Collection; |
| 39 | |
| 40 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
| 41 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 42 | |
| 43 | /** |
| 44 | * Component that produces and registers the basic pipeconfs when loaded. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public final class PipeconfLoader { |
| 48 | |
| 49 | private static final PiPipeconfId BASIC_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.basic"); |
| 50 | private static final String BASIC_JSON_PATH = "/p4c-out/bmv2/basic.json"; |
| 51 | private static final String BASIC_P4INFO = "/p4c-out/bmv2/basic.p4info"; |
| 52 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 53 | public static final PiPipeconf BASIC_PIPECONF = buildBasicPipeconf(); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 54 | |
Jonghwan Hyun | b935882 | 2017-12-06 17:55:27 -0800 | [diff] [blame] | 55 | private static final PiPipeconfId INT_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.int"); |
| 56 | private static final String INT_JSON_PATH = "/p4c-out/bmv2/int.json"; |
| 57 | private static final String INT_P4INFO = "/p4c-out/bmv2/int.p4info"; |
| 58 | |
| 59 | public static final PiPipeconf INT_PIPECONF = buildIntPipeconf(); |
| 60 | |
| 61 | private static final Collection<PiPipeconf> ALL_PIPECONFS = ImmutableList.of(BASIC_PIPECONF, INT_PIPECONF); |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 62 | |
| 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | private PiPipeconfService piPipeconfService; |
| 65 | |
| 66 | @Activate |
| 67 | public void activate() { |
| 68 | // Registers all pipeconf at component activation. |
| 69 | ALL_PIPECONFS.forEach(piPipeconfService::register); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove); |
| 75 | } |
| 76 | |
| 77 | private static PiPipeconf buildBasicPipeconf() { |
| 78 | final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH); |
| 79 | final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 80 | |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 81 | return DefaultPiPipeconf.builder() |
| 82 | .withId(BASIC_PIPECONF_ID) |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 83 | .withPipelineModel(parseP4Info(p4InfoUrl)) |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 84 | .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class) |
| 85 | .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class) |
| 86 | .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class) |
| 87 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 88 | .addExtension(BMV2_JSON, jsonUrl) |
| 89 | // Put here other target-specific extensions, |
| 90 | // e.g. Tofino's bin and context.json. |
| 91 | .build(); |
| 92 | } |
| 93 | |
Jonghwan Hyun | b935882 | 2017-12-06 17:55:27 -0800 | [diff] [blame] | 94 | private static PiPipeconf buildIntPipeconf() { |
| 95 | final URL jsonUrl = PipeconfLoader.class.getResource(INT_JSON_PATH); |
| 96 | final URL p4InfoUrl = PipeconfLoader.class.getResource(INT_P4INFO); |
| 97 | |
| 98 | // INT behavior is controlled using pipeline-specific flow rule, |
| 99 | // not using flow objectives, so we just borrow pipeliner to basic pipeconf. |
| 100 | return DefaultPiPipeconf.builder() |
| 101 | .withId(INT_PIPECONF_ID) |
| 102 | .withPipelineModel(parseP4Info(p4InfoUrl)) |
Carmelo Cascone | 255125d | 2018-04-11 14:03:22 -0700 | [diff] [blame^] | 103 | .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class) |
Jonghwan Hyun | b935882 | 2017-12-06 17:55:27 -0800 | [diff] [blame] | 104 | .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class) |
| 105 | .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class) |
| 106 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 107 | .addExtension(BMV2_JSON, jsonUrl) |
| 108 | .build(); |
| 109 | } |
| 110 | |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 111 | private static PiPipelineModel parseP4Info(URL p4InfoUrl) { |
| 112 | try { |
| 113 | return P4InfoParser.parse(p4InfoUrl); |
| 114 | } catch (P4InfoParserException e) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 115 | throw new IllegalStateException(e); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
Carmelo Cascone | ca94bcf | 2017-10-27 14:16:59 -0700 | [diff] [blame] | 118 | } |