blob: 140a816794aa43b0932835012a7c7e53a9373f40 [file] [log] [blame]
samueljcc4a527ed2015-12-03 13:59:49 +08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.driver.extensions;
17
18import java.util.Map;
19import java.util.Objects;
20
21import org.onlab.util.KryoNamespace;
22import org.onosproject.net.flow.AbstractExtension;
23import org.onosproject.net.flow.instructions.ExtensionTreatmentType;
24
25import com.google.common.base.MoreObjects;
26import com.google.common.collect.Maps;
27
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()
42 .register(byte[].class).register(Integer.class).register(Map.class)
43 .build();
44
45 /**
46 * Creates a new move Treatment.
47 *
48 * @param srcOfs source offset
49 * @param dstOfs destination offset
50 * @param nBits nbits
51 * @param src source
52 * @param dst destination
Phaneendra Mandaefb38752015-12-04 00:43:38 +053053 * @param type extension treatment type
samueljcc4a527ed2015-12-03 13:59:49 +080054 */
55 public DefaultMoveExtensionTreatment(int srcOfs, int dstOfs, int nBits,
56 int src, int dst, ExtensionTreatmentType type) {
57 this.srcOfs = srcOfs;
58 this.dstOfs = dstOfs;
59 this.nBits = nBits;
60 this.src = src;
61 this.dst = dst;
62 this.type = type;
63 }
64
65 @Override
66 public ExtensionTreatmentType type() {
67 return type;
68 }
69
70 @Override
71 public byte[] serialize() {
72 Map<String, Integer> values = Maps.newHashMap();
73 values.put("srcOfs", srcOfs);
74 values.put("dstOfs", dstOfs);
75 values.put("nBits", nBits);
76 values.put("src", src);
77 values.put("dst", dst);
78 values.put("type", ExtensionTreatmentType.ExtensionTreatmentTypes.valueOf(type.toString()).ordinal());
79 return appKryo.serialize(values);
80 }
81
82 @Override
83 public void deserialize(byte[] data) {
84 Map<String, Integer> values = appKryo.deserialize(data);
85 srcOfs = values.get("srcOfs");
86 dstOfs = values.get("dstOfs");
87 nBits = values.get("nBits");
88 src = values.get("src");
89 dst = values.get("dst");
90 type = new ExtensionTreatmentType(values.get("type").intValue());
91 }
92
93 @Override
94 public int srcOffset() {
95 return srcOfs;
96 }
97
98 @Override
99 public int dstOffset() {
100 return dstOfs;
101 }
102
103 @Override
104 public int src() {
105 return src;
106 }
107
108 @Override
109 public int dst() {
110 return dst;
111 }
112
113 @Override
114 public int nBits() {
115 return nBits;
116 }
117
118 @Override
119 public int hashCode() {
120 return Objects.hash(srcOfs, dstOfs, src, dst, nBits);
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 if (this == obj) {
126 return true;
127 }
128 if (obj instanceof DefaultMoveExtensionTreatment) {
129 DefaultMoveExtensionTreatment that = (DefaultMoveExtensionTreatment) obj;
130 return Objects.equals(srcOfs, that.srcOfs)
131 && Objects.equals(dstOfs, that.dstOfs)
132 && Objects.equals(src, that.src)
133 && Objects.equals(dst, that.dst)
134 && Objects.equals(nBits, that.nBits);
135
136 }
137 return false;
138 }
139
140 @Override
141 public String toString() {
142 return MoreObjects.toStringHelper(getClass()).add("srcOfs", srcOfs)
143 .add("dstOfs", dstOfs).add("nBits", nBits).add("src", src)
144 .add("dst", dst).toString();
145 }
146}