blob: c7c7421178561b9065b56cdfa279112e701c01f3 [file] [log] [blame]
Yi Tsengbe342052017-11-03 10:21:23 -07001/*
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
17package org.onosproject.pipelines.fabric;
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;
Yi Tseng0b809722017-11-03 10:23:26 -070025import org.onosproject.net.behaviour.Pipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070026import 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;
Yi Tsengfa4a1c72017-11-03 10:22:38 -070030import org.onosproject.net.pi.model.PiPipelineInterpreter;
Yi Tsengbe342052017-11-03 10:21:23 -070031import 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;
Yi Tseng0b809722017-11-03 10:23:26 -070035import org.onosproject.pipelines.fabric.pipeliner.FabricPipeliner;
Yi Tsengbe342052017-11-03 10:21:23 -070036
37import java.net.URL;
38import java.util.Collection;
39
40import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
41import 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)
47public 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 Tsengfa4a1c72017-11-03 10:22:38 -070082 .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class)
Yi Tseng0b809722017-11-03 10:23:26 -070083 .addBehaviour(Pipeliner.class, FabricPipeliner.class)
Yi Tsengbe342052017-11-03 10:21:23 -070084 .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}