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