Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-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.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onosproject.net.AnnotationKeys; |
| 28 | import org.onosproject.net.Port; |
| 29 | import org.opencord.kafka.EventBusService; |
| 30 | import org.opencord.olt.AccessDeviceEvent; |
| 31 | import org.opencord.olt.AccessDeviceListener; |
| 32 | import org.opencord.olt.AccessDeviceService; |
| 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
| 35 | |
| 36 | import java.time.Instant; |
| 37 | |
| 38 | /** |
| 39 | * Listens for access device events and pushes them on a Kafka bus. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public class AccessDeviceKafkaIntegration { |
| 43 | |
| 44 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected EventBusService eventBusService; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected AccessDeviceService accessDeviceService; |
| 51 | |
| 52 | private final AccessDeviceListener listener = new InternalAccessDeviceListener(); |
| 53 | |
| 54 | private static final String TOPIC = "onu.events"; |
| 55 | |
| 56 | private static final String STATUS = "status"; |
| 57 | private static final String SERIAL_NUMBER = "serial_number"; |
| 58 | private static final String UNI_PORT_ID = "uni_port_id"; |
| 59 | private static final String OF_DPID = "of_dpid"; |
| 60 | private static final String ACTIVATED = "activated"; |
| 61 | private static final String TIMESTAMP = "timestamp"; |
| 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | accessDeviceService.addListener(listener); |
| 66 | log.info("Started"); |
| 67 | } |
| 68 | |
| 69 | @Deactivate |
| 70 | public void deactivate() { |
| 71 | accessDeviceService.removeListener(listener); |
| 72 | log.info("Stopped"); |
| 73 | } |
| 74 | |
| 75 | private void handle(AccessDeviceEvent event) { |
| 76 | eventBusService.send(TOPIC, serialize(event)); |
| 77 | } |
| 78 | |
| 79 | private JsonNode serialize(AccessDeviceEvent event) { |
| 80 | Port port = event.port().get(); |
| 81 | String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 82 | |
| 83 | ObjectMapper mapper = new ObjectMapper(); |
| 84 | ObjectNode onuNode = mapper.createObjectNode(); |
| 85 | onuNode.put(TIMESTAMP, Instant.now().toString()); |
| 86 | onuNode.put(STATUS, ACTIVATED); |
| 87 | onuNode.put(SERIAL_NUMBER, serialNumber); |
| 88 | onuNode.put(UNI_PORT_ID, port.number().toLong()); |
| 89 | onuNode.put(OF_DPID, port.element().id().toString()); |
| 90 | |
| 91 | return onuNode; |
| 92 | } |
| 93 | |
| 94 | private class InternalAccessDeviceListener implements |
| 95 | AccessDeviceListener { |
| 96 | |
| 97 | @Override |
| 98 | public void event(AccessDeviceEvent accessDeviceEvent) { |
| 99 | switch (accessDeviceEvent.type()) { |
| 100 | case UNI_ADDED: |
| 101 | handle(accessDeviceEvent); |
| 102 | break; |
| 103 | default: |
| 104 | break; |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |