Andreas Papazois | a9964ea | 2016-01-08 15:58:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | |
| 17 | package org.onosproject.sdxl3; |
| 18 | |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onosproject.app.ApplicationService; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.incubator.component.ComponentService; |
| 29 | import org.onosproject.incubator.net.intf.InterfaceService; |
| 30 | import org.onosproject.net.config.NetworkConfigService; |
| 31 | import org.onosproject.net.edge.EdgePortService; |
| 32 | import org.onosproject.net.host.HostService; |
| 33 | import org.onosproject.net.packet.InboundPacket; |
| 34 | import org.onosproject.net.packet.PacketContext; |
| 35 | import org.onosproject.net.packet.PacketProcessor; |
| 36 | import org.onosproject.net.packet.PacketService; |
| 37 | import org.onosproject.routing.IntentSynchronizationAdminService; |
| 38 | import org.onosproject.routing.IntentSynchronizationService; |
| 39 | import org.onosproject.routing.RoutingService; |
| 40 | import org.onosproject.routing.config.BgpConfig; |
| 41 | import org.slf4j.Logger; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
| 45 | |
| 46 | import static org.slf4j.LoggerFactory.getLogger; |
| 47 | |
| 48 | /** |
| 49 | * Component for the SDX-L3 application. |
| 50 | */ |
| 51 | @Component(immediate = true) |
| 52 | public class SdxL3 { |
| 53 | |
| 54 | public static final String SDX_L3_APP = "org.onosproject.sdxl3"; |
| 55 | |
| 56 | private final Logger log = getLogger(getClass()); |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected ApplicationService applicationService; |
| 63 | |
| 64 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 65 | protected NetworkConfigService networkConfigService; |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected InterfaceService interfaceService; |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected IntentSynchronizationService intentSynchronizer; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected IntentSynchronizationAdminService intentSynchronizerAdmin; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected ComponentService componentService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected PacketService packetService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected EdgePortService edgeService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected HostService hostService; |
| 87 | |
| 88 | protected ArpHandler arpHandler = null; |
| 89 | |
| 90 | private InternalPacketProcessor processor = null; |
| 91 | |
| 92 | private ApplicationId appId; |
| 93 | |
| 94 | private static List<String> components = new ArrayList<>(); |
| 95 | static { |
| 96 | components.add("org.onosproject.routing.bgp.BgpSessionManager"); |
| 97 | components.add("org.onosproject.routing.impl.Router"); |
| 98 | components.add(org.onosproject.sdxl3.impl.PeerConnectivityManager.class.getName()); |
| 99 | components.add(org.onosproject.sdxl3.SdxL3Fib.class.getName()); |
| 100 | } |
| 101 | |
| 102 | @Activate |
| 103 | protected void activate() { |
| 104 | components.forEach(name -> componentService.activate(appId, name)); |
| 105 | |
| 106 | appId = coreService.registerApplication(SDX_L3_APP); |
| 107 | |
| 108 | ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID); |
| 109 | BgpConfig bgpConfig = |
| 110 | networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS); |
| 111 | |
| 112 | arpHandler = new ArpHandler(bgpConfig, |
| 113 | edgeService, |
| 114 | hostService, |
| 115 | packetService, |
| 116 | interfaceService); |
| 117 | processor = new InternalPacketProcessor(); |
| 118 | packetService.addProcessor(processor, PacketProcessor.director(2)); |
| 119 | |
| 120 | // TODO fix removing intents |
| 121 | applicationService.registerDeactivateHook(appId, |
| 122 | intentSynchronizerAdmin::removeIntents); |
| 123 | |
| 124 | log.info("SDX-L3 started"); |
| 125 | } |
| 126 | |
| 127 | @Deactivate |
| 128 | protected void deactivate() { |
| 129 | components.forEach(name -> componentService.deactivate(appId, name)); |
| 130 | |
| 131 | packetService.removeProcessor(processor); |
| 132 | |
| 133 | log.info("SDX-L3 stopped"); |
| 134 | } |
| 135 | |
| 136 | private class InternalPacketProcessor implements PacketProcessor { |
| 137 | @Override |
| 138 | public void process(PacketContext context) { |
| 139 | |
| 140 | if (context.isHandled()) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | InboundPacket pkt = context.inPacket(); |
| 145 | Ethernet ethernet = pkt.parsed(); |
| 146 | if (ethernet.getEtherType() == Ethernet.TYPE_ARP) { |
| 147 | arpHandler.processPacketIn(pkt); |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } |