blob: 4a623114c62f2f16f4481181ee7940dc803dd0e3 [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
Carmelo Casconeb531b682018-01-30 17:55:56 -080049 public static final PiPipeconfId FABRIC_PIPECONF_ID =
Yi Tsengbe342052017-11-03 10:21:23 -070050 new PiPipeconfId("org.onosproject.pipelines.fabric");
Carmelo Casconeb531b682018-01-30 17:55:56 -080051
Yi Tsengbe342052017-11-03 10:21:23 -070052 private static final String FABRIC_JSON_PATH = "/p4c-out/bmv2/fabric.json";
53 private static final String FABRIC_P4INFO_PATH = "/p4c-out/bmv2/fabric.p4info";
54
Carmelo Casconeb531b682018-01-30 17:55:56 -080055 private static final PiPipeconf FABRIC_PIPECONF = buildFabricPipeconf();
Yi Tsengbe342052017-11-03 10:21:23 -070056
57 // XXX: Use a collection to hold only one pipeconf because we might separate
58 // fabric pipeconf to leaf/spine pipeconf in the future.
59 private static final Collection<PiPipeconf> ALL_PIPECONFS =
60 ImmutableList.of(FABRIC_PIPECONF);
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 private PiPipeconfService piPipeconfService;
64
65 @Activate
66 public void activate() {
67 // Registers all pipeconf at component activation.
68 ALL_PIPECONFS.forEach(piPipeconfService::register);
69 }
70
71 @Deactivate
72 public void deactivate() {
73 ALL_PIPECONFS.stream().map(PiPipeconf::id).forEach(piPipeconfService::remove);
74 }
75
76 private static PiPipeconf buildFabricPipeconf() {
77 final URL jsonUrl = PipeconfLoader.class.getResource(FABRIC_JSON_PATH);
78 final URL p4InfoUrl = PipeconfLoader.class.getResource(FABRIC_P4INFO_PATH);
79 final PiPipelineModel model = parseP4Info(p4InfoUrl);
Yi Tseng4fd28432018-02-01 14:48:03 -080080 // TODO: add properties to pipeconf instead of adding it to driver
Yi Tsengbe342052017-11-03 10:21:23 -070081 return DefaultPiPipeconf.builder()
82 .withId(FABRIC_PIPECONF_ID)
83 .withPipelineModel(model)
Yi Tsengfa4a1c72017-11-03 10:22:38 -070084 .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class)
Yi Tseng0b809722017-11-03 10:23:26 -070085 .addBehaviour(Pipeliner.class, FabricPipeliner.class)
Yi Tsengbe342052017-11-03 10:21:23 -070086 .addBehaviour(PortStatisticsDiscovery.class, FabricPortStatisticsDiscovery.class)
87 .addExtension(P4_INFO_TEXT, p4InfoUrl)
88 .addExtension(BMV2_JSON, jsonUrl)
Yi Tsengbe342052017-11-03 10:21:23 -070089 .build();
90 }
91
92 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
93 try {
94 return P4InfoParser.parse(p4InfoUrl);
95 } catch (P4InfoParserException e) {
Ray Milkey2b4958a2018-02-06 18:59:06 -080096 throw new IllegalStateException(e);
Yi Tsengbe342052017-11-03 10:21:23 -070097 }
98 }
99}