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