blob: 0c4628c1181883fc56cf0449029e5d1e4611a607 [file] [log] [blame]
pierventre4b72c472020-05-22 09:42:31 -07001/*
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
17package org.onosproject.net;
18
19import org.onosproject.net.flow.FlowEntry;
20import org.onosproject.net.group.Group;
21
22import java.util.Objects;
23
24/**
25 * Generic abstraction to hold dataplane entities.
26 */
27public 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}