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; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 22 | import org.onosproject.net.AnnotationKeys; |
Amit Ghosh | ec08699 | 2020-02-24 13:52:17 +0000 | [diff] [blame] | 23 | import org.onosproject.net.Device; |
| 24 | import org.onosproject.net.DeviceId; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Port; |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 26 | import org.onosproject.net.device.DeviceService; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 27 | import org.opencord.kafka.EventBusService; |
| 28 | import org.opencord.olt.AccessDeviceEvent; |
| 29 | import org.opencord.olt.AccessDeviceListener; |
| 30 | import org.opencord.olt.AccessDeviceService; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 31 | import org.osgi.service.component.annotations.Activate; |
| 32 | import org.osgi.service.component.annotations.Component; |
| 33 | import org.osgi.service.component.annotations.Deactivate; |
| 34 | import org.osgi.service.component.annotations.Reference; |
| 35 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 36 | import org.osgi.service.component.annotations.ReferencePolicy; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 39 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 40 | import java.time.Instant; |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 41 | import java.util.concurrent.atomic.AtomicReference; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Listens for access device events and pushes them on a Kafka bus. |
| 45 | */ |
| 46 | @Component(immediate = true) |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 47 | public class AccessDeviceKafkaIntegration extends AbstractKafkaIntegration { |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 48 | |
| 49 | public Logger log = LoggerFactory.getLogger(getClass()); |
| 50 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 52 | protected EventBusService eventBusService; |
| 53 | |
Amit Ghosh | ec08699 | 2020-02-24 13:52:17 +0000 | [diff] [blame] | 54 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
| 55 | protected DeviceService deviceService; |
| 56 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 57 | @Reference(cardinality = ReferenceCardinality.OPTIONAL, |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 58 | policy = ReferencePolicy.DYNAMIC, |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 59 | bind = "bindAccessDeviceService", |
| 60 | unbind = "unbindAccessDeviceService") |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 61 | protected volatile AccessDeviceService accessDeviceService; |
| 62 | private final AtomicReference<AccessDeviceService> accessDeviceServiceRef = new AtomicReference<>(); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 63 | |
| 64 | private final AccessDeviceListener listener = new InternalAccessDeviceListener(); |
| 65 | |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 66 | protected static final String TOPIC = "onu.events"; |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 67 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 68 | // event fields |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 69 | private static final String STATUS = "status"; |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame] | 70 | private static final String SERIAL_NUMBER = "serialNumber"; |
| 71 | private static final String PORT_NUMBER = "portNumber"; // uni port |
| 72 | private static final String DEVICE_ID = "deviceId"; // OLT OpenFlow Id |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 73 | private static final String TIMESTAMP = "timestamp"; |
Amit Ghosh | ec08699 | 2020-02-24 13:52:17 +0000 | [diff] [blame] | 74 | private static final String OLT_SERIAL_NUMBER = "oltSerialNumber"; // OLT Serial Number |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 75 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 76 | // statuses |
| 77 | private static final String ACTIVATED = "activated"; |
| 78 | private static final String DISABLED = "disabled"; |
| 79 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 80 | protected void bindAccessDeviceService(AccessDeviceService incomingService) { |
| 81 | bindAndAddListener(incomingService, accessDeviceServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 84 | protected void unbindAccessDeviceService(AccessDeviceService outgoingService) { |
| 85 | unbindAndRemoveListener(outgoingService, accessDeviceServiceRef, listener); |
Matteo Scandolo | 03f13c1 | 2019-03-20 14:38:12 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 88 | @Activate |
| 89 | public void activate() { |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 90 | log.info("Started AccessDeviceKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
Carmelo Cascone | 7e73fa1 | 2019-07-15 18:29:01 -0700 | [diff] [blame] | 95 | unbindAccessDeviceService(accessDeviceServiceRef.get()); |
Matteo Scandolo | d50a4d3 | 2019-04-24 12:10:21 -0700 | [diff] [blame] | 96 | log.info("Stopped AccessDeviceKafkaIntegration"); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 99 | private void handle(AccessDeviceEvent event, String status) { |
| 100 | eventBusService.send(TOPIC, serialize(event, status)); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 103 | private JsonNode serialize(AccessDeviceEvent event, String status) { |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 104 | Port port = event.port().get(); |
| 105 | String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME); |
| 106 | |
| 107 | ObjectMapper mapper = new ObjectMapper(); |
| 108 | ObjectNode onuNode = mapper.createObjectNode(); |
| 109 | onuNode.put(TIMESTAMP, Instant.now().toString()); |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 110 | onuNode.put(STATUS, status); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 111 | onuNode.put(SERIAL_NUMBER, serialNumber); |
Hardik Windlass | 97b4041 | 2019-03-30 20:34:58 +0530 | [diff] [blame] | 112 | onuNode.put(PORT_NUMBER, port.number().toString()); |
| 113 | onuNode.put(DEVICE_ID, port.element().id().toString()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 114 | |
Amit Ghosh | ec08699 | 2020-02-24 13:52:17 +0000 | [diff] [blame] | 115 | Device d = deviceService.getDevice((DeviceId) port.element().id()); |
| 116 | if (d != null) { |
| 117 | onuNode.put(OLT_SERIAL_NUMBER, d.serialNumber()); |
| 118 | } |
| 119 | |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 120 | return onuNode; |
| 121 | } |
| 122 | |
| 123 | private class InternalAccessDeviceListener implements |
| 124 | AccessDeviceListener { |
| 125 | |
| 126 | @Override |
| 127 | public void event(AccessDeviceEvent accessDeviceEvent) { |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 128 | log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type()); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 129 | switch (accessDeviceEvent.type()) { |
| 130 | case UNI_ADDED: |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 131 | handle(accessDeviceEvent, ACTIVATED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 132 | break; |
Matteo Scandolo | d9db2bd | 2018-08-16 11:40:11 -0700 | [diff] [blame] | 133 | case UNI_REMOVED: |
| 134 | handle(accessDeviceEvent, DISABLED); |
Jonathan Hart | 501f788 | 2018-07-24 14:39:57 -0700 | [diff] [blame] | 135 | default: |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | } |