pierventre | b58e9c9 | 2020-05-22 09:42:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.net; |
| 18 | |
| 19 | import org.onosproject.net.flow.FlowEntry; |
| 20 | import org.onosproject.net.group.Group; |
| 21 | |
| 22 | import java.util.Objects; |
| 23 | |
| 24 | /** |
| 25 | * Generic abstraction to hold dataplane entities. |
| 26 | */ |
| 27 | public class DataPlaneEntity { |
| 28 | private FlowEntry flowEntry; |
| 29 | private Group groupEntry; |
| 30 | private Type type; |
| 31 | |
| 32 | /** |
| 33 | * Types of entity. |
| 34 | */ |
| 35 | public enum Type { |
| 36 | /** |
| 37 | * Flow rule entity. |
| 38 | */ |
| 39 | FLOWRULE, |
| 40 | |
| 41 | /** |
| 42 | * Group entity. |
| 43 | */ |
| 44 | GROUP |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates a dataplane entity from a flow entry. |
| 49 | * |
| 50 | * @param flow the inner flow entry |
| 51 | */ |
| 52 | public DataPlaneEntity(FlowEntry flow) { |
| 53 | flowEntry = flow; |
| 54 | type = Type.FLOWRULE; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a dataplane entity from a group entry. |
| 59 | * |
| 60 | * @param group the inner group entry |
| 61 | */ |
| 62 | public DataPlaneEntity(Group group) { |
| 63 | groupEntry = group; |
| 64 | type = Type.GROUP; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the flow entry. |
| 69 | * |
| 70 | * @return the flow entry |
| 71 | */ |
| 72 | public FlowEntry getFlowEntry() { |
| 73 | return flowEntry; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Returns the group entry. |
| 78 | * |
| 79 | * @return the group entry |
| 80 | */ |
| 81 | public Group getGroupEntry() { |
| 82 | return groupEntry; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Returns the type of the entry. |
| 87 | * |
| 88 | * @return the type |
| 89 | */ |
| 90 | public Type getType() { |
| 91 | return type; |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public int hashCode() { |
| 96 | return type == Type.FLOWRULE ? flowEntry.hashCode() : groupEntry.hashCode(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean equals(Object obj) { |
| 101 | if (this == obj) { |
| 102 | return true; |
| 103 | } |
| 104 | if (obj instanceof DataPlaneEntity) { |
| 105 | DataPlaneEntity that = (DataPlaneEntity) obj; |
| 106 | if (this.type == that.type) { |
| 107 | return Objects.equals(flowEntry, that.flowEntry) && |
| 108 | Objects.equals(groupEntry, that.groupEntry); |
| 109 | } |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public String toString() { |
| 116 | Object entity = type == Type.FLOWRULE ? flowEntry : groupEntry; |
| 117 | return "DataPlaneEntity{" + |
| 118 | "entity=" + entity + |
| 119 | '}'; |
| 120 | } |
| 121 | } |