blob: 02f9cd291a73e7aa0b469c5c9f81340c77868044 [file] [log] [blame]
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -07001/*
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
17package org.onosproject.artemis.impl;
18
19import org.apache.commons.lang.exception.ExceptionUtils;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070020import org.json.JSONArray;
21import org.json.JSONException;
22import org.json.JSONObject;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.artemis.ArtemisDetector;
25import org.onosproject.artemis.ArtemisEventListener;
26import org.onosproject.artemis.ArtemisService;
27import org.onosproject.core.CoreService;
28import org.onosproject.event.EventDeliveryService;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070029import org.osgi.service.component.annotations.Activate;
30import org.osgi.service.component.annotations.Component;
31import org.osgi.service.component.annotations.Deactivate;
32import org.osgi.service.component.annotations.Reference;
33import org.osgi.service.component.annotations.ReferenceCardinality;
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
36
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Component(immediate = true, service = ArtemisDetector.class)
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070038public class ArtemisDetectorImpl implements ArtemisDetector {
39 private final Logger log = LoggerFactory.getLogger(getClass());
40
41 /* Services */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070043 private CoreService coreService;
44
Ray Milkeyd84f89b2018-08-17 14:54:17 -070045 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070046 private ArtemisService artemisService;
47
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Dimitrios Mavrommatisf0c06322017-10-31 23:49:04 -070049 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}