blob: 20ed5d74be54d154f8d798ae00f0da5c179b5ab0 [file] [log] [blame]
Frank Wang5733c382017-03-28 10:15:18 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Frank Wang5733c382017-03-28 10:15:18 +08003 *
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.driver.extensions;
18
19import com.google.common.base.MoreObjects;
20import com.google.common.collect.Maps;
21import org.onlab.packet.IpAddress;
22import org.onlab.util.KryoNamespace;
23import org.onosproject.net.flow.AbstractExtension;
24import org.onosproject.net.flow.instructions.ExtensionTreatment;
25import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
26import org.onosproject.store.serializers.IpAddressSerializer;
27
28import java.util.Map;
29import java.util.Objects;
30
31/**
32 * Nicira nat extension instruction.
33 */
34public class NiciraNat extends AbstractExtension implements ExtensionTreatment {
35 private int flags;
36 private int presentFlags;
37 private int portMin;
38 private int portMax;
39 private IpAddress ipAddressMin;
40 private IpAddress ipAddressMax;
41 private final KryoNamespace appKryo = new KryoNamespace.Builder()
42 .register(new IpAddressSerializer(), IpAddress.class)
43 .register(byte[].class)
44 .build();
45
46 /**
47 * Creates a new nat instruction.
48 */
49 public NiciraNat() {
50 flags = 0;
51 presentFlags = 0;
52 portMin = 0;
53 portMax = 0;
54 ipAddressMin = IpAddress.valueOf(0);
55 ipAddressMax = IpAddress.valueOf(0);
56 }
57
58 /**
59 * Creates a new nat instruction.
60 * @param flags nat flags
61 * @param presentFlags nat present flags
62 * @param portMin min port
63 * @param portMax max port
64 * @param ipAddressMin min ip address
65 * @param ipAddressMax max ip address
66 */
67 public NiciraNat(int flags, int presentFlags, int portMin, int portMax, IpAddress ipAddressMin,
68 IpAddress ipAddressMax) {
69 this.flags = flags;
70 this.presentFlags = presentFlags;
71 this.portMin = portMin;
72 this.portMax = portMax;
73 this.ipAddressMin = ipAddressMin;
74 this.ipAddressMax = ipAddressMax;
75 }
76
77 @Override
78 public ExtensionTreatmentType type() {
79 return ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_NAT.type();
80 }
81
82 /**
83 * Get Nicira nat flags.
84 * @return flags
85 */
86 public int niciraNatFlags() {
87 return flags;
88 }
89
90 /**
91 * Get Nicira present flags.
92 * @return present flags
93 */
94 public int niciraNatPresentFlags() {
95 return presentFlags;
96 }
97
98 /**
99 * Get Nicira Nat min port.
100 * @return min port
101 */
102 public int niciraNatPortMin() {
103 return portMin;
104 }
105
106 /**
107 * Get Nicira Nat max port.
108 * @return max port
109 */
110 public int niciraNatPortMax() {
111 return portMax;
112 }
113
114 /**
115 * Get Nicira Nat min ip address.
116 * @return min ipaddress
117 */
118 public IpAddress niciraNatIpAddressMin() {
119 return ipAddressMin;
120 }
121
122 /**
123 * Get Nicira Nat max ip address.
124 * @return max ipaddress
125 */
126 public IpAddress niciraNatIpAddressMax() {
127 return ipAddressMax;
128 }
129
130 @Override
131 public void deserialize(byte[] data) {
132 Map<String, Object> values = appKryo.deserialize(data);
133 flags = (int) values.get("flags");
134 presentFlags = (int) values.get("presentFlags");
135 portMin = (int) values.get("portMin");
136 portMax = (int) values.get("portMax");
137 ipAddressMin = (IpAddress) values.get("ipAddressMin");
138 ipAddressMax = (IpAddress) values.get("ipAddressMax");
139 }
140
141 @Override
142 public byte[] serialize() {
143 Map<String, Object> values = Maps.newHashMap();
144 values.put("flags", flags);
145 values.put("presentFlags", presentFlags);
146 values.put("portMin", portMin);
147 values.put("portMax", portMax);
148 values.put("ipAddressMin", ipAddressMin);
149 values.put("ipAddressMax", ipAddressMax);
150 return appKryo.serialize(values);
151 }
152
153 @Override
154 public int hashCode() {
155 return Objects.hash(type(), flags, presentFlags, portMin, portMax, ipAddressMin, ipAddressMax);
156 }
157
158 @Override
159 public boolean equals(Object obj) {
160 if (this == obj) {
161 return true;
162 }
163 if (obj instanceof NiciraNat) {
164 NiciraNat that = (NiciraNat) obj;
165 return Objects.equals(flags, that.flags) &&
166 Objects.equals(presentFlags, that.presentFlags) &&
167 Objects.equals(portMin, that.portMin) &&
168 Objects.equals(portMax, that.portMax) &&
169 Objects.equals(ipAddressMin, that.ipAddressMin) &&
170 Objects.equals(ipAddressMax, that.ipAddressMax) &&
171 Objects.equals(this.type(), that.type());
172 }
173 return false;
174 }
175
176 @Override
177 public String toString() {
178 return MoreObjects.toStringHelper(getClass())
179 .add("flags", flags)
180 .add("present_flags", presentFlags)
181 .add("portMin", portMin)
182 .add("portMax", portMax)
183 .add("ipAddressMin", ipAddressMin)
184 .add("ipAddressMax", ipAddressMax)
185 .toString();
186 }
187}