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; |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.ReferencePolicy; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 28 | import org.onosproject.net.AnnotationKeys; |
| 29 | import org.onosproject.net.Port; |
| 30 | import org.opencord.kafka.EventBusService; |
| 31 | import org.opencord.olt.AccessDeviceEvent; |
| 32 | import org.opencord.olt.AccessDeviceListener; |
| 33 | import org.opencord.olt.AccessDeviceService; |
| 34 | import org.slf4j.Logger; |
| 35 | import org.slf4j.LoggerFactory; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 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 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 50 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 51 | bind = "bindAccessDeviceService", |
| 52 | unbind = "unbindAccessDeviceService") |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 53 | protected AccessDeviceService accessDeviceService; |
| 54 | |
| 55 | private final AccessDeviceListener listener = new InternalAccessDeviceListener(); |
| 56 | |
| 57 | private static final String TOPIC = "onu.events"; |
| 58 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 59 | // event fields |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 60 | private static final String STATUS = "status"; |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame] | 61 | private static final String SERIAL_NUMBER = "serialNumber"; |
| 62 | private static final String PORT_NUMBER = "portNumber"; // uni port |
| 63 | private static final String DEVICE_ID = "deviceId"; // OLT OpenFlow Id |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 64 | private static final String TIMESTAMP = "timestamp"; |
| 65 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 66 | // statuses |
| 67 | private static final String ACTIVATED = "activated"; |
| 68 | private static final String DISABLED = "disabled"; |
| 69 | |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 70 | protected void bindAccessDeviceService(AccessDeviceService accessDeviceService) { |
| 71 | if (this.accessDeviceService == null) { |
| 72 | log.info("Binding AccessDeviceService"); |
| 73 | this.accessDeviceService = accessDeviceService; |
| 74 | log.info("Adding listener on AccessDeviceService"); |
| 75 | accessDeviceService.addListener(listener); |
| 76 | } else { |
| 77 | log.warn("Trying to bind AccessDeviceService but it is already bound"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | protected void unbindAccessDeviceService(AccessDeviceService accessDeviceService) { |
| 82 | if (this.accessDeviceService == accessDeviceService) { |
| 83 | log.info("Unbinding AccessDeviceService"); |
| 84 | this.accessDeviceService = null; |
| 85 | log.info("Removing listener on AccessDeviceService"); |
| 86 | accessDeviceService.removeListener(listener); |
| 87 | } else { |
| 88 | log.warn("Trying to unbind AccessDeviceService but it is already unbound"); |
| 89 | } |
| 90 | } |
| 91 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 94 | log.info("Started AccessDeviceKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 99 | log.info("Stopped AccessDeviceKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 102 | private void handle(AccessDeviceEvent event, String status) { |
| 103 | eventBusService.send(TOPIC, serialize(event, status)); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 106 | private JsonNode serialize(AccessDeviceEvent event, String status) { |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 107 | Port port = event.port().get(); |
| 108 | String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 109 | |
| 110 | ObjectMapper mapper = new ObjectMapper(); |
| 111 | ObjectNode onuNode = mapper.createObjectNode(); |
| 112 | onuNode.put(TIMESTAMP, Instant.now().toString()); |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 113 | onuNode.put(STATUS, status); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 114 | onuNode.put(SERIAL_NUMBER, serialNumber); |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame] | 115 | onuNode.put(PORT_NUMBER, port.number().toString()); |
| 116 | onuNode.put(DEVICE_ID, port.element().id().toString()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 117 | |
| 118 | return onuNode; |
| 119 | } |
| 120 | |
| 121 | private class InternalAccessDeviceListener implements |
| 122 | AccessDeviceListener { |
| 123 | |
| 124 | @Override |
| 125 | public void event(AccessDeviceEvent accessDeviceEvent) { |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 126 | log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 127 | switch (accessDeviceEvent.type()) { |
| 128 | case UNI_ADDED: |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 129 | handle(accessDeviceEvent, ACTIVATED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 130 | break; |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 131 | case UNI_REMOVED: |
| 132 | handle(accessDeviceEvent, DISABLED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 133 | default: |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |