blob: b1ba927a9402b08911bffd80576012e6620e2b06 [file] [log] [blame]
samueljcc4a527ed2015-12-03 13:59:49 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
samueljcc4a527ed2015-12-03 13:59:49 +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 */
16package org.onosproject.driver.extensions;
17
Andrea Campanella238d96e2016-01-20 11:52:02 -080018import com.google.common.base.MoreObjects;
19import com.google.common.collect.Maps;
samueljcc4a527ed2015-12-03 13:59:49 +080020import org.onlab.util.KryoNamespace;
21import org.onosproject.net.flow.AbstractExtension;
22import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
23
Andrea Campanella238d96e2016-01-20 11:52:02 -080024import java.util.Map;
25import java.util.Objects;
samueljcc4a527ed2015-12-03 13:59:49 +080026
27/**
28 * Default implementation of Move treatment.
29 */
30public class DefaultMoveExtensionTreatment extends AbstractExtension
31 implements MoveExtensionTreatment {
32
33 private int srcOfs;
34 private int dstOfs;
35 private int nBits;
36 private int src;
37 private int dst;
38 private ExtensionTreatmentType type;
39
40 private final KryoNamespace appKryo = new KryoNamespace.Builder()
HIGUCHI Yutae81d7e92016-05-30 20:07:48 -070041 .register(Map.class)
Charles Chaneefdedf2016-05-23 16:45:45 -070042 .build("DefaultMoveExtensionTreatment");
samueljcc4a527ed2015-12-03 13:59:49 +080043
44 /**
45 * Creates a new move Treatment.
46 *
47 * @param srcOfs source offset
48 * @param dstOfs destination offset
49 * @param nBits nbits
50 * @param src source
51 * @param dst destination
Phaneendra Mandaefb38752015-12-04 00:43:38 +053052 * @param type extension treatment type
samueljcc4a527ed2015-12-03 13:59:49 +080053 */
54 public DefaultMoveExtensionTreatment(int srcOfs, int dstOfs, int nBits,
55 int src, int dst, ExtensionTreatmentType type) {
56 this.srcOfs = srcOfs;
57 this.dstOfs = dstOfs;
58 this.nBits = nBits;
59 this.src = src;
60 this.dst = dst;
61 this.type = type;
62 }
63
64 @Override
65 public ExtensionTreatmentType type() {
66 return type;
67 }
68
69 @Override
70 public byte[] serialize() {
71 Map<String, Integer> values = Maps.newHashMap();
72 values.put("srcOfs", srcOfs);
73 values.put("dstOfs", dstOfs);
74 values.put("nBits", nBits);
75 values.put("src", src);
76 values.put("dst", dst);
77 values.put("type", ExtensionTreatmentType.ExtensionTreatmentTypes.valueOf(type.toString()).ordinal());
78 return appKryo.serialize(values);
79 }
80
81 @Override
82 public void deserialize(byte[] data) {
83 Map<String, Integer> values = appKryo.deserialize(data);
84 srcOfs = values.get("srcOfs");
85 dstOfs = values.get("dstOfs");
86 nBits = values.get("nBits");
87 src = values.get("src");
88 dst = values.get("dst");
89 type = new ExtensionTreatmentType(values.get("type").intValue());
90 }
91
92 @Override
93 public int srcOffset() {
94 return srcOfs;
95 }
96
97 @Override
98 public int dstOffset() {
99 return dstOfs;
100 }
101
102 @Override
103 public int src() {
104 return src;
105 }
106
107 @Override
108 public int dst() {
109 return dst;
110 }
111
112 @Override
113 public int nBits() {
114 return nBits;
115 }
116
117 @Override
118 public int hashCode() {
119 return Objects.hash(srcOfs, dstOfs, src, dst, nBits);
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127 if (obj instanceof DefaultMoveExtensionTreatment) {
128 DefaultMoveExtensionTreatment that = (DefaultMoveExtensionTreatment) obj;
129 return Objects.equals(srcOfs, that.srcOfs)
130 && Objects.equals(dstOfs, that.dstOfs)
131 && Objects.equals(src, that.src)
132 && Objects.equals(dst, that.dst)
133 && Objects.equals(nBits, that.nBits);
134
135 }
136 return false;
137 }
138
139 @Override
140 public String toString() {
141 return MoreObjects.toStringHelper(getClass()).add("srcOfs", srcOfs)
142 .add("dstOfs", dstOfs).add("nBits", nBits).add("src", src)
143 .add("dst", dst).toString();
144 }
145}