blob: 15f317f5794b6060381a90dfd068c3cd3f68bcfc [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;
27import org.onosproject.net.AnnotationKeys;
28import org.onosproject.net.Port;
29import org.opencord.kafka.EventBusService;
30import org.opencord.olt.AccessDeviceEvent;
31import org.opencord.olt.AccessDeviceListener;
32import org.opencord.olt.AccessDeviceService;
33import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
Jonathan Hart501f7882018-07-24 14:39:57 -070035import java.time.Instant;
36
37/**
38 * Listens for access device events and pushes them on a Kafka bus.
39 */
40@Component(immediate = true)
41public class AccessDeviceKafkaIntegration {
42
43 public Logger log = LoggerFactory.getLogger(getClass());
44
45 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
46 protected EventBusService eventBusService;
47
48 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected AccessDeviceService accessDeviceService;
50
51 private final AccessDeviceListener listener = new InternalAccessDeviceListener();
52
53 private static final String TOPIC = "onu.events";
54
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070055 // event fields
Jonathan Hart501f7882018-07-24 14:39:57 -070056 private static final String STATUS = "status";
Hardik Windlass97b40412019-03-30 20:34:58 +053057 private static final String SERIAL_NUMBER = "serialNumber";
58 private static final String PORT_NUMBER = "portNumber"; // uni port
59 private static final String DEVICE_ID = "deviceId"; // OLT OpenFlow Id
Jonathan Hart501f7882018-07-24 14:39:57 -070060 private static final String TIMESTAMP = "timestamp";
61
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070062 // statuses
63 private static final String ACTIVATED = "activated";
64 private static final String DISABLED = "disabled";
65
Jonathan Hart501f7882018-07-24 14:39:57 -070066 @Activate
67 public void activate() {
68 accessDeviceService.addListener(listener);
69 log.info("Started");
70 }
71
72 @Deactivate
73 public void deactivate() {
74 accessDeviceService.removeListener(listener);
75 log.info("Stopped");
76 }
77
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070078 private void handle(AccessDeviceEvent event, String status) {
79 eventBusService.send(TOPIC, serialize(event, status));
Jonathan Hart501f7882018-07-24 14:39:57 -070080 }
81
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070082 private JsonNode serialize(AccessDeviceEvent event, String status) {
Jonathan Hart501f7882018-07-24 14:39:57 -070083 Port port = event.port().get();
84 String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME);
85
86 ObjectMapper mapper = new ObjectMapper();
87 ObjectNode onuNode = mapper.createObjectNode();
88 onuNode.put(TIMESTAMP, Instant.now().toString());
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070089 onuNode.put(STATUS, status);
Jonathan Hart501f7882018-07-24 14:39:57 -070090 onuNode.put(SERIAL_NUMBER, serialNumber);
Hardik Windlass97b40412019-03-30 20:34:58 +053091 onuNode.put(PORT_NUMBER, port.number().toString());
92 onuNode.put(DEVICE_ID, port.element().id().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -070093
94 return onuNode;
95 }
96
97 private class InternalAccessDeviceListener implements
98 AccessDeviceListener {
99
100 @Override
101 public void event(AccessDeviceEvent accessDeviceEvent) {
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700102 log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type());
Jonathan Hart501f7882018-07-24 14:39:57 -0700103 switch (accessDeviceEvent.type()) {
104 case UNI_ADDED:
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700105 handle(accessDeviceEvent, ACTIVATED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700106 break;
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700107 case UNI_REMOVED:
108 handle(accessDeviceEvent, DISABLED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700109 default:
110 break;
111 }
112 }
113 }
114}