Jian Li | 672ebda | 2017-02-06 20:21:04 +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 | |
| 18 | import io.netty.buffer.ByteBuf; |
| 19 | import org.onlab.packet.DeserializationException; |
| 20 | import org.onosproject.lisp.msg.exceptions.LispParseError; |
| 21 | import org.onosproject.lisp.msg.exceptions.LispReaderException; |
| 22 | import org.onosproject.lisp.msg.exceptions.LispWriterException; |
| 23 | import org.onosproject.lisp.msg.types.LispAfiAddress; |
| 24 | |
| 25 | import static com.google.common.base.Preconditions.checkNotNull; |
| 26 | |
| 27 | /** |
| 28 | * Default implementation of LispReferral. |
| 29 | */ |
| 30 | public final class DefaultLispReferral extends DefaultLispGenericLocator |
| 31 | implements LispReferral { |
| 32 | |
| 33 | static final DefaultLispReferral.ReferralWriter WRITER; |
| 34 | static { |
| 35 | WRITER = new ReferralWriter(); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * A private constructor that protects object instantiation from external. |
| 40 | * |
| 41 | * @param priority uni-cast priority |
| 42 | * @param weight uni-cast weight |
| 43 | * @param multicastPriority multi-cast priority |
| 44 | * @param multicastWeight multi-cast weight |
| 45 | * @param localLocator local locator flag |
| 46 | * @param rlocProbed RLOC probed flag |
| 47 | * @param routed routed flag |
| 48 | * @param locatorAfi locator AFI |
| 49 | */ |
| 50 | private DefaultLispReferral(byte priority, byte weight, byte multicastPriority, |
| 51 | byte multicastWeight, boolean localLocator, |
| 52 | boolean rlocProbed, boolean routed, |
| 53 | LispAfiAddress locatorAfi) { |
| 54 | super(priority, weight, multicastPriority, multicastWeight, |
| 55 | localLocator, rlocProbed, routed, locatorAfi); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void writeTo(ByteBuf byteBuf) throws LispWriterException { |
| 60 | WRITER.writeTo(byteBuf, this); |
| 61 | } |
| 62 | |
| 63 | public static final class DefaultReferralBuilder |
| 64 | extends AbstractGenericLocatorBuilder<ReferralBuilder> |
| 65 | implements ReferralBuilder { |
| 66 | |
| 67 | @Override |
| 68 | public LispReferral build() { |
| 69 | |
| 70 | checkNotNull(locatorAfi, "Must specify a locator address"); |
| 71 | |
| 72 | return new DefaultLispReferral(priority, weight, multicastPriority, |
| 73 | multicastWeight, localLocator, rlocProbed, routed, locatorAfi); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * A LISP message reader for Referral portion. |
| 79 | */ |
| 80 | public static final class ReferralReader implements LispMessageReader<LispReferral> { |
| 81 | |
| 82 | @Override |
| 83 | public LispReferral readFrom(ByteBuf byteBuf) throws LispParseError, |
| 84 | LispReaderException, |
| 85 | DeserializationException { |
| 86 | LispGenericLocator gl = deserialize(byteBuf); |
| 87 | |
| 88 | return new DefaultReferralBuilder() |
| 89 | .withPriority(gl.getPriority()) |
| 90 | .withWeight(gl.getWeight()) |
| 91 | .withMulticastPriority(gl.getMulticastPriority()) |
| 92 | .withMulticastWeight(gl.getMulticastWeight()) |
| 93 | .withLocalLocator(gl.isLocalLocator()) |
| 94 | .withRouted(gl.isRouted()) |
| 95 | .withLocatorAfi(gl.getLocatorAfi()).build(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * A LISP message writer for Referral portion. |
| 101 | */ |
| 102 | public static final class ReferralWriter implements LispMessageWriter<LispReferral> { |
| 103 | |
| 104 | @Override |
| 105 | public void writeTo(ByteBuf byteBuf, LispReferral message) |
| 106 | throws LispWriterException { |
| 107 | |
| 108 | serialize(byteBuf, message); |
| 109 | } |
| 110 | } |
| 111 | } |