blob: 9090f66eae20fdd74aceee1d626cabfa6e04a46a [file] [log] [blame]
Jonathan Hart501f7882018-07-24 14:39:57 -07001/*
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
17package org.opencord.kafka.integrations;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart501f7882018-07-24 14:39:57 -070022import org.onosproject.net.AnnotationKeys;
Amit Ghoshec086992020-02-24 13:52:17 +000023import org.onosproject.net.Device;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.device.DeviceService;
Jonathan Hart501f7882018-07-24 14:39:57 -070026import org.onosproject.net.Port;
27import org.opencord.kafka.EventBusService;
28import org.opencord.olt.AccessDeviceEvent;
29import org.opencord.olt.AccessDeviceListener;
30import org.opencord.olt.AccessDeviceService;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070031import org.osgi.service.component.annotations.Activate;
32import org.osgi.service.component.annotations.Component;
33import org.osgi.service.component.annotations.Deactivate;
34import org.osgi.service.component.annotations.Reference;
35import org.osgi.service.component.annotations.ReferenceCardinality;
36import org.osgi.service.component.annotations.ReferencePolicy;
Jonathan Hart501f7882018-07-24 14:39:57 -070037import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070039
Jonathan Hart501f7882018-07-24 14:39:57 -070040import java.time.Instant;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070041import java.util.concurrent.atomic.AtomicReference;
Jonathan Hart501f7882018-07-24 14:39:57 -070042
43/**
44 * Listens for access device events and pushes them on a Kafka bus.
45 */
46@Component(immediate = true)
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070047public class AccessDeviceKafkaIntegration extends AbstractKafkaIntegration {
Jonathan Hart501f7882018-07-24 14:39:57 -070048
49 public Logger log = LoggerFactory.getLogger(getClass());
50
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart501f7882018-07-24 14:39:57 -070052 protected EventBusService eventBusService;
53
Amit Ghoshec086992020-02-24 13:52:17 +000054 @Reference(cardinality = ReferenceCardinality.MANDATORY)
55 protected DeviceService deviceService;
56
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070057 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070058 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070059 bind = "bindAccessDeviceService",
60 unbind = "unbindAccessDeviceService")
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070061 protected volatile AccessDeviceService accessDeviceService;
62 private final AtomicReference<AccessDeviceService> accessDeviceServiceRef = new AtomicReference<>();
Jonathan Hart501f7882018-07-24 14:39:57 -070063
64 private final AccessDeviceListener listener = new InternalAccessDeviceListener();
65
66 private static final String TOPIC = "onu.events";
67
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070068 // event fields
Jonathan Hart501f7882018-07-24 14:39:57 -070069 private static final String STATUS = "status";
Hardik Windlass97b40412019-03-30 20:34:58 +053070 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 Hart501f7882018-07-24 14:39:57 -070073 private static final String TIMESTAMP = "timestamp";
Amit Ghoshec086992020-02-24 13:52:17 +000074 private static final String OLT_SERIAL_NUMBER = "oltSerialNumber"; // OLT Serial Number
Jonathan Hart501f7882018-07-24 14:39:57 -070075
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070076 // statuses
77 private static final String ACTIVATED = "activated";
78 private static final String DISABLED = "disabled";
79
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070080 protected void bindAccessDeviceService(AccessDeviceService incomingService) {
81 bindAndAddListener(incomingService, accessDeviceServiceRef, listener);
Matteo Scandolo03f13c12019-03-20 14:38:12 -070082 }
83
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070084 protected void unbindAccessDeviceService(AccessDeviceService outgoingService) {
85 unbindAndRemoveListener(outgoingService, accessDeviceServiceRef, listener);
Matteo Scandolo03f13c12019-03-20 14:38:12 -070086 }
87
Jonathan Hart501f7882018-07-24 14:39:57 -070088 @Activate
89 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070090 log.info("Started AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070091 }
92
93 @Deactivate
94 public void deactivate() {
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070095 unbindAccessDeviceService(accessDeviceServiceRef.get());
Matteo Scandolod50a4d32019-04-24 12:10:21 -070096 log.info("Stopped AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070097 }
98
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070099 private void handle(AccessDeviceEvent event, String status) {
100 eventBusService.send(TOPIC, serialize(event, status));
Jonathan Hart501f7882018-07-24 14:39:57 -0700101 }
102
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700103 private JsonNode serialize(AccessDeviceEvent event, String status) {
Jonathan Hart501f7882018-07-24 14:39:57 -0700104 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 Scandolod9db2bd2018-08-16 11:40:11 -0700110 onuNode.put(STATUS, status);
Jonathan Hart501f7882018-07-24 14:39:57 -0700111 onuNode.put(SERIAL_NUMBER, serialNumber);
Hardik Windlass97b40412019-03-30 20:34:58 +0530112 onuNode.put(PORT_NUMBER, port.number().toString());
113 onuNode.put(DEVICE_ID, port.element().id().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700114
Amit Ghoshec086992020-02-24 13:52:17 +0000115 Device d = deviceService.getDevice((DeviceId) port.element().id());
116 if (d != null) {
117 onuNode.put(OLT_SERIAL_NUMBER, d.serialNumber());
118 }
119
Jonathan Hart501f7882018-07-24 14:39:57 -0700120 return onuNode;
121 }
122
123 private class InternalAccessDeviceListener implements
124 AccessDeviceListener {
125
126 @Override
127 public void event(AccessDeviceEvent accessDeviceEvent) {
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700128 log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type());
Jonathan Hart501f7882018-07-24 14:39:57 -0700129 switch (accessDeviceEvent.type()) {
130 case UNI_ADDED:
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700131 handle(accessDeviceEvent, ACTIVATED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700132 break;
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700133 case UNI_REMOVED:
134 handle(accessDeviceEvent, DISABLED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700135 default:
136 break;
137 }
138 }
139 }
140}