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