Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | package org.onosproject.lisp.msg.protocols; |
| 17 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame^] | 18 | import com.google.common.base.Objects; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
| 20 | import com.google.common.collect.Lists; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 21 | import io.netty.buffer.ByteBuf; |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 22 | import org.onlab.util.ImmutableByteSequence; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 23 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame^] | 26 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 27 | |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 28 | /** |
| 29 | * Default LISP map notify message class. |
| 30 | */ |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 31 | public final class DefaultLispMapNotify implements LispMapNotify { |
| 32 | |
| 33 | private final long nonce; |
| 34 | private final short keyId; |
| 35 | private final byte[] authenticationData; |
| 36 | private final byte recordCount; |
| 37 | private final List<LispMapRecord> mapRecords; |
| 38 | |
| 39 | /** |
| 40 | * A private constructor that protects object instantiation from external. |
| 41 | * |
| 42 | * @param nonce nonce |
| 43 | * @param keyId key identifier |
| 44 | * @param authenticationData authentication data |
| 45 | * @param recordCount record count number |
| 46 | * @param mapRecords a collection of map records |
| 47 | */ |
| 48 | private DefaultLispMapNotify(long nonce, short keyId, byte[] authenticationData, |
| 49 | byte recordCount, List<LispMapRecord> mapRecords) { |
| 50 | this.nonce = nonce; |
| 51 | this.keyId = keyId; |
| 52 | this.authenticationData = authenticationData; |
| 53 | this.recordCount = recordCount; |
| 54 | this.mapRecords = mapRecords; |
| 55 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 56 | |
| 57 | @Override |
| 58 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 59 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void writeTo(ByteBuf byteBuf) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 64 | // TODO: serialize LispMapRegister message |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Builder createBuilder() { |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 69 | return new DefaultNotifyBuilder(); |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 70 | } |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 71 | |
| 72 | @Override |
| 73 | public long getNonce() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 74 | return this.nonce; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public byte getRecordCount() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 79 | return this.recordCount; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public short getKeyId() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 84 | return this.keyId; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public byte[] getAuthenticationData() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 89 | return ImmutableByteSequence.copyFrom(this.authenticationData).asArray(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 93 | public List<LispMapRecord> getLispRecords() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 94 | return ImmutableList.copyOf(mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 95 | } |
| 96 | |
Jian Li | 20850d3 | 2016-08-04 02:15:57 +0900 | [diff] [blame^] | 97 | @Override |
| 98 | public String toString() { |
| 99 | return toStringHelper(this) |
| 100 | .add("type", getType()) |
| 101 | .add("nonce", nonce) |
| 102 | .add("recordCount", recordCount) |
| 103 | .add("keyId", keyId) |
| 104 | .add("mapRecords", mapRecords).toString(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean equals(Object o) { |
| 109 | if (this == o) { |
| 110 | return true; |
| 111 | } |
| 112 | if (o == null || getClass() != o.getClass()) { |
| 113 | return false; |
| 114 | } |
| 115 | DefaultLispMapNotify that = (DefaultLispMapNotify) o; |
| 116 | return Objects.equal(nonce, that.nonce) && |
| 117 | Objects.equal(recordCount, that.recordCount) && |
| 118 | Objects.equal(keyId, that.keyId) && |
| 119 | Objects.equal(authenticationData, that.authenticationData); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return Objects.hashCode(nonce, recordCount, keyId, authenticationData); |
| 125 | } |
| 126 | |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 127 | public static final class DefaultNotifyBuilder implements NotifyBuilder { |
| 128 | |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 129 | private long nonce; |
| 130 | private short keyId; |
| 131 | private byte[] authenticationData; |
| 132 | private byte recordCount; |
| 133 | private List<LispMapRecord> mapRecords = Lists.newArrayList(); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 134 | |
| 135 | @Override |
| 136 | public LispType getType() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 137 | return LispType.LISP_MAP_NOTIFY; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public NotifyBuilder withNonce(long nonce) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 142 | this.nonce = nonce; |
| 143 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public NotifyBuilder withRecordCount(byte recordCount) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 148 | this.recordCount = recordCount; |
| 149 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public NotifyBuilder withKeyId(short keyId) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 154 | this.keyId = keyId; |
| 155 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public NotifyBuilder withAuthenticationData(byte[] authenticationData) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 160 | this.authenticationData = authenticationData; |
| 161 | return this; |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | @Override |
Jian Li | 10a0906 | 2016-07-26 23:58:50 +0900 | [diff] [blame] | 165 | public NotifyBuilder addRecord(LispMapRecord record) { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 166 | this.mapRecords.add(record); |
| 167 | return this; |
| 168 | } |
| 169 | |
| 170 | @Override |
Jian Li | 525fded | 2016-08-04 01:15:33 +0900 | [diff] [blame] | 171 | public LispMapNotify build() { |
Jian Li | f59c0ad | 2016-08-02 18:11:30 +0900 | [diff] [blame] | 172 | return new DefaultLispMapNotify(nonce, keyId, authenticationData, |
| 173 | recordCount, mapRecords); |
Jian Li | 719b3bf | 2016-07-22 00:38:29 +0900 | [diff] [blame] | 174 | } |
| 175 | } |
Jian Li | 451175e | 2016-07-19 23:22:20 +0900 | [diff] [blame] | 176 | } |