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 | |
| 17 | package org.onosproject.artemis.impl; |
| 18 | |
| 19 | import org.apache.commons.lang.exception.ExceptionUtils; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 20 | import org.json.JSONArray; |
| 21 | import org.json.JSONException; |
| 22 | import org.json.JSONObject; |
| 23 | import org.onlab.packet.IpPrefix; |
| 24 | import org.onosproject.artemis.ArtemisDetector; |
| 25 | import org.onosproject.artemis.ArtemisEventListener; |
| 26 | import org.onosproject.artemis.ArtemisService; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.event.EventDeliveryService; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | import org.osgi.service.component.annotations.Activate; |
| 30 | import org.osgi.service.component.annotations.Component; |
| 31 | import org.osgi.service.component.annotations.Deactivate; |
| 32 | import org.osgi.service.component.annotations.Reference; |
| 33 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
| 36 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Component(immediate = true, service = ArtemisDetector.class) |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 38 | public class ArtemisDetectorImpl implements ArtemisDetector { |
| 39 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 | |
| 41 | /* Services */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 43 | private CoreService coreService; |
| 44 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 46 | private ArtemisService artemisService; |
| 47 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Dimitrios Mavrommatis | f0c0632 | 2017-10-31 23:49:04 -0700 | [diff] [blame] | 49 | protected EventDeliveryService eventDispatcher; |
| 50 | |
| 51 | private final ArtemisEventListener artemisEventListener = this::handleArtemisEvent; |
| 52 | |
| 53 | @Activate |
| 54 | protected void activate() { |
| 55 | artemisService.addListener(artemisEventListener); |
| 56 | log.info("Artemis Detector Service Started"); |
| 57 | } |
| 58 | |
| 59 | @Deactivate |
| 60 | protected void deactivate() { |
| 61 | artemisService.removeListener(artemisEventListener); |
| 62 | log.info("Artemis Detector Service Stopped"); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Handles a artemis event. |
| 67 | * |
| 68 | * @param event the artemis event |
| 69 | */ |
| 70 | void handleArtemisEvent(ArtemisEvent event) { |
| 71 | // If an instance was deactivated, check whether we need to roll back the upgrade. |
| 72 | if (event.type().equals(ArtemisEvent.Type.BGPUPDATE_ADDED)) { |
| 73 | JSONObject take = (JSONObject) event.subject(); |
| 74 | |
| 75 | log.info("Received information about monitored prefix " + take.toString()); |
| 76 | artemisService.getConfig().ifPresent(config -> |
| 77 | config.monitoredPrefixes().forEach(artemisPrefix -> { |
| 78 | try { |
| 79 | IpPrefix prefix = artemisPrefix.prefix(), receivedPrefix; |
| 80 | |
| 81 | receivedPrefix = IpPrefix.valueOf(take.getString("prefix")); |
| 82 | |
| 83 | if (prefix.contains(receivedPrefix)) { |
| 84 | JSONArray path = take.getJSONArray("path"); |
| 85 | |
| 86 | int state = artemisPrefix.checkPath(path); |
| 87 | if (state >= 100) { |
| 88 | log.info("BGP Hijack detected; pushing prefix for hijack Deaggregation"); |
| 89 | eventDispatcher.post(new ArtemisEvent(ArtemisEvent.Type.HIJACK_ADDED, |
| 90 | receivedPrefix)); |
| 91 | } else { |
| 92 | log.info("BGP Update is legit"); |
| 93 | } |
| 94 | } |
| 95 | } catch (JSONException e) { |
| 96 | log.error(ExceptionUtils.getFullStackTrace(e)); |
| 97 | } |
| 98 | }) |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | } |