blob: bcfb7d06a3c072fe06600bdff20ec7931607c032 [file] [log] [blame]
Carmelo Casconeca94bcf2017-10-27 14:16:59 -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.basic;
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.bmv2.model.Bmv2PipelineModelParser;
26import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
27import org.onosproject.net.behaviour.Pipeliner;
28import org.onosproject.net.device.PortStatisticsDiscovery;
29import org.onosproject.net.pi.model.DefaultPiPipeconf;
30import org.onosproject.net.pi.model.PiPipeconf;
31import org.onosproject.net.pi.model.PiPipeconfId;
32import org.onosproject.net.pi.model.PiPipelineInterpreter;
33import org.onosproject.net.pi.model.PiPipelineModel;
34import org.onosproject.net.pi.runtime.PiPipeconfService;
35
36import java.net.URL;
37import java.util.Collection;
38
39import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.BMV2_JSON;
40import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.P4_INFO_TEXT;
41
42/**
43 * Component that produces and registers the basic pipeconfs when loaded.
44 */
45@Component(immediate = true)
46public final class PipeconfLoader {
47
48 private static final PiPipeconfId BASIC_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.basic");
49 private static final String BASIC_JSON_PATH = "/p4c-out/bmv2/basic.json";
50 private static final String BASIC_P4INFO = "/p4c-out/bmv2/basic.p4info";
51
52 private static final PiPipeconfId ECMP_PIPECONF_ID = new PiPipeconfId("org.onosproject.pipelines.ecmp");
53 private static final String ECMP_JSON_PATH = "/p4c-out/bmv2/ecmp.json";
54 private static final String ECMP_P4INFO = "/p4c-out/bmv2/ecmp.p4info";
55
56 public static final PiPipeconf BASIC_PIPECONF = buildBasicPipeconf();
57 public static final PiPipeconf ECMP_PIPECONF = buildEcmpPipeconf();
58
59 private static final Collection<PiPipeconf> ALL_PIPECONFS = ImmutableList.of(BASIC_PIPECONF, ECMP_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 buildBasicPipeconf() {
76 final URL jsonUrl = PipeconfLoader.class.getResource(BASIC_JSON_PATH);
77 final URL p4InfoUrl = PipeconfLoader.class.getResource(BASIC_P4INFO);
78 final PiPipelineModel model = Bmv2PipelineModelParser.parse(jsonUrl);
79 return DefaultPiPipeconf.builder()
80 .withId(BASIC_PIPECONF_ID)
81 .withPipelineModel(model)
82 .addBehaviour(PiPipelineInterpreter.class, BasicInterpreterImpl.class)
83 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
84 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.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 PiPipeconf buildEcmpPipeconf() {
93 final URL jsonUrl = PipeconfLoader.class.getResource(ECMP_JSON_PATH);
94 final URL p4InfoUrl = PipeconfLoader.class.getResource(ECMP_P4INFO);
95 final PiPipelineModel model = Bmv2PipelineModelParser.parse(jsonUrl);
96 return DefaultPiPipeconf.builder()
97 .withId(ECMP_PIPECONF_ID)
98 .withPipelineModel(model)
99 .addBehaviour(PiPipelineInterpreter.class, EcmpInterpreterImpl.class)
100 .addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
101 .addBehaviour(PortStatisticsDiscovery.class, PortStatisticsDiscoveryImpl.class)
102 .addExtension(P4_INFO_TEXT, p4InfoUrl)
103 .addExtension(BMV2_JSON, jsonUrl)
104 .build();
105 }
106}