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 | |
| 18 | import java.util.Arrays; |
| 19 | import java.util.Map; |
| 20 | import java.util.Optional; |
| 21 | import java.util.concurrent.ConcurrentHashMap; |
| 22 | |
| 23 | import org.onlab.packet.BasePacket; |
| 24 | import org.onlab.packet.DeserializationException; |
| 25 | import org.onlab.packet.Deserializer; |
| 26 | |
| 27 | /** |
| 28 | * FlowSet is a generic term for a collection of Flow Records that have. |
| 29 | * a similar structure. In an Export Packet, one or more FlowSets |
| 30 | * follow the Packet Header. |
| 31 | * Ref: https://www.ietf.org/rfc/rfc3954.txt |
| 32 | */ |
| 33 | public abstract class FlowSet extends BasePacket { |
| 34 | |
| 35 | public static final int FLOW_SET_HEADER_LENTH = 4; |
| 36 | |
| 37 | public static final int FIELD_LENTH = 4; |
| 38 | |
| 39 | public static final int RECORD_HEADER_LENGTH = 4; |
| 40 | |
| 41 | /** |
| 42 | * FlowSets type |
| 43 | * FlowSets: Template FlowSet, Options Template FlowSet, and Data FlowSet. |
| 44 | */ |
| 45 | public enum Type { |
| 46 | |
| 47 | TEMPLATE_FLOWSET(0, TemplateFlowSet.deserializer()), |
| 48 | OPTIONAL_TEMPLATE_FLOWSET(1, OptionalTemplateFlowSet.deserializer()), |
| 49 | DATA_FLOWSET(Integer.MAX_VALUE, DataFlowSet.deserializer()); |
| 50 | |
| 51 | private final int flowSetId; |
| 52 | private final Deserializer deserializer; |
| 53 | |
| 54 | Type(int flowSetId, Deserializer deserializer) { |
| 55 | this.flowSetId = flowSetId; |
| 56 | this.deserializer = deserializer; |
| 57 | } |
| 58 | |
| 59 | private static Map<Integer, Type> parser = new ConcurrentHashMap<>(); |
| 60 | |
| 61 | static { |
| 62 | Arrays.stream(Type.values()).forEach(type -> parser.put(type.flowSetId, type)); |
| 63 | } |
| 64 | |
| 65 | public static Type getType(int flowSetId) throws DeserializationException { |
| 66 | if (flowSetId < 0) { |
| 67 | throw new DeserializationException("Invalid trap type"); |
| 68 | } |
| 69 | return Optional.of(flowSetId) |
| 70 | .filter(id -> parser.containsKey(id)) |
| 71 | .map(id -> parser.get(id)) |
| 72 | .orElse(DATA_FLOWSET); |
| 73 | } |
| 74 | |
| 75 | public Deserializer getDecoder() { |
| 76 | return this.deserializer; |
| 77 | } |
| 78 | |
| 79 | } |
| 80 | |
| 81 | public abstract Type getType(); |
| 82 | |
| 83 | public abstract int getFlowSetId(); |
| 84 | |
| 85 | public abstract int getLength(); |
| 86 | |
| 87 | @Override |
| 88 | public byte[] serialize() { |
| 89 | throw new UnsupportedOperationException("Not supported yet."); |
| 90 | } |
| 91 | |
| 92 | } |