Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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 | */ |
| 16 | |
| 17 | package org.onosproject.drivers.lisp.extensions; |
| 18 | |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 20 | import org.onosproject.mapping.addresses.ExtensionMappingAddress; |
| 21 | import org.onosproject.mapping.addresses.ExtensionMappingAddressType; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 22 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 23 | import org.onosproject.net.flow.AbstractExtension; |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 24 | |
| 25 | import java.util.Map; |
| 26 | import java.util.Objects; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 29 | import static org.onosproject.mapping.addresses.ExtensionMappingAddressType |
| 30 | .ExtensionMappingAddressTypes.AS_ADDRESS; |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Implementation of LISP Autonomous System (AS) address. |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 34 | * When an AS number is stored in the LISP Mapping Database System for either |
| 35 | * policy or documentation reasons, it can be encoded in a LISP Canonical Address. |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 36 | */ |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 37 | public final class LispAsAddress extends AbstractExtension |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 38 | implements ExtensionMappingAddress { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 39 | |
| 40 | private static final String AS_NUMBER = "asNumber"; |
| 41 | private static final String ADDRESS = "address"; |
| 42 | |
| 43 | private int asNumber; |
| 44 | private MappingAddress address; |
| 45 | |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 46 | /** |
| 47 | * Default constructor. |
| 48 | */ |
| 49 | public LispAsAddress() { |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Creates an instance with initialized parameters. |
| 54 | * |
| 55 | * @param asNumber AS number |
| 56 | */ |
| 57 | private LispAsAddress(int asNumber, MappingAddress address) { |
| 58 | this.asNumber = asNumber; |
| 59 | this.address = address; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Obtains AS number. |
| 64 | * |
| 65 | * @return AS number |
| 66 | */ |
| 67 | public int getAsNumber() { |
| 68 | return asNumber; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Obtains mapping address. |
| 73 | * |
| 74 | * @return mapping address |
| 75 | */ |
| 76 | public MappingAddress getAddress() { |
| 77 | return address; |
| 78 | } |
| 79 | |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 80 | @Override |
| 81 | public ExtensionMappingAddressType type() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 82 | return AS_ADDRESS.type(); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public byte[] serialize() { |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 87 | Map<String, Object> parameterMap = Maps.newHashMap(); |
| 88 | |
| 89 | parameterMap.put(AS_NUMBER, asNumber); |
| 90 | parameterMap.put(ADDRESS, address); |
Jian Li | 924995b | 2017-03-18 12:34:46 +0900 | [diff] [blame] | 91 | return APP_KRYO.serialize(parameterMap); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void deserialize(byte[] data) { |
Jian Li | 924995b | 2017-03-18 12:34:46 +0900 | [diff] [blame] | 96 | Map<String, Object> parameterMap = APP_KRYO.deserialize(data); |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 97 | |
Jian Li | 299bc1d | 2017-03-17 19:17:40 +0900 | [diff] [blame] | 98 | this.asNumber = (int) parameterMap.get(AS_NUMBER); |
| 99 | this.address = (MappingAddress) parameterMap.get(ADDRESS); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public int hashCode() { |
| 104 | return Objects.hash(asNumber, address); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object obj) { |
| 109 | if (this == obj) { |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | if (obj instanceof LispAsAddress) { |
| 114 | final LispAsAddress other = (LispAsAddress) obj; |
| 115 | return Objects.equals(asNumber, other.asNumber) && |
| 116 | Objects.equals(address, other.address); |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public String toString() { |
| 124 | return toStringHelper(this) |
| 125 | .add("AS number", asNumber) |
| 126 | .add("address", address) |
| 127 | .toString(); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * A builder for building LispAsAddress. |
| 132 | */ |
| 133 | public static final class Builder { |
| 134 | private int asNumber; |
| 135 | private MappingAddress address; |
| 136 | |
| 137 | /** |
| 138 | * Sets AS number. |
| 139 | * |
| 140 | * @param asNumber AS number |
| 141 | * @return Builder object |
| 142 | */ |
| 143 | public Builder withAsNumber(int asNumber) { |
| 144 | this.asNumber = asNumber; |
| 145 | return this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Sets mapping address. |
| 150 | * |
| 151 | * @param address mapping address |
| 152 | * @return Builder object |
| 153 | */ |
| 154 | public Builder withAddress(MappingAddress address) { |
| 155 | this.address = address; |
| 156 | return this; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Builds LispAsAddress instance. |
| 161 | * |
| 162 | * @return LispAsAddress instance |
| 163 | */ |
| 164 | public LispAsAddress build() { |
| 165 | |
| 166 | return new LispAsAddress(asNumber, address); |
| 167 | } |
Jian Li | 3ff327a | 2017-03-14 17:02:19 +0900 | [diff] [blame] | 168 | } |
| 169 | } |