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