Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [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 | |
| 17 | package org.onosproject.driver.extensions; |
| 18 | |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 19 | import java.util.Map; |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 20 | import java.util.Objects; |
| 21 | |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 23 | import org.onosproject.net.NshContextHeader; |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 24 | import org.onosproject.net.flow.AbstractExtension; |
| 25 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
| 27 | |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 28 | import com.google.common.base.MoreObjects; |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 29 | import com.google.common.collect.Maps; |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Nicira set NSH Context header extension instruction. |
| 33 | */ |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 34 | public class NiciraSetNshContextHeader extends AbstractExtension implements |
| 35 | ExtensionTreatment { |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 36 | |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 37 | private NshContextHeader nshCh; |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 38 | private ExtensionTreatmentType type; |
| 39 | |
| 40 | private final KryoNamespace appKryo = new KryoNamespace.Builder().build(); |
| 41 | |
| 42 | /** |
| 43 | * Creates a new set nsh context header instruction. |
| 44 | * |
| 45 | * @param type extension treatment type |
| 46 | */ |
| 47 | NiciraSetNshContextHeader(ExtensionTreatmentType type) { |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 48 | this.nshCh = NshContextHeader.of(0); |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 49 | this.type = type; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates a new set nsh context header instruction. |
| 54 | * |
| 55 | * @param nshCh nsh context header |
| 56 | * @param type extension treatment type |
| 57 | */ |
Jian Li | dab7256 | 2016-04-12 14:10:32 -0700 | [diff] [blame] | 58 | public NiciraSetNshContextHeader(NshContextHeader nshCh, ExtensionTreatmentType type) { |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 59 | this.nshCh = nshCh; |
| 60 | this.type = type; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Gets the nsh context header. |
| 65 | * |
| 66 | * @return nsh context header |
| 67 | */ |
Phaneendra Manda | a272a9f | 2015-12-04 02:42:55 +0530 | [diff] [blame] | 68 | public NshContextHeader nshCh() { |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 69 | return nshCh; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public ExtensionTreatmentType type() { |
| 74 | return type; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void deserialize(byte[] data) { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 79 | Map<String, Object> values = appKryo.deserialize(data); |
| 80 | nshCh = (NshContextHeader) values.get("nshCh"); |
| 81 | type = (ExtensionTreatmentType) values.get("type"); |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public byte[] serialize() { |
Phaneendra Manda | 8db7d09 | 2016-06-04 00:17:24 +0530 | [diff] [blame] | 86 | Map<String, Object> values = Maps.newHashMap(); |
| 87 | values.put("nshCh", nshCh); |
| 88 | values.put("type", type); |
| 89 | return appKryo.serialize(values); |
Phaneendra Manda | ab5a736 | 2015-12-02 01:10:01 +0530 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public int hashCode() { |
| 94 | return Objects.hash(nshCh, type); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public boolean equals(Object obj) { |
| 99 | if (this == obj) { |
| 100 | return true; |
| 101 | } |
| 102 | if (obj instanceof NiciraSetNshContextHeader) { |
| 103 | NiciraSetNshContextHeader that = (NiciraSetNshContextHeader) obj; |
| 104 | return Objects.equals(nshCh, that.nshCh) && Objects.equals(type, that.type); |
| 105 | |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public String toString() { |
| 112 | return MoreObjects.toStringHelper(getClass()) |
| 113 | .add("nshCh", nshCh) |
| 114 | .add("type", type) |
| 115 | .toString(); |
| 116 | } |
| 117 | } |