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; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 35 | import java.time.Instant; |
| 36 | |
| 37 | /** |
| 38 | * Listens for access device events and pushes them on a Kafka bus. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | public class AccessDeviceKafkaIntegration { |
| 42 | |
| 43 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 46 | protected EventBusService eventBusService; |
| 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 49 | protected AccessDeviceService accessDeviceService; |
| 50 | |
| 51 | private final AccessDeviceListener listener = new InternalAccessDeviceListener(); |
| 52 | |
| 53 | private static final String TOPIC = "onu.events"; |
| 54 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 55 | // event fields |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 56 | private static final String STATUS = "status"; |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame^] | 57 | private static final String SERIAL_NUMBER = "serialNumber"; |
| 58 | private static final String PORT_NUMBER = "portNumber"; // uni port |
| 59 | private static final String DEVICE_ID = "deviceId"; // OLT OpenFlow Id |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 60 | private static final String TIMESTAMP = "timestamp"; |
| 61 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 62 | // statuses |
| 63 | private static final String ACTIVATED = "activated"; |
| 64 | private static final String DISABLED = "disabled"; |
| 65 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 66 | @Activate |
| 67 | public void activate() { |
| 68 | accessDeviceService.addListener(listener); |
| 69 | log.info("Started"); |
| 70 | } |
| 71 | |
| 72 | @Deactivate |
| 73 | public void deactivate() { |
| 74 | accessDeviceService.removeListener(listener); |
| 75 | log.info("Stopped"); |
| 76 | } |
| 77 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 78 | private void handle(AccessDeviceEvent event, String status) { |
| 79 | eventBusService.send(TOPIC, serialize(event, status)); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 82 | private JsonNode serialize(AccessDeviceEvent event, String status) { |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 83 | Port port = event.port().get(); |
| 84 | String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 85 | |
| 86 | ObjectMapper mapper = new ObjectMapper(); |
| 87 | ObjectNode onuNode = mapper.createObjectNode(); |
| 88 | onuNode.put(TIMESTAMP, Instant.now().toString()); |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 89 | onuNode.put(STATUS, status); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 90 | onuNode.put(SERIAL_NUMBER, serialNumber); |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame^] | 91 | onuNode.put(PORT_NUMBER, port.number().toString()); |
| 92 | onuNode.put(DEVICE_ID, port.element().id().toString()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 93 | |
| 94 | return onuNode; |
| 95 | } |
| 96 | |
| 97 | private class InternalAccessDeviceListener implements |
| 98 | AccessDeviceListener { |
| 99 | |
| 100 | @Override |
| 101 | public void event(AccessDeviceEvent accessDeviceEvent) { |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 102 | log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 103 | switch (accessDeviceEvent.type()) { |
| 104 | case UNI_ADDED: |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 105 | handle(accessDeviceEvent, ACTIVATED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 106 | break; |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 107 | case UNI_REMOVED: |
| 108 | handle(accessDeviceEvent, DISABLED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 109 | default: |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | } |