blob: d8af39584d412e8094ab1a7b1dd630c44d08ea6e [file] [log] [blame]
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -07001/*
2 * Copyright 2019-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.impl;
18
Carmelo Casconedefc74e2020-07-17 15:27:02 -070019import org.onosproject.net.behaviour.inbandtelemetry.IntProgrammable;
Daniele Moro8fd75e72019-07-25 14:45:01 -070020
21import org.onosproject.net.behaviour.BngProgrammable;
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070022import org.onosproject.net.behaviour.Pipeliner;
23import org.onosproject.net.pi.model.DefaultPiPipeconf;
24import org.onosproject.net.pi.model.PiPipeconf;
25import org.onosproject.net.pi.model.PiPipelineInterpreter;
26import org.onosproject.net.pi.model.PiPipelineModel;
27import org.onosproject.p4runtime.model.P4InfoParser;
28import org.onosproject.p4runtime.model.P4InfoParserException;
29import org.onosproject.pipelines.fabric.FabricPipeconfService;
30import org.onosproject.pipelines.fabric.impl.behaviour.FabricIntProgrammable;
Daniele Moro8fd75e72019-07-25 14:45:01 -070031import org.onosproject.pipelines.fabric.impl.behaviour.bng.FabricBngProgrammable;
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070032import org.onosproject.pipelines.fabric.impl.behaviour.FabricInterpreter;
33import org.onosproject.pipelines.fabric.impl.behaviour.pipeliner.FabricPipeliner;
34import org.osgi.service.component.annotations.Activate;
35import org.osgi.service.component.annotations.Component;
36import org.osgi.service.component.annotations.Deactivate;
37import org.slf4j.Logger;
38
39import java.net.URL;
40
41import static com.google.common.base.Preconditions.checkArgument;
42import static com.google.common.base.Preconditions.checkNotNull;
43import static org.slf4j.LoggerFactory.getLogger;
44
45/**
46 * Implementation of the FabricPipeconfService.
47 */
48@Component(immediate = true, service = FabricPipeconfService.class)
49public final class FabricPipeconfManager implements FabricPipeconfService {
50
51 private static final String INT_PROFILE_SUFFIX = "-int";
52 private static final String FULL_PROFILE_SUFFIX = "-full";
Daniele Moro8fd75e72019-07-25 14:45:01 -070053 private static final String BNG_PROFILE_SUFFIX = "-bng";
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -070054
55 private static Logger log = getLogger(FabricPipeconfLoader.class);
56
57 @Activate
58 public void activate() {
59 log.info("Started");
60 }
61
62 @Deactivate
63 public void deactivate() {
64 log.info("Stopped");
65 }
66
67 @Override
68 public PiPipeconf buildFabricPipeconf(
69 DefaultPiPipeconf.Builder builder, String profile, URL p4InfoUrl, URL cpuPortUrl) {
70 return build(builder, profile, p4InfoUrl, cpuPortUrl);
71 }
72
73 static PiPipeconf build(
74 DefaultPiPipeconf.Builder pipeconfBuilder,
75 String profileName, URL p4InfoUrl, URL cpuPortUrl) {
76 checkNotNull(pipeconfBuilder,
77 "pipeconfBuilder cannot be null");
78 checkArgument(profileName != null && !profileName.isEmpty(),
79 "profileName cannot be null or empty");
80 checkNotNull(p4InfoUrl,
81 "p4InfoUrl cannot be null (check if file exists)");
82 checkNotNull(cpuPortUrl,
83 "cpuPortUrl cannot be null (check if file exists)");
84
85 pipeconfBuilder
86 .withPipelineModel(parseP4Info(p4InfoUrl))
87 .addBehaviour(PiPipelineInterpreter.class, FabricInterpreter.class)
88 .addBehaviour(Pipeliner.class, FabricPipeliner.class)
89 .addExtension(PiPipeconf.ExtensionType.P4_INFO_TEXT, p4InfoUrl)
90 .addExtension(PiPipeconf.ExtensionType.CPU_PORT_TXT, cpuPortUrl);
91
92 // Add IntProgrammable behaviour for INT-enabled profiles.
93 if (profileName.endsWith(INT_PROFILE_SUFFIX) ||
94 profileName.endsWith(FULL_PROFILE_SUFFIX)) {
95 pipeconfBuilder.addBehaviour(IntProgrammable.class, FabricIntProgrammable.class);
96 }
Daniele Moro8fd75e72019-07-25 14:45:01 -070097 // Add BngProgrammable behavior for BNG-enabled pipelines.
98 if (profileName.endsWith(BNG_PROFILE_SUFFIX)) {
99 pipeconfBuilder.addBehaviour(BngProgrammable.class, FabricBngProgrammable.class);
100 }
Carmelo Cascone36d5e7a2019-09-25 01:02:53 -0700101 return pipeconfBuilder.build();
102 }
103
104 private static PiPipelineModel parseP4Info(URL p4InfoUrl) {
105 try {
106 return P4InfoParser.parse(p4InfoUrl);
107 } catch (P4InfoParserException e) {
108 // FIXME: propagate exception that can be handled by whoever is
109 // trying to build pipeconfs.
110 throw new IllegalStateException(e);
111 }
112 }
113}