Jian Li | de6eb18 | 2019-02-25 21:33:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 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 | |
| 18 | import com.google.common.base.MoreObjects; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import org.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.net.flow.AbstractExtension; |
| 22 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 23 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 24 | |
| 25 | import java.util.HashMap; |
| 26 | import java.util.Map; |
| 27 | import java.util.Objects; |
| 28 | |
| 29 | /** |
| 30 | * Nicira load extension instruction. |
| 31 | */ |
| 32 | public class NiciraLoad extends AbstractExtension implements ExtensionTreatment { |
| 33 | private int ofsNbits; |
| 34 | private long dst; |
| 35 | private long value; |
| 36 | |
| 37 | private final KryoNamespace appKryo = new KryoNamespace.Builder() |
| 38 | .register(Map.class) |
| 39 | .register(HashMap.class) |
| 40 | .build(); |
| 41 | |
| 42 | /** |
| 43 | * Empty constructor. |
| 44 | */ |
| 45 | public NiciraLoad() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a new load treatment. |
| 50 | * |
| 51 | * @param ofsNbits off set and nBits |
| 52 | * @param dst destination |
| 53 | * @param value value |
| 54 | */ |
| 55 | public NiciraLoad(int ofsNbits, long dst, long value) { |
| 56 | this.ofsNbits = ofsNbits; |
| 57 | this.dst = dst; |
| 58 | this.value = value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets load nBits. |
| 63 | * |
| 64 | * @return nBits |
| 65 | */ |
| 66 | public int ofsNbits() { |
| 67 | return ofsNbits; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets load destination. |
| 72 | * |
| 73 | * @return load destination |
| 74 | */ |
| 75 | public long dst() { |
| 76 | return dst; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets load value. |
| 81 | * |
| 82 | * @return load value |
| 83 | */ |
| 84 | public long value() { |
| 85 | return value; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public ExtensionTreatmentType type() { |
| 90 | return ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_LOAD.type(); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public byte[] serialize() { |
| 95 | Map<String, Object> values = Maps.newHashMap(); |
| 96 | values.put("ofsNbits", ofsNbits); |
| 97 | values.put("dst", dst); |
| 98 | values.put("value", value); |
| 99 | return appKryo.serialize(values); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void deserialize(byte[] data) { |
| 104 | Map<String, Object> values = appKryo.deserialize(data); |
| 105 | ofsNbits = (int) values.get("ofsNbits"); |
| 106 | dst = (long) values.get("dst"); |
| 107 | value = (long) values.get("value"); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public boolean equals(Object o) { |
| 112 | if (this == o) { |
| 113 | return true; |
| 114 | } |
| 115 | if (o == null || getClass() != o.getClass()) { |
| 116 | return false; |
| 117 | } |
| 118 | NiciraLoad that = (NiciraLoad) o; |
| 119 | return ofsNbits == that.ofsNbits && |
| 120 | dst == that.dst && |
| 121 | value == that.value && |
| 122 | Objects.equals(this.type(), that.type()); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public int hashCode() { |
| 127 | return Objects.hash(ofsNbits, dst, value); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public String toString() { |
| 132 | return MoreObjects.toStringHelper(this) |
| 133 | .add("ofsNbits", ofsNbits) |
| 134 | .add("dst", dst) |
| 135 | .add("value", value) |
| 136 | .toString(); |
| 137 | } |
| 138 | } |