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