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