Daniele Moro | 6f30ffd | 2019-12-06 16:10:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.opencord.kafka.integrations; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import org.opencord.bng.PppoeBngControlHandler; |
| 23 | import org.opencord.bng.PppoeEvent; |
| 24 | import org.opencord.bng.PppoeEventListener; |
| 25 | import org.opencord.kafka.EventBusService; |
| 26 | import org.osgi.service.component.annotations.Activate; |
| 27 | import org.osgi.service.component.annotations.Component; |
| 28 | import org.osgi.service.component.annotations.Deactivate; |
| 29 | import org.osgi.service.component.annotations.Reference; |
| 30 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 31 | import org.osgi.service.component.annotations.ReferencePolicy; |
| 32 | |
| 33 | import java.time.Instant; |
| 34 | import 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) |
| 41 | public class BngPppoeKafkaIntegration extends AbstractKafkaIntegration { |
| 42 | |
Daniele Moro | 16600ce | 2020-01-15 12:00:05 -0800 | [diff] [blame] | 43 | @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 | |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 53 | protected static final String TOPIC_PPPOE = "bng.pppoe"; |
Daniele Moro | 6f30ffd | 2019-12-06 16:10:40 -0800 | [diff] [blame] | 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 Moro | 6f30ffd | 2019-12-06 16:10:40 -0800 | [diff] [blame] | 65 | 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 | } |