blob: 05e03e608dbadaf3ed69c6cebbe4f3cc008645e9 [file] [log] [blame]
Carmelo Cascone04098db2018-01-30 18:10:32 -08001/*
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
17package org.onosproject.pipelines.fabric.hw;
18
19import com.google.common.collect.ImmutableList;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.net.behaviour.Pipeliner;
26import org.onosproject.net.device.PortStatisticsDiscovery;
27import org.onosproject.net.pi.model.DefaultPiPipeconf;
28import org.onosproject.net.pi.model.PiPipeconf;
29import org.onosproject.net.pi.model.PiPipeconfId;
30import org.onosproject.net.pi.model.PiPipelineInterpreter;
31import org.onosproject.net.pi.model.PiPipelineModel;
32import org.onosproject.net.pi.service.PiPipeconfService;
33import org.onosproject.p4runtime.model.P4InfoParser;
34import org.onosproject.p4runtime.model.P4InfoParserException;
35import org.onosproject.pipelines.fabric.FabricInterpreter;
36import org.onosproject.pipelines.fabric.FabricPortStatisticsDiscovery;
37import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
38
39import java.net.URL;
40import java.util.Collection;
41
42import static java.lang.String.format;
43import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
44import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_BIN;
45import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.TOFINO_CONTEXT_JSON;
46import static org.onosproject.pipelines.fabric.PipeconfLoader.FABRIC_PIPECONF_ID;
47
48/**
49 * Pipeline config loader for fabric pipeline.
50 */
51@Component(immediate = true)
52public 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}