BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +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 | |
| 17 | package org.onosproject.driver.extensions; |
| 18 | |
| 19 | import com.google.common.base.MoreObjects; |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
| 21 | |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 22 | import org.onlab.util.KryoNamespace; |
| 23 | import org.onosproject.net.PortNumber; |
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; |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 26 | import org.onosproject.net.flow.instructions.ExtensionTreatmentType; |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 27 | import org.onosproject.store.serializers.PortNumberSerializer; |
| 28 | |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 29 | import java.util.HashMap; |
| 30 | import java.util.Map; |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 31 | import java.util.Objects; |
| 32 | |
| 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 34 | |
| 35 | /** |
| 36 | * Nicira resubmit extension instruction. |
| 37 | */ |
Jonathan Hart | 26a8d95 | 2015-12-02 15:16:35 -0800 | [diff] [blame] | 38 | public class NiciraResubmit extends AbstractExtension implements ExtensionTreatment { |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 39 | |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 40 | private static final short DEFAULT_TABLE_ID = 0; |
| 41 | |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 42 | private PortNumber inPort; |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 43 | private short table; |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 44 | |
| 45 | private final KryoNamespace appKryo = new KryoNamespace.Builder() |
| 46 | .register(new PortNumberSerializer(), PortNumber.class) |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 47 | .register(Map.class).register(HashMap.class) |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 48 | .register(byte[].class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 49 | .build("NiciraResubmit"); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Creates a new resubmit instruction. |
| 53 | */ |
| 54 | NiciraResubmit() { |
| 55 | inPort = null; |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 56 | table = DEFAULT_TABLE_ID; |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Creates a new resubmit instruction with a particular inPort. |
| 61 | * |
| 62 | * @param inPort in port number |
| 63 | */ |
| 64 | public NiciraResubmit(PortNumber inPort) { |
| 65 | checkNotNull(inPort); |
| 66 | this.inPort = inPort; |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 67 | this.table = DEFAULT_TABLE_ID; |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the inPort. |
| 72 | * |
| 73 | * @return inPort |
| 74 | */ |
| 75 | public PortNumber inPort() { |
| 76 | return inPort; |
| 77 | } |
| 78 | |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 79 | /** |
| 80 | * Gets the table. |
| 81 | * |
| 82 | * @return table |
| 83 | */ |
| 84 | public short table() { |
| 85 | return table; |
| 86 | } |
| 87 | |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 88 | @Override |
alshabib | 880b644 | 2015-11-23 22:13:04 -0800 | [diff] [blame] | 89 | public ExtensionTreatmentType type() { |
| 90 | return ExtensionTreatmentType.ExtensionTreatmentTypes.NICIRA_RESUBMIT.type(); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void deserialize(byte[] data) { |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 95 | Map<String, Object> values = appKryo.deserialize(data); |
| 96 | inPort = (PortNumber) values.get("inPort"); |
| 97 | table = (short) values.get("table"); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public byte[] serialize() { |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 102 | Map<String, Object> values = Maps.newHashMap(); |
| 103 | values.put("inPort", inPort); |
| 104 | values.put("table", table); |
| 105 | return appKryo.serialize(values); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public int hashCode() { |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 110 | return Objects.hash(inPort, table); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public boolean equals(Object obj) { |
| 115 | if (this == obj) { |
| 116 | return true; |
| 117 | } |
| 118 | if (obj instanceof NiciraResubmit) { |
| 119 | NiciraResubmit that = (NiciraResubmit) obj; |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 120 | return Objects.equals(inPort, that.inPort) |
| 121 | && (table == that.table()); |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 122 | } |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public String toString() { |
| 128 | return MoreObjects.toStringHelper(getClass()) |
| 129 | .add("inPort", inPort) |
BitOhenry | 5e2233a | 2015-12-08 09:26:38 +0800 | [diff] [blame] | 130 | .add("table", table) |
BitOhenry | b53ac6c | 2015-11-16 20:58:58 +0800 | [diff] [blame] | 131 | .toString(); |
| 132 | } |
| 133 | } |