karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.netflow.impl; |
| 17 | |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 18 | import java.util.Optional; |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 19 | import org.jboss.netty.channel.ChannelHandlerContext; |
| 20 | import org.jboss.netty.channel.ChannelStateEvent; |
| 21 | import org.jboss.netty.channel.ExceptionEvent; |
| 22 | import org.jboss.netty.channel.MessageEvent; |
| 23 | import org.jboss.netty.channel.Channel; |
| 24 | import org.jboss.netty.channel.SimpleChannelHandler; |
| 25 | |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 26 | import org.slf4j.Logger; |
| 27 | import org.slf4j.LoggerFactory; |
| 28 | |
| 29 | import org.onosproject.netflow.DataTemplateRecord; |
| 30 | import org.onosproject.netflow.FlowSet; |
| 31 | import org.onosproject.netflow.TemplateFlowSet; |
| 32 | import org.onosproject.netflow.DataFlowSet; |
| 33 | import org.onosproject.netflow.TemplateId; |
| 34 | import org.onosproject.netflow.NetflowController; |
| 35 | |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 36 | /** |
| 37 | * Channel handler deals with the netfow exporter connection and dispatches messages |
| 38 | * from netfow exporter to the appropriate locations. |
| 39 | */ |
| 40 | public class NeflowChannelHandler extends SimpleChannelHandler { |
| 41 | |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 42 | private static final Logger log = LoggerFactory.getLogger(NeflowChannelHandler.class); |
| 43 | |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 44 | private Channel channel; |
| 45 | |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 46 | private NetflowController controller; |
| 47 | |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 48 | /** |
| 49 | * Create a new netflow channelHandler instance. |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 50 | * |
| 51 | * @param controller netflow controller. |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 52 | */ |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 53 | NeflowChannelHandler(NetflowController controller) { |
| 54 | this.controller = controller; |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 55 | } |
| 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 { |
senthil | 7f2018e | 2023-06-21 22:00:44 +0530 | [diff] [blame] | 102 | 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 | |
karthik1977 | bc5ea1e | 2023-01-02 19:25:14 +0530 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } |