blob: 17b7088109a9ba5b6bdfaab1353efe3490d01f6e [file] [log] [blame]
Daniele Moro6f30ffd2019-12-06 16:10:40 -08001/*
2 * Copyright 2019-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.opencord.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import org.opencord.bng.PppoeBngControlHandler;
23import org.opencord.bng.PppoeEvent;
24import org.opencord.bng.PppoeEventListener;
25import org.opencord.kafka.EventBusService;
26import org.osgi.service.component.annotations.Activate;
27import org.osgi.service.component.annotations.Component;
28import org.osgi.service.component.annotations.Deactivate;
29import org.osgi.service.component.annotations.Reference;
30import org.osgi.service.component.annotations.ReferenceCardinality;
31import org.osgi.service.component.annotations.ReferencePolicy;
32
33import java.time.Instant;
34import java.util.concurrent.atomic.AtomicReference;
35
36/**
37 * Listens for PPPoE handler events from the BNG app and pushes them on a Kafka
38 * bus.
39 */
40@Component(immediate = true)
41public class BngPppoeKafkaIntegration extends AbstractKafkaIntegration {
42
Daniele Moro16600ce2020-01-15 12:00:05 -080043 @Reference(cardinality = ReferenceCardinality.MANDATORY)
44 protected EventBusService eventBusService;
45
46 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
47 policy = ReferencePolicy.DYNAMIC,
48 bind = "bindPppoeBngControl",
49 unbind = "unbindPppoeBngControl")
50 protected volatile PppoeBngControlHandler ignore;
51 private final AtomicReference<PppoeBngControlHandler> pppoeBngControlRef = new AtomicReference<>();
52
Daniele Moro6f30ffd2019-12-06 16:10:40 -080053 private static final String TOPIC_PPPOE = "bng.pppoe";
54 private static final String TIMESTAMP = "timestamp";
55 private static final String EVENT_TYPE = "eventType";
56 private static final String OLT_DEVICE_ID = "deviceId";
57 private static final String OLT_PORT_NUMBER = "portNumber";
58 private static final String MAC_ADDRESS = "macAddress";
59 private static final String IP_ADDRESS = "ipAddress";
60 private static final String SERIAL_NUMBER = "serialNumber";
61 private static final String SESSION_ID = "sessionId";
62
63 private final PppoeEventListener pppoeEventListener = new InternalPppoeListener();
64
Daniele Moro6f30ffd2019-12-06 16:10:40 -080065 protected void bindPppoeBngControl(PppoeBngControlHandler incomingService) {
66 bindAndAddListener(incomingService, pppoeBngControlRef, pppoeEventListener);
67 }
68
69 protected void unbindPppoeBngControl(PppoeBngControlHandler outgoingService) {
70 unbindAndRemoveListener(outgoingService, pppoeBngControlRef, pppoeEventListener);
71 }
72
73 @Activate
74 public void activate() {
75 log.info("Started PppoeKafkaIntegration");
76 }
77
78 @Deactivate
79 public void deactivate() {
80 unbindPppoeBngControl(pppoeBngControlRef.get());
81 log.info("Stopped PppoeKafkaIntegration");
82 }
83
84 private JsonNode serializePppoeEvent(PppoeEvent event) {
85 // Serialize PPPoE event in a JSON node
86 ObjectMapper mapper = new ObjectMapper();
87 ObjectNode pppoeEvent = mapper.createObjectNode();
88 pppoeEvent.put(TIMESTAMP, Instant.now().toString());
89 pppoeEvent.put(EVENT_TYPE, event.type().toString());
90 pppoeEvent.put(OLT_DEVICE_ID, event.subject().getOltConnectPoint().deviceId().toString());
91 pppoeEvent.put(OLT_PORT_NUMBER, event.subject().getOltConnectPoint().port().toString());
92 pppoeEvent.put(MAC_ADDRESS, event.subject().getMacAddress().toString());
93 pppoeEvent.put(IP_ADDRESS, event.subject().getIpAddress().toString());
94 pppoeEvent.put(SERIAL_NUMBER, event.subject().getOnuSerialNumber());
95 pppoeEvent.put(SESSION_ID, event.subject().getSessionId() == 0 ?
96 "" : String.valueOf(event.subject().getSessionId()));
97 return pppoeEvent;
98 }
99
100 private class InternalPppoeListener implements PppoeEventListener {
101
102 @Override
103 public void event(PppoeEvent event) {
104 eventBusService.send(TOPIC_PPPOE, serializePppoeEvent(event));
105 }
106 }
107}