blob: e09435f73bbb0562e83176e5c355bfa6c73bde1f [file] [log] [blame]
/*
* Copyright 2017-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.pi.demo.app.tor;
import com.google.common.collect.Lists;
import org.onosproject.bmv2.model.Bmv2PipelineModelParser;
import org.onosproject.driver.pipeline.DefaultSingleTablePipeline;
import org.onosproject.drivers.p4runtime.DefaultP4PortStatisticsDiscovery;
import org.onosproject.net.behaviour.Pipeliner;
import org.onosproject.net.device.PortStatisticsDiscovery;
import org.onosproject.net.pi.model.DefaultPiPipeconf;
import org.onosproject.net.pi.model.PiPipeconf;
import org.onosproject.net.pi.model.PiPipeconfId;
import org.onosproject.net.pi.model.PiPipelineInterpreter;
import org.onosproject.net.pi.model.PiPipelineModel;
import java.net.URL;
import java.util.Collection;
import static java.lang.String.format;
import static org.onosproject.net.pi.model.PiPipeconf.ExtensionType.*;
/**
* Pipeconf factory for all tor.p4 based pipeconf, both BMv2 and Tofino
*/
final class TorPipeconfFactory {
private static final String BMV2_PIPECONF_ID = "bmv2-tor";
private static final URL BMV2_P4INFO_URL = TorApp.class.getResource("/p4c-out/tor.p4info");
private static final URL BMV2_JSON_URL = TorApp.class.getResource("/p4c-out/tor.json");
private static final String TOFINO_PIPECONF_ID_BASE = "tofino-tor-%s";
private static final String TOFINO_P4INFO_PATH_BASE = "/p4c-out/tofino/tor/%s/tor.p4info";
private static final String TOFINO_CONTEXT_JSON_PATH_BASE = "/p4c-out/tofino/tor/%s/context.json";
private static final String TOFINO_BIN_PATH_BASE = "/p4c-out/tofino/tor/%s/tofino.bin";
private static final String MAVERICKS = "mavericks";
private static final String MONTARA = "montara";
private static final PiPipelineModel PIPELINE_MODEL = Bmv2PipelineModelParser.parse(BMV2_JSON_URL);
private static final PiPipeconf BMV2_PIPECONF = buildBmv2Pipeconf();
private static final PiPipeconf MAVERICKS_PIPECONF = buildTofinoPipeconf(MAVERICKS);
private static final PiPipeconf MONTARA_PIPECONF = buildTofinoPipeconf(MONTARA);
private TorPipeconfFactory() {
// Hides constructor.
}
static Collection<PiPipeconf> getAll() {
return Lists.newArrayList(BMV2_PIPECONF, MAVERICKS_PIPECONF, MONTARA_PIPECONF);
}
private static DefaultPiPipeconf.Builder baseBuilder() {
return DefaultPiPipeconf.builder()
.withPipelineModel(PIPELINE_MODEL)
.addBehaviour(PiPipelineInterpreter.class, TorInterpreter.class)
// FIXME: add if Packet I/O is not working
// .addBehaviour(LinkDiscovery.class, NetcfgLinkDiscovery.class)
// FIXME: change underneath to proper TOR implementation
.addBehaviour(Pipeliner.class, DefaultSingleTablePipeline.class)
.addBehaviour(PortStatisticsDiscovery.class, TorPortStatisticsDiscovery.class);
}
private static PiPipeconf buildTofinoPipeconf(String system) {
final URL p4InfoUrl = TorPipeconfFactory.class.getResource(format(TOFINO_P4INFO_PATH_BASE, system));
final URL tofinoUrl = TorPipeconfFactory.class.getResource(format(TOFINO_BIN_PATH_BASE, system));
final URL contextUrl = TorPipeconfFactory.class.getResource(format(TOFINO_CONTEXT_JSON_PATH_BASE, system));
return baseBuilder()
.withId(new PiPipeconfId(format(TOFINO_PIPECONF_ID_BASE, system)))
.addExtension(P4_INFO_TEXT, p4InfoUrl)
.addExtension(TOFINO_BIN, tofinoUrl)
.addExtension(TOFINO_CONTEXT_JSON, contextUrl)
.build();
}
private static PiPipeconf buildBmv2Pipeconf() {
return baseBuilder()
.withId(new PiPipeconfId(BMV2_PIPECONF_ID))
.addExtension(P4_INFO_TEXT, BMV2_P4INFO_URL)
.addExtension(BMV2_JSON, BMV2_JSON_URL)
.build();
}
public static class TorPortStatisticsDiscovery extends DefaultP4PortStatisticsDiscovery {
@Override
public String scope() {
return "packetio_egress";
}
}
}