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 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.bmv2.model.Bmv2PipelineModelParser; |
| 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; |
| 33 | import org.onosproject.net.pi.runtime.PiPipeconfService; |
| 34 | |
| 35 | import java.net.URL; |
| 36 | |
| 37 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON; |
| 38 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 39 | |
| 40 | /** |
| 41 | * Component that produces and registers a pipeconf when loaded. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public final class PipeconfFactory { |
| 45 | |
| 46 | public static final PiPipeconfId PIPECONF_ID = new PiPipeconfId("p4-tutorial-pipeconf"); |
| 47 | private static final URL P4INFO_URL = PipeconfFactory.class.getResource("/main.p4info"); |
| 48 | private static final URL BMV2_JSON_URL = PipeconfFactory.class.getResource("/main.json"); |
| 49 | private static final PiPipelineModel PIPELINE_MODEL = Bmv2PipelineModelParser.parse(BMV2_JSON_URL); |
| 50 | |
| 51 | private static final PiPipeconf PIPECONF = DefaultPiPipeconf.builder() |
| 52 | .withId(PIPECONF_ID) |
| 53 | .withPipelineModel(PIPELINE_MODEL) |
| 54 | .addBehaviour(PiPipelineInterpreter.class, PipelineInterpreterImpl.class) |
| 55 | .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class) |
| 56 | // Since main.p4 defines only 1 table, we re-use the existing single-table pipeliner. |
| 57 | .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class) |
| 58 | .addExtension(P4_INFO_TEXT, P4INFO_URL) |
| 59 | .addExtension(BMV2_JSON, BMV2_JSON_URL) |
| 60 | .build(); |
| 61 | |
| 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | private PiPipeconfService piPipeconfService; |
| 64 | |
| 65 | @Activate |
| 66 | public void activate() { |
| 67 | // Registers the pipeconf at component activation. |
| 68 | piPipeconfService.register(PIPECONF); |
| 69 | } |
| 70 | |
| 71 | @Deactivate |
| 72 | public void deactivate() { |
| 73 | piPipeconfService.remove(PIPECONF.id()); |
| 74 | } |
| 75 | } |