blob: 5a8015496422e51f209068d87621e539e43b3126 [file] [log] [blame]
Henry Yu05dcc212017-01-05 16:05:26 -05001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Henry Yu05dcc212017-01-05 16:05:26 -05003 *
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.onosproject.provider.te.utils;
18
19import org.onosproject.net.DeviceId;
20import org.onosproject.protocol.restconf.RestconfNotificationEventListener;
21import org.slf4j.Logger;
22
23import java.util.Map;
24import java.util.concurrent.ConcurrentHashMap;
25
26import static org.slf4j.LoggerFactory.getLogger;
27
28/**
29 * Implementation of the RESTCONF notification event
30 * listener for TE Topology.
31 */
32public class TeTopologyRestconfEventListener implements
33 RestconfNotificationEventListener<String> {
34 private static final String TE_TOPOLOGY_NOTIFICATION_PREFIX =
35 "{\"ietf-te-topology:ietf-te-topology\":";
36 private static final String TE_LINK_EVENT_PREFIX =
37 "{\"ietf-te-topology:te-link-event\":";
38 private static final String TE_NODE_EVENT_PREFIX =
39 "{\"ietf-te-topology:te-node-event\":";
40
41 private final Logger log = getLogger(getClass());
42
43 private Map<TeTopologyRestconfEventType, RestconfNotificationEventProcessor>
44 eventCallbackFunctionMap = new ConcurrentHashMap<>();
45
46 @Override
47 public void handleNotificationEvent(DeviceId deviceId,
48 String eventJsonString) {
49 log.debug("New notification: {} for device: {}",
50 eventJsonString, deviceId.toString());
51
52 if (!eventJsonString.startsWith(TE_TOPOLOGY_NOTIFICATION_PREFIX)) {
53 // This is not a TE topology event.
54 return;
55 }
56
57 String teEventString = removePrefixTagFromJson(eventJsonString,
58 TE_TOPOLOGY_NOTIFICATION_PREFIX);
59
60 TeTopologyRestconfEventType eventType = getEventType(teEventString);
61
62 if (eventType == TeTopologyRestconfEventType.TE_UNKNOWN_EVENT) {
63 log.error("handleNotificationEvent: unknown event: {}", eventJsonString);
64 return;
65 }
66
67 RestconfNotificationEventProcessor eventProcessor =
68 eventCallbackFunctionMap.get(eventType);
69
70 if (eventProcessor != null) {
71 eventProcessor.processEventPayload(teEventString);
72 } else {
73 log.info("Event callback not installed for event type: {}", eventType);
74 }
75 }
76
77 /**
78 * Registers an notification event callback function which is called by
79 * the listener when it receives an event.
80 *
81 * @param eventType notification event type corresponding to the
82 * callback function
83 * @param eventProcessor callback function
84 */
85 public void addCallbackFunction(TeTopologyRestconfEventType eventType,
86 RestconfNotificationEventProcessor eventProcessor) {
87 if (eventCallbackFunctionMap.containsKey(eventType)) {
88 removeCallbackFunction(eventType);
89 }
90
91 eventCallbackFunctionMap.put(eventType, eventProcessor);
92 }
93
94 /**
95 * Removes the callback function associated with the given event type.
96 *
97 * @param eventType notification event type
98 */
99 public void removeCallbackFunction(TeTopologyRestconfEventType eventType) {
100 eventCallbackFunctionMap.remove(eventType);
101 }
102
103 @Override
104 public boolean equals(Object o) {
105 if (this == o) {
106 return true;
107 }
108 if (o == null || getClass() != o.getClass()) {
109 return false;
110 }
111
112 TeTopologyRestconfEventListener that = (TeTopologyRestconfEventListener) o;
113
114 return eventCallbackFunctionMap != null ?
115 eventCallbackFunctionMap.equals(that.eventCallbackFunctionMap) :
116 that.eventCallbackFunctionMap == null;
117 }
118
119 @Override
120 public int hashCode() {
121 return eventCallbackFunctionMap != null ? eventCallbackFunctionMap.hashCode() : 0;
122 }
123
124 private String removePrefixTagFromJson(String jsonString, String prefixTag) {
125 if (jsonString.startsWith(prefixTag)) {
126 return jsonString.substring(prefixTag.length(), jsonString.length() - 1);
127 }
128 return jsonString;
129 }
130
131 private TeTopologyRestconfEventType getEventType(String teEventString) {
132 if (teEventString.startsWith(TE_LINK_EVENT_PREFIX)) {
133 return TeTopologyRestconfEventType.TE_TOPOLOGY_LINK_NOTIFICATION;
134 }
135
136 if (teEventString.startsWith(TE_NODE_EVENT_PREFIX)) {
137 return TeTopologyRestconfEventType.TE_TOPOLOGY_NODE_NOTIFICATION;
138 }
139
140 return TeTopologyRestconfEventType.TE_UNKNOWN_EVENT;
141 }
142}
143