added authentication to map notify and map reply, and moved the serialization process...
[lispflowmapping.git] / mappingservice / api / src / main / java / org / opendaylight / lispflowmapping / type / lisp / MapNotify.java
1 /*
2  * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.lispflowmapping.type.lisp;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 /**
16  * <pre>
17  *         0                   1                   2                   3
18  *         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
19  *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20  *        |Type=4 |              Reserved                 | Record Count  |
21  *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22  *        |                         Nonce . . .                           |
23  *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24  *        |                         . . . Nonce                           |
25  *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26  *        |            Key ID             |  Authentication Data Length   |
27  *        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28  *        ~                     Authentication Data                       ~
29  *    +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
30  *    |   |                          Record TTL                           |
31  *    |   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  *    R   | Locator Count | EID mask-len  | ACT |A|      Reserved         |
33  *    e   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  *    c   | Rsvd  |  Map-Version Number   |         EID-Prefix-AFI        |
35  *    o   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  *    r   |                          EID-Prefix                           |
37  *    d   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  *    |  /|    Priority   |    Weight     |  M Priority   |   M Weight    |
39  *    | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40  *    | o |        Unused Flags     |L|p|R|           Loc-AFI             |
41  *    | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42  *    |  \|                             Locator                           |
43  *    +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44  * </pre>
45  * 
46  * @author gmainzer
47  * 
48  */
49 public class MapNotify {
50
51     /**
52      * P: This is the proxy Map-Reply bit. When set to 1, an ETR sends a
53      * Map-Register message requesting the Map-Server to proxy a Map-Reply. The
54      * Map-Server will send non-authoritative Map-Replies on behalf of the ETR.
55      * Details on this usage can be found in [RFC6833].
56      */
57     private boolean proxyMapReply;
58     /**
59      * M: This is the want-map-notify bit. When set to 1, an ETR is requesting a
60      * Map-Notify message to be returned in response to sending a Map-Register
61      * message. The Map-Notify message sent by a Map-Server is used to
62      * acknowledge receipt of a Map-Register message.
63      */
64     private boolean wantMapNotify;
65
66     /**
67      * Nonce: This 8-octet 'Nonce' field is set to 0 in Map-Register messages.
68      * Since the Map-Register message is authenticated, the 'Nonce' field is not
69      * currently used for any security function but may be in the future as part
70      * of an anti-replay solution.
71      */
72     private long nonce;
73
74     /**
75      * Key ID: This is a configured ID to find the configured Message
76      * Authentication Code (MAC) algorithm and key value used for the
77      * authentication function. See Section 14.4 for codepoint assignments.
78      */
79     private short keyId;
80
81     /**
82      * Authentication Data Length: This is the length in octets of the
83      * 'Authentication Data' field that follows this field. The length of the
84      * 'Authentication Data' field is dependent on the MAC algorithm used. The
85      * length field allows a device that doesn't know the MAC algorithm to
86      * correctly parse the packet. private short authenticationLength;
87      */
88
89     /**
90      * Authentication Data: This is the message digest used from the output of
91      * the MAC algorithm. The entire Map-Register payload is authenticated with
92      * this field preset to 0. After the MAC is computed, it is placed in this
93      * field. Implementations of this specification MUST include support for
94      * HMAC-SHA-1-96 [RFC2404], and support for HMAC-SHA-256-128 [RFC4868] is
95      * RECOMMENDED.
96      */
97     private byte[] authenticationData;
98     /**
99      * Record Count: This is the number of records in this Map-Register message.
100      * A record is comprised of that portion of the packet labeled 'Record'
101      * above and occurs the number of times equal to Record Count.
102      * 
103      * private byte recordCount;
104      */
105     private List<EidToLocatorRecord> eidToLocatorRecords;
106
107     public MapNotify() {
108         eidToLocatorRecords = new ArrayList<EidToLocatorRecord>();
109         setAuthenticationData(null);
110     }
111
112     public boolean isProxyMapReply() {
113         return proxyMapReply;
114     }
115
116     public MapNotify setProxyMapReply(boolean proxyMapReply) {
117         this.proxyMapReply = proxyMapReply;
118         return this;
119     }
120
121     public boolean isWantMapNotify() {
122         return wantMapNotify;
123     }
124
125     public MapNotify setWantMapNotify(boolean wantMapNotify) {
126         this.wantMapNotify = wantMapNotify;
127         return this;
128     }
129
130     public long getNonce() {
131         return nonce;
132     }
133
134     public MapNotify setNonce(long nonce) {
135         this.nonce = nonce;
136         return this;
137     }
138
139     public short getKeyId() {
140         return keyId;
141     }
142
143     public MapNotify setKeyId(short keyId) {
144         this.keyId = keyId;
145         return this;
146     }
147
148     public byte[] getAuthenticationData() {
149         return authenticationData;
150     }
151
152     public MapNotify setAuthenticationData(byte[] authenticationData) {
153         this.authenticationData = (authenticationData != null) ? authenticationData : NO_AUTHENTICATION_DATA;
154         return this;
155     }
156
157     public List<EidToLocatorRecord> getEidToLocatorRecords() {
158         return eidToLocatorRecords;
159     }
160
161     public void addEidToLocator(EidToLocatorRecord record) {
162         eidToLocatorRecords.add(record);
163     }
164
165     
166
167     public void setFromMapRegister(MapRegister mapRegister) {
168         setNonce(mapRegister.getNonce());
169         setKeyId(mapRegister.getKeyId());
170         byte[] authenticationData = mapRegister.getAuthenticationData();
171         if (authenticationData != null) {
172             authenticationData = authenticationData.clone();
173             Arrays.fill(authenticationData, (byte)0);
174         }
175         setAuthenticationData(authenticationData);
176
177         for (EidToLocatorRecord eidToLocator : mapRegister.getEidToLocatorRecords()) {
178             addEidToLocator(eidToLocator.clone());
179         }
180     }
181
182     private static byte[] NO_AUTHENTICATION_DATA = new byte[] {};
183 }