Carmelo Cascone | 04098db | 2018-01-30 18:10:32 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-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.hw; |
| 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; |
| 25 | import org.onosproject.net.behaviour.Pipeliner; |
| 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; |
| 30 | import org.onosproject.net.pi.model.PiPipelineInterpreter; |
| 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; |
| 35 | import org.onosproject.pipelines.fabric.FabricInterpreter; |
| 36 | import org.onosproject.pipelines.fabric.FabricPortStatisticsDiscovery; |
| 37 | import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner; |
| 38 | |
| 39 | import java.net.URL; |
| 40 | import java.util.Collection; |
| 41 | |
| 42 | import static java.lang.String.format; |
| 43 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT; |
| 44 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN; |
| 45 | import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_CONTEXT_JSON; |
| 46 | import static org.onosproject.pipelines.fabric.PipeconfLoader.FABRIC_PIPECONF_ID; |
| 47 | |
| 48 | /** |
| 49 | * Pipeline config loader for fabric pipeline. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | public class HwPipeconfLoader { |
| 53 | |
| 54 | private static final String MAVERICKS = "mavericks"; |
| 55 | private static final String MONTANA = "montana"; |
| 56 | |
| 57 | private static final String PLAIN_FABRIC = ""; |
| 58 | // private static final String WITH_SPGW = "-spgw"; |
| 59 | |
| 60 | // TODO: add WITH_SPGW when ready |
| 61 | private static final Collection<String> APPENDICES = ImmutableList |
| 62 | .of(PLAIN_FABRIC); |
| 63 | |
| 64 | private static final Collection<String> PLATFORMS = ImmutableList |
| 65 | .of(MAVERICKS, MONTANA); |
| 66 | |
| 67 | private static final String BASE_PATH = "/p4c-out/tofino/fabric%s/%s"; |
| 68 | private static final String BASE_TOFINO_BIN_PATH = BASE_PATH + "/tofino.bin"; |
| 69 | private static final String BASE_CONTEXT_JSON_PATH = BASE_PATH + "/context.json"; |
| 70 | private static final String BASE_P4INFO_PATH = BASE_PATH + "/fabric.p4info"; |
| 71 | |
| 72 | private static final Collection<PiPipeconf> ALL_PIPECONFS = buildAll(); |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 75 | private PiPipeconfService piPipeconfService; |
| 76 | |
| 77 | @Activate |
| 78 | public void activate() { |
| 79 | // Registers all pipeconf at component activation. |
| 80 | ALL_PIPECONFS.forEach(piPipeconfService::register); |
| 81 | } |
| 82 | |
| 83 | @Deactivate |
| 84 | public void deactivate() { |
| 85 | ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove); |
| 86 | } |
| 87 | |
| 88 | private static PiPipeconf buildTofinoPipeconf(String platform, String appendix) { |
| 89 | final PiPipeconfId pipeconfId = new PiPipeconfId( |
| 90 | FABRIC_PIPECONF_ID.id() + appendix + "." + platform); |
| 91 | final URL tofinoBinUrl = HwPipeconfLoader.class |
| 92 | .getResource(format(BASE_TOFINO_BIN_PATH, appendix, platform)); |
| 93 | final URL contextJsonUrl = HwPipeconfLoader.class |
| 94 | .getResource(format(BASE_CONTEXT_JSON_PATH, appendix, platform)); |
| 95 | final URL p4InfoUrl = HwPipeconfLoader.class |
| 96 | .getResource(format(BASE_P4INFO_PATH, appendix, platform)); |
| 97 | |
| 98 | final PiPipelineModel model = parseP4Info(p4InfoUrl); |
| 99 | return DefaultPiPipeconf.builder() |
| 100 | .withId(pipeconfId) |
| 101 | .withPipelineModel(model) |
| 102 | .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class) |
| 103 | .addBehaviour(Pipeliner.class, FabricPipeliner.class) |
| 104 | .addBehaviour(PortStatisticsDiscovery.class, FabricPortStatisticsDiscovery.class) |
| 105 | .addExtension(P4_INFO_TEXT, p4InfoUrl) |
| 106 | .addExtension(TOFINO_BIN, tofinoBinUrl) |
| 107 | .addExtension(TOFINO_CONTEXT_JSON, contextJsonUrl) |
| 108 | .build(); |
| 109 | } |
| 110 | |
| 111 | private static Collection<PiPipeconf> buildAll() { |
| 112 | ImmutableList.Builder<PiPipeconf> builder = ImmutableList.builder(); |
| 113 | for (String platform : PLATFORMS) { |
| 114 | for (String appendix : APPENDICES) { |
| 115 | builder.add(buildTofinoPipeconf(platform, appendix)); |
| 116 | } |
| 117 | } |
| 118 | return builder.build(); |
| 119 | } |
| 120 | |
| 121 | private static PiPipelineModel parseP4Info(URL p4InfoUrl) { |
| 122 | try { |
| 123 | return P4InfoParser.parse(p4InfoUrl); |
| 124 | } catch (P4InfoParserException e) { |
| 125 | throw new RuntimeException(e); |
| 126 | } |
| 127 | } |
| 128 | } |