blob: 8dddb78c656750c85c2bf486801b7dfaea798018 [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
Matteo Scandolo03f13c12019-03-20 14:38:12 -070048 @Reference(cardinality = ReferenceCardinality.OPTIONAL_UNARY,
49 bind = "bindAccessDeviceService",
50 unbind = "unbindAccessDeviceService")
Jonathan Hart501f7882018-07-24 14:39:57 -070051 protected AccessDeviceService accessDeviceService;
52
53 private final AccessDeviceListener listener = new InternalAccessDeviceListener();
54
55 private static final String TOPIC = "onu.events";
56
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070057 // event fields
Jonathan Hart501f7882018-07-24 14:39:57 -070058 private static final String STATUS = "status";
Hardik Windlass97b40412019-03-30 20:34:58 +053059 private static final String SERIAL_NUMBER = "serialNumber";
60 private static final String PORT_NUMBER = "portNumber"; // uni port
61 private static final String DEVICE_ID = "deviceId"; // OLT OpenFlow Id
Jonathan Hart501f7882018-07-24 14:39:57 -070062 private static final String TIMESTAMP = "timestamp";
63
Matteo Scandolod9db2bd2018-08-16 11:40:11 -070064 // statuses
65 private static final String ACTIVATED = "activated";
66 private static final String DISABLED = "disabled";
67
Matteo Scandolo03f13c12019-03-20 14:38:12 -070068 protected void bindAccessDeviceService(AccessDeviceService accessDeviceService) {
69 if (this.accessDeviceService == null) {
70 log.info("Binding AccessDeviceService");
71 this.accessDeviceService = accessDeviceService;
72 log.info("Adding listener on AccessDeviceService");
73 accessDeviceService.addListener(listener);
74 } else {
75 log.warn("Trying to bind AccessDeviceService but it is already bound");
76 }
77 }
78
79 protected void unbindAccessDeviceService(AccessDeviceService accessDeviceService) {
80 if (this.accessDeviceService == accessDeviceService) {
81 log.info("Unbinding AccessDeviceService");
82 this.accessDeviceService = null;
83 log.info("Removing listener on AccessDeviceService");
84 accessDeviceService.removeListener(listener);
85 } else {
86 log.warn("Trying to unbind AccessDeviceService but it is already unbound");
87 }
88 }
89
Jonathan Hart501f7882018-07-24 14:39:57 -070090 @Activate
91 public void activate() {
Jonathan Hart501f7882018-07-24 14:39:57 -070092 log.info("Started");
93 }
94
95 @Deactivate
96 public void deactivate() {
Jonathan Hart501f7882018-07-24 14:39:57 -070097 log.info("Stopped");
98 }
99
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700100 private void handle(AccessDeviceEvent event, String status) {
101 eventBusService.send(TOPIC, serialize(event, status));
Jonathan Hart501f7882018-07-24 14:39:57 -0700102 }
103
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700104 private JsonNode serialize(AccessDeviceEvent event, String status) {
Jonathan Hart501f7882018-07-24 14:39:57 -0700105 Port port = event.port().get();
106 String serialNumber = port.annotations().value(AnnotationKeys.PORT_NAME);
107
108 ObjectMapper mapper = new ObjectMapper();
109 ObjectNode onuNode = mapper.createObjectNode();
110 onuNode.put(TIMESTAMP, Instant.now().toString());
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700111 onuNode.put(STATUS, status);
Jonathan Hart501f7882018-07-24 14:39:57 -0700112 onuNode.put(SERIAL_NUMBER, serialNumber);
Hardik Windlass97b40412019-03-30 20:34:58 +0530113 onuNode.put(PORT_NUMBER, port.number().toString());
114 onuNode.put(DEVICE_ID, port.element().id().toString());
Jonathan Hart501f7882018-07-24 14:39:57 -0700115
116 return onuNode;
117 }
118
119 private class InternalAccessDeviceListener implements
120 AccessDeviceListener {
121
122 @Override
123 public void event(AccessDeviceEvent accessDeviceEvent) {
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700124 log.info("Got AccessDeviceEvent: " + accessDeviceEvent.type());
Jonathan Hart501f7882018-07-24 14:39:57 -0700125 switch (accessDeviceEvent.type()) {
126 case UNI_ADDED:
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700127 handle(accessDeviceEvent, ACTIVATED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700128 break;
Matteo Scandolod9db2bd2018-08-16 11:40:11 -0700129 case UNI_REMOVED:
130 handle(accessDeviceEvent, DISABLED);
Jonathan Hart501f7882018-07-24 14:39:57 -0700131 default:
132 break;
133 }
134 }
135 }
136}