samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 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 | package org.onosproject.driver.extensions; |
| 17 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.Maps; |
samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.net.flow.AbstractExtension; |
| 22 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 23 | |
Andrea Campanella | 238d96e | 2016-01-20 11:52:02 -0800 | [diff] [blame] | 24 | import java.util.Map; |
| 25 | import java.util.Objects; |
samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Default implementation of Move treatment. |
| 29 | */ |
| 30 | public 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 Yuta | 881b630 | 2016-05-30 20:07:48 -0700 | [diff] [blame] | 41 | .register(Map.class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 42 | .build("DefaultMoveExtensionTreatment"); |
samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 43 | |
| 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 Manda | efb3875 | 2015-12-04 00:43:38 +0530 | [diff] [blame] | 52 | * @param type extension treatment type |
samueljcc | 4a527ed | 2015-12-03 13:59:49 +0800 | [diff] [blame] | 53 | */ |
| 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 | } |