blob: 9ffb90f97c685fdcb96bf6fd5f4e10b76064bef5 [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;
23import org.onosproject.net.Port;
24import org.opencord.kafka.EventBusService;
25import org.opencord.olt.AccessDeviceEvent;
26import org.opencord.olt.AccessDeviceListener;
27import org.opencord.olt.AccessDeviceService;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070028import org.osgi.service.component.annotations.Activate;
29import org.osgi.service.component.annotations.Component;
30import org.osgi.service.component.annotations.Deactivate;
31import org.osgi.service.component.annotations.Reference;
32import org.osgi.service.component.annotations.ReferenceCardinality;
33import org.osgi.service.component.annotations.ReferencePolicy;
Jonathan Hart501f7882018-07-24 14:39:57 -070034import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070036
Jonathan Hart501f7882018-07-24 14:39:57 -070037import java.time.Instant;
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070038import java.util.concurrent.atomic.AtomicReference;
Jonathan Hart501f7882018-07-24 14:39:57 -070039
40/**
41 * Listens for access device events and pushes them on a Kafka bus.
42 */
43@Component(immediate = true)
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070044public class AccessDeviceKafkaIntegration extends AbstractKafkaIntegration {
Jonathan Hart501f7882018-07-24 14:39:57 -070045
46 public Logger log = LoggerFactory.getLogger(getClass());
47
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hart501f7882018-07-24 14:39:57 -070049 protected EventBusService eventBusService;
50
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070051 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070052 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070053 bind = "bindAccessDeviceService",
54 unbind = "unbindAccessDeviceService")
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070055 protected volatile AccessDeviceService accessDeviceService;
56 private final AtomicReference<AccessDeviceService> accessDeviceServiceRef = new AtomicReference<>();
Jonathan Hart501f7882018-07-24 14:39:57 -070057
58 private final AccessDeviceListener listener = new InternalAccessDeviceListener();
59
60 private static final String TOPIC = "onu.events";
61
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070062 // event fields
Jonathan Hart501f7882018-07-24 14:39:57 -070063 private static final String STATUS = "status";
Hardik Windlass97b40412019-03-30 20:34:58 +053064 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 Hart501f7882018-07-24 14:39:57 -070067 private static final String TIMESTAMP = "timestamp";
68
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070069 // statuses
70 private static final String ACTIVATED = "activated";
71 private static final String DISABLED = "disabled";
72
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070073 protected void bindAccessDeviceService(AccessDeviceService incomingService) {
74 bindAndAddListener(incomingService, accessDeviceServiceRef, listener);
Matteo Scandolo03f13c12019-03-20 14:38:12 -070075 }
76
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070077 protected void unbindAccessDeviceService(AccessDeviceService outgoingService) {
78 unbindAndRemoveListener(outgoingService, accessDeviceServiceRef, listener);
Matteo Scandolo03f13c12019-03-20 14:38:12 -070079 }
80
Jonathan Hart501f7882018-07-24 14:39:57 -070081 @Activate
82 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070083 log.info("Started AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070084 }
85
86 @Deactivate
87 public void deactivate() {
Carmelo Cascone7e73fa12019-07-15 18:29:01 -070088 unbindAccessDeviceService(accessDeviceServiceRef.get());
Matteo Scandolod50a4d32019-04-24 12:10:21 -070089 log.info("Stopped AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070090 }
91
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070092 private void handle(AccessDeviceEvent event, String status) {
93 eventBusService.send(TOPIC, serialize(event, status));
Jonathan Hart501f7882018-07-24 14:39:57 -070094 }
95
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070096 private JsonNode serialize(AccessDeviceEvent event, String status) {
Jonathan Hart501f7882018-07-24 14:39:57 -070097 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 Scandolod9db2bd2018-08-16 11:40:11 -0700103 onuNode.put(STATUS, status);
Jonathan Hart501f7882018-07-24 14:39:57 -0700104 onuNode.put(SERIAL_NUMBER, serialNumber);
Hardik Windlass97b40412019-03-30 20:34:58 +0530105 onuNode.put(PORT_NUMBER, port.number().toString());
106 onuNode.put(DEVICE_ID, port.element().id().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700107
108 return onuNode;
109 }
110
111 private class InternalAccessDeviceListener implements
112 AccessDeviceListener {
113
114 @Override
115 public void event(AccessDeviceEvent accessDeviceEvent) {
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700116 log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type());
Jonathan Hart501f7882018-07-24 14:39:57 -0700117 switch (accessDeviceEvent.type()) {
118 case UNI_ADDED:
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700119 handle(accessDeviceEvent, ACTIVATED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700120 break;
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700121 case UNI_REMOVED:
122 handle(accessDeviceEvent, DISABLED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700123 default:
124 break;
125 }
126 }
127 }
128}