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