blob: 36954ad7e6ea4fc85cbd38b4a1662a348f4c3804 [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;
22import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Matteo Scandolod50a4d32019-04-24 12:10:21 -070027import org.apache.felix.scr.annotations.ReferencePolicy;
Jonathan Hart501f7882018-07-24 14:39:57 -070028import org.onosproject.net.AnnotationKeys;
29import org.onosproject.net.Port;
30import org.opencord.kafka.EventBusService;
31import org.opencord.olt.AccessDeviceEvent;
32import org.opencord.olt.AccessDeviceListener;
33import org.opencord.olt.AccessDeviceService;
34import org.slf4j.Logger;
35import org.slf4j.LoggerFactory;
Jonathan Hart501f7882018-07-24 14:39:57 -070036import java.time.Instant;
37
38/**
39 * Listens for access device events and pushes them on a Kafka bus.
40 */
41@Component(immediate = true)
42public class AccessDeviceKafkaIntegration {
43
44 public Logger log = LoggerFactory.getLogger(getClass());
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected EventBusService eventBusService;
48
Matteo Scandolo03f13c12019-03-20 14:38:12 -070049 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
Matteo Scandolod50a4d32019-04-24 12:10:21 -070050 policy = ReferencePolicy.DYNAMIC,
Matteo Scandolo03f13c12019-03-20 14:38:12 -070051 bind = "bindAccessDeviceService",
52 unbind = "unbindAccessDeviceService")
Jonathan Hart501f7882018-07-24 14:39:57 -070053 protected AccessDeviceService accessDeviceService;
54
55 private final AccessDeviceListener listener = new InternalAccessDeviceListener();
56
57 private static final String TOPIC = "onu.events";
58
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070059 // event fields
Jonathan Hart501f7882018-07-24 14:39:57 -070060 private static final String STATUS = "status";
Hardik Windlass97b40412019-03-30 20:34:58 +053061 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 Hart501f7882018-07-24 14:39:57 -070064 private static final String TIMESTAMP = "timestamp";
65
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070066 // statuses
67 private static final String ACTIVATED = "activated";
68 private static final String DISABLED = "disabled";
69
Matteo Scandolo03f13c12019-03-20 14:38:12 -070070 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 Hart501f7882018-07-24 14:39:57 -070092 @Activate
93 public void activate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070094 log.info("Started AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -070095 }
96
97 @Deactivate
98 public void deactivate() {
Matteo Scandolod50a4d32019-04-24 12:10:21 -070099 log.info("Stopped AccessDeviceKafkaIntegration");
Jonathan Hart501f7882018-07-24 14:39:57 -0700100 }
101
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700102 private void handle(AccessDeviceEvent event, String status) {
103 eventBusService.send(TOPIC, serialize(event, status));
Jonathan Hart501f7882018-07-24 14:39:57 -0700104 }
105
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700106 private JsonNode serialize(AccessDeviceEvent event, String status) {
Jonathan Hart501f7882018-07-24 14:39:57 -0700107 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 Scandolod9db2bd2018-08-16 11:40:11 -0700113 onuNode.put(STATUS, status);
Jonathan Hart501f7882018-07-24 14:39:57 -0700114 onuNode.put(SERIAL_NUMBER, serialNumber);
Hardik Windlass97b40412019-03-30 20:34:58 +0530115 onuNode.put(PORT_NUMBER, port.number().toString());
116 onuNode.put(DEVICE_ID, port.element().id().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700117
118 return onuNode;
119 }
120
121 private class InternalAccessDeviceListener implements
122 AccessDeviceListener {
123
124 @Override
125 public void event(AccessDeviceEvent accessDeviceEvent) {
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700126 log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type());
Jonathan Hart501f7882018-07-24 14:39:57 -0700127 switch (accessDeviceEvent.type()) {
128 case UNI_ADDED:
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700129 handle(accessDeviceEvent, ACTIVATED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700130 break;
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700131 case UNI_REMOVED:
132 handle(accessDeviceEvent, DISABLED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700133 default:
134 break;
135 }
136 }
137 }
138}