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