Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 1 | /* |
| 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 | package org.onosproject.artemis.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import io.netty.channel.ChannelHandlerContext; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | import org.json.JSONObject; |
| 27 | import org.onlab.packet.IpPrefix; |
| 28 | import org.onosproject.artemis.ArtemisMonitor; |
| 29 | import org.onosproject.artemis.ArtemisPacketProcessor; |
| 30 | import org.onosproject.artemis.impl.objects.ArtemisMessage; |
| 31 | import org.onosproject.event.EventDeliveryService; |
| 32 | import org.onosproject.net.config.NetworkConfigEvent; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigService; |
| 35 | import org.slf4j.Logger; |
| 36 | import org.slf4j.LoggerFactory; |
| 37 | |
| 38 | import java.util.Map; |
| 39 | import java.util.Set; |
| 40 | import java.util.stream.Collectors; |
| 41 | |
| 42 | @Component(immediate = true) |
| 43 | @Service |
| 44 | public class ArtemisMonitorImpl implements ArtemisMonitor { |
| 45 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 46 | private static final Class<ArtemisConfig> CONFIG_CLASS = ArtemisConfig.class; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected EventDeliveryService eventDispatcher; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | private NetworkConfigService configService; |
| 53 | |
| 54 | /* Variables */ |
| 55 | private Set<PrefixHandler> prefixHandlers = Sets.newHashSet(); |
| 56 | private InternalPacketProcessor packetProcessor = new InternalPacketProcessor(); |
| 57 | |
| 58 | private final InternalNetworkConfigListener configListener = |
| 59 | new InternalNetworkConfigListener(); |
| 60 | |
| 61 | @Activate |
| 62 | protected void activate() { |
| 63 | configService.addListener(configListener); |
| 64 | log.info("Artemis Monitor Service Started"); |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | protected void deactivate() { |
| 69 | configService.removeListener(configListener); |
| 70 | prefixHandlers.forEach(PrefixHandler::stopPrefixMonitors); |
| 71 | prefixHandlers.clear(); |
| 72 | |
| 73 | log.info("Artemis Monitor Service Stopped"); |
| 74 | } |
| 75 | |
| 76 | private class InternalPacketProcessor implements ArtemisPacketProcessor { |
| 77 | |
| 78 | @Override |
| 79 | public void processMoasPacket(ArtemisMessage msg, ChannelHandlerContext ctx) { |
| 80 | |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void processMonitorPacket(JSONObject msg) { |
| 85 | // TODO: in future maybe store the BGP Update message and propagate it to the cluster instead of Events |
| 86 | eventDispatcher.post(new ArtemisEvent(ArtemisEvent.Type.BGPUPDATE_ADDED, msg)); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private class InternalNetworkConfigListener implements NetworkConfigListener { |
| 91 | |
| 92 | @Override |
| 93 | public void event(NetworkConfigEvent event) { |
| 94 | switch (event.type()) { |
| 95 | case CONFIG_REGISTERED: |
| 96 | case CONFIG_UNREGISTERED: { |
| 97 | break; |
| 98 | } |
| 99 | case CONFIG_REMOVED: { |
| 100 | if (event.configClass() == CONFIG_CLASS) { |
| 101 | prefixHandlers.forEach(PrefixHandler::stopPrefixMonitors); |
| 102 | prefixHandlers.clear(); |
| 103 | } |
| 104 | break; |
| 105 | } |
| 106 | case CONFIG_UPDATED: |
| 107 | case CONFIG_ADDED: { |
| 108 | if (event.configClass() == CONFIG_CLASS) { |
| 109 | event.config().ifPresent(config -> { |
| 110 | ArtemisConfig artemisConfig = (ArtemisConfig) config; |
| 111 | Set<IpPrefix> ipPrefixes = artemisConfig.prefixesToMonitor(); |
| 112 | Map<String, Set<String>> monitors = artemisConfig.activeMonitors(); |
| 113 | |
| 114 | prefixHandlers.forEach(PrefixHandler::stopPrefixMonitors); |
| 115 | prefixHandlers.clear(); |
| 116 | prefixHandlers = ipPrefixes.stream() |
| 117 | .map(prefix -> new PrefixHandler(prefix, monitors, packetProcessor)) |
| 118 | .collect(Collectors.toSet()); |
| 119 | |
| 120 | prefixHandlers.forEach(PrefixHandler::startPrefixMonitors); |
| 121 | }); |
| 122 | } |
| 123 | break; |
| 124 | } |
| 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | } |