Yi Tseng | be34205 | 2017-11-03 10:21:23 -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.fabric; |
| 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; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.Pipeliner; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 26 | import org.onosproject.net.device.PortStatisticsDiscovery; |
| 27 | import org.onosproject.net.pi.model.DefaultPiPipeconf; |
| 28 | import org.onosproject.net.pi.model.PiPipeconf; |
| 29 | import org.onosproject.net.pi.model.PiPipeconfId; |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 30 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 31 | import org.onosproject.net.pi.model.PiPipelineModel; |
| 32 | import org.onosproject.net.pi.service.PiPipeconfService; |
| 33 | import org.onosproject.p4runtime.model.P4InfoParser; |
| 34 | import org.onosproject.p4runtime.model.P4InfoParserException; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 35 | import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner; |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -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 | * Pipeline config loader for fabric pipeline. |
| 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public class PipeconfLoader { |
| 48 | |
| 49 | private static final PiPipeconfId FABRIC_PIPECONF_ID = |
| 50 | new PiPipeconfId("org.onosproject.pipelines.fabric"); |
| 51 | private static final String FABRIC_JSON_PATH = "/p4c-out/bmv2/fabric.json"; |
| 52 | private static final String FABRIC_P4INFO_PATH = "/p4c-out/bmv2/fabric.p4info"; |
| 53 | |
| 54 | public static final PiPipeconf FABRIC_PIPECONF = buildFabricPipeconf(); |
| 55 | |
| 56 | // XXX: Use a collection to hold only one pipeconf because we might separate |
| 57 | // fabric pipeconf to leaf/spine pipeconf in the future. |
| 58 | private static final Collection<PiPipeconf> ALL_PIPECONFS = |
| 59 | ImmutableList.of(FABRIC_PIPECONF); |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | private PiPipeconfService piPipeconfService; |
| 63 | |
| 64 | @Activate |
| 65 | public void activate() { |
| 66 | // Registers all pipeconf at component activation. |
| 67 | ALL_PIPECONFS.forEach(piPipeconfService::register); |
| 68 | } |
| 69 | |
| 70 | @Deactivate |
| 71 | public void deactivate() { |
| 72 | ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove); |
| 73 | } |
| 74 | |
| 75 | private static PiPipeconf buildFabricPipeconf() { |
| 76 | final URL jsonUrl = PipeconfLoader.class.getResource(FABRIC_JSON_PATH); |
| 77 | final URL p4InfoUrl = PipeconfLoader.class.getResource(FABRIC_P4INFO_PATH); |
| 78 | final PiPipelineModel model = parseP4Info(p4InfoUrl); |
| 79 | return DefaultPiPipeconf.builder() |
| 80 | .withId(FABRIC_PIPECONF_ID) |
| 81 | .withPipelineModel(model) |
Yi Tseng | fa4a1c7 | 2017-11-03 10:22:38 -0700 | [diff] [blame] | 82 | .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 83 | .addBehaviour(Pipeliner.class, FabricPipeliner.class) |
Yi Tseng | be34205 | 2017-11-03 10:21:23 -0700 | [diff] [blame] | 84 | .addBehaviour(PortStatisticsDiscovery.class, FabricPortStatisticsDiscovery.class) |
| 85 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 86 | .addExtension(BMV2_JSON, jsonUrl) |
| 87 | // Put here other target-specific extensions, |
| 88 | // e.g. Tofino's bin and context.json. |
| 89 | .build(); |
| 90 | } |
| 91 | |
| 92 | private static PiPipelineModel parseP4Info(URL p4InfoUrl) { |
| 93 | try { |
| 94 | return P4InfoParser.parse(p4InfoUrl); |
| 95 | } catch (P4InfoParserException e) { |
| 96 | throw new RuntimeException(e); |
| 97 | } |
| 98 | } |
| 99 | } |