Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [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.p4tutorial.pipeconf; |
| 18 | |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 19 | import org.onosproject.driver.pipeline.DefaultSingleTablePipeline; |
| 20 | import org.onosproject.net.behaviour.Pipeliner; |
| 21 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 22 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 23 | import org.onosproject.net.pi.model.PiPipeconf; |
| 24 | import org.onosproject.net.pi.model.PiPipeconfId; |
| 25 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 26 | import org.onosproject.net.pi.model.PiPipelineModel; |
Carmelo Cascone | 39c28ca | 2017-11-15 13:03:57 -0800 | [diff] [blame] | 27 | import org.onosproject.net.pi.service.PiPipeconfService; |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 28 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 29 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Carmelo Cascone | a4dc3c1 | 2019-02-12 17:30:00 -0800 | [diff] [blame] | 30 | import org.osgi.service.component.annotations.Activate; |
| 31 | import org.osgi.service.component.annotations.Component; |
| 32 | import org.osgi.service.component.annotations.Deactivate; |
| 33 | import org.osgi.service.component.annotations.Reference; |
| 34 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 37 | |
| 38 | import java.net.URL; |
| 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 a pipeconf when loaded. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public final class PipeconfFactory { |
| 48 | |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 49 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 51 | public static final PiPipeconfId PIPECONF_ID = new PiPipeconfId("p4-tutorial-pipeconf"); |
Carmelo Cascone | a4dc3c1 | 2019-02-12 17:30:00 -0800 | [diff] [blame] | 52 | private static final URL P4INFO_URL = PipeconfFactory.class.getResource("/mytunnel_p4info.txt"); |
Carmelo Cascone | 700648c | 2018-04-11 12:02:16 -0700 | [diff] [blame] | 53 | private static final URL BMV2_JSON_URL = PipeconfFactory.class.getResource("/mytunnel.json"); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 54 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 56 | private PiPipeconfService piPipeconfService; |
| 57 | |
| 58 | @Activate |
| 59 | public void activate() { |
| 60 | // Registers the pipeconf at component activation. |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 61 | try { |
| 62 | piPipeconfService.register(buildPipeconf()); |
| 63 | } catch (P4InfoParserException e) { |
| 64 | log.error("Fail to register {} - Exception: {} - Cause: {}", |
| 65 | PIPECONF_ID, e.getMessage(), e.getCause().getMessage()); |
| 66 | } |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 71 | // Unregisters the pipeconf at component deactivation. |
| 72 | try { |
Carmelo Cascone | 75a9a89 | 2019-04-22 12:12:23 -0700 | [diff] [blame] | 73 | piPipeconfService.unregister(PIPECONF_ID); |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 74 | } catch (IllegalStateException e) { |
| 75 | log.warn("{} haven't been registered", PIPECONF_ID); |
| 76 | } |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Jianwei Mao | fd9f744 | 2018-05-24 21:36:44 +0800 | [diff] [blame] | 79 | private PiPipeconf buildPipeconf() throws P4InfoParserException { |
| 80 | |
| 81 | final PiPipelineModel pipelineModel = P4InfoParser.parse(P4INFO_URL); |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 82 | |
| 83 | return DefaultPiPipeconf.builder() |
| 84 | .withId(PIPECONF_ID) |
| 85 | .withPipelineModel(pipelineModel) |
| 86 | .addBehaviour(PiPipelineInterpreter.class, PipelineInterpreterImpl.class) |
| 87 | .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class) |
Carmelo Cascone | 700648c | 2018-04-11 12:02:16 -0700 | [diff] [blame] | 88 | // Since mytunnel.p4 defines only 1 table, we re-use the existing single-table pipeliner. |
Carmelo Cascone | 87892e2 | 2017-11-13 16:01:29 -0800 | [diff] [blame] | 89 | .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class) |
| 90 | .addExtension(P4_INFO_TEXT, P4INFO_URL) |
| 91 | .addExtension(BMV2_JSON, BMV2_JSON_URL) |
| 92 | .build(); |
Carmelo Cascone | 770507f | 2017-09-14 20:58:04 +0200 | [diff] [blame] | 93 | } |
| 94 | } |