blob: 27eb8eadd799f98f69a6670635a29c6b8b08b3d0 [file] [log] [blame]
karthik1977bc5ea1e2023-01-02 19:25:14 +05301/*
2 * Copyright 2023-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 */
16package org.onosproject.netflow.impl;
17
senthil7f2018e2023-06-21 22:00:44 +053018import java.util.Optional;
karthik1977bc5ea1e2023-01-02 19:25:14 +053019import org.jboss.netty.channel.ChannelHandlerContext;
20import org.jboss.netty.channel.ChannelStateEvent;
21import org.jboss.netty.channel.ExceptionEvent;
22import org.jboss.netty.channel.MessageEvent;
23import org.jboss.netty.channel.Channel;
24import org.jboss.netty.channel.SimpleChannelHandler;
25
senthil7f2018e2023-06-21 22:00:44 +053026import org.slf4j.Logger;
27import org.slf4j.LoggerFactory;
28
29import org.onosproject.netflow.DataTemplateRecord;
30import org.onosproject.netflow.FlowSet;
31import org.onosproject.netflow.TemplateFlowSet;
32import org.onosproject.netflow.DataFlowSet;
33import org.onosproject.netflow.TemplateId;
34import org.onosproject.netflow.NetflowController;
35
karthik1977bc5ea1e2023-01-02 19:25:14 +053036/**
37 * Channel handler deals with the netfow exporter connection and dispatches messages
38 * from netfow exporter to the appropriate locations.
39 */
40public class NeflowChannelHandler extends SimpleChannelHandler {
41
senthil7f2018e2023-06-21 22:00:44 +053042 private static final Logger log = LoggerFactory.getLogger(NeflowChannelHandler.class);
43
karthik1977bc5ea1e2023-01-02 19:25:14 +053044 private Channel channel;
45
senthil7f2018e2023-06-21 22:00:44 +053046 private NetflowController controller;
47
karthik1977bc5ea1e2023-01-02 19:25:14 +053048 /**
49 * Create a new netflow channelHandler instance.
senthil7f2018e2023-06-21 22:00:44 +053050 *
51 * @param controller netflow controller.
karthik1977bc5ea1e2023-01-02 19:25:14 +053052 */
senthil7f2018e2023-06-21 22:00:44 +053053 NeflowChannelHandler(NetflowController controller) {
54 this.controller = controller;
karthik1977bc5ea1e2023-01-02 19:25:14 +053055 }
56
57 /**
58 * Netflow channel connect to netflow exporter.
59 *
60 * @param ctx channel handler context
61 * @param event channel state event
62 * @throws Exception on error while connecting channel
63 */
64 @Override
65 public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent event) throws Exception {
66 channel = event.getChannel();
67 }
68
69 /**
70 * Netflow channel disconnect to netflow exporter.
71 *
72 * @param ctx channel handler context
73 * @param event channel state event
74 * @throws Exception on error while disconnecting channel
75 */
76 @Override
77 public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent event) throws Exception {
78 channel = event.getChannel();
79 }
80
81 /**
82 * Netflow channel exception to netflow exporter.
83 *
84 * @param ctx channel handler context
85 * @param event channel exception event
86 * @throws Exception on error while parsing exception
87 */
88 @Override
89 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent event) throws Exception {
90 //TODO exception handler
91 }
92
93 /**
94 * Netflow message receive from netflow exporter.
95 *
96 * @param ctx channel handler context
97 * @param event channel message event
98 * @throws Exception on error while parsing exception
99 */
100 @Override
101 public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throws Exception {
senthil7f2018e2023-06-21 22:00:44 +0530102 try {
103 NetFlowPacket netFlowPacket = (NetFlowPacket) event.getMessage();
104 netFlowPacket.getFlowSets()
105 .stream()
106 .filter(n -> n.getType() == FlowSet.Type.TEMPLATE_FLOWSET)
107 .map(t -> (TemplateFlowSet) t)
108 .flatMap(t -> t.getRecords().stream())
109 .forEach(t -> controller.addTemplateFlowSet(t));
110
111 netFlowPacket.getFlowSets()
112 .stream()
113 .filter(n -> n.getType() == FlowSet.Type.DATA_FLOWSET)
114 .map(t -> (DataFlowSet) t)
115 .forEach(data -> {
116 Optional<DataTemplateRecord> template = controller
117 .getTemplateFlowSet(TemplateId.valueOf(data.getFlowSetId()));
118 if (!template.isPresent()) {
119 return;
120 }
121 try {
122 data.dataDeserializer(template.get());
123 data.getDataFlow()
124 .stream()
125 .forEach(dataflow -> controller.updateDataFlowSet(dataflow));
126 } catch (Exception ex) {
127 log.error("Netflow dataflow deserializer exception ", ex);
128 }
129 });
130 log.info("Netflow message received {}", netFlowPacket);
131 } catch (Exception er) {
132 log.error("Netflow message deserializer exception ", er);
133 }
134
karthik1977bc5ea1e2023-01-02 19:25:14 +0530135 }
136
137}