ebdbc0d2f4659bbb19395545793d839cc77be952
[lispflowmapping.git] / mappingservice / lisp-proto / src / main / java / org / opendaylight / lispflowmapping / lisp / type / MappingData.java
1 /*
2  * Copyright (c) 2016, 2017 Cisco Systems, 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 package org.opendaylight.lispflowmapping.lisp.type;
9
10 import java.util.Date;
11 import java.util.Optional;
12 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
13 import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
14 import org.opendaylight.lispflowmapping.lisp.util.Stringifier;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
17
18 /**
19  * Wrapper class for MappingRecord with timestamp added.
20  *
21  * @author Lorand Jakab
22  *
23  */
24 public class MappingData {
25     private MappingRecord record = null;
26     private Date timestamp = null;
27     private XtrId xtrId = null;
28     private boolean mergeEnabled = false;
29
30     public MappingData(MappingRecord record, Long timestamp) {
31         this(record, new Date(timestamp));
32     }
33
34     public MappingData(MappingRecord record, Date timestamp) {
35         setRecord(record);
36         setTimestamp(timestamp);
37     }
38
39     public MappingData(MappingRecord record) {
40         setRecord(record);
41     }
42
43     public synchronized MappingRecord getRecord() {
44         return record;
45     }
46
47     public synchronized void setRecord(MappingRecord record) {
48         this.record = record;
49     }
50
51     public synchronized Date getTimestamp() {
52         return timestamp;
53     }
54
55     public synchronized void setTimestamp(Date timestamp) {
56         this.timestamp = timestamp;
57     }
58
59     public synchronized XtrId getXtrId() {
60         return xtrId;
61     }
62
63     public synchronized void setXtrId(XtrId xtrId) {
64         this.xtrId = xtrId;
65     }
66
67     public synchronized boolean isMergeEnabled() {
68         return mergeEnabled;
69     }
70
71     public synchronized void setMergeEnabled(boolean mergeEnabled) {
72         this.mergeEnabled = mergeEnabled;
73     }
74
75     public Optional<Boolean> isNegative() {
76         if (record != null) {
77             return Optional.of(MappingRecordUtil.isNegativeMapping(record));
78         } else {
79             return Optional.empty();
80         }
81     }
82
83     public Optional<Boolean> isPositive() {
84         if (record != null) {
85             return Optional.of(MappingRecordUtil.isPositiveMapping(record));
86         } else {
87             return Optional.empty();
88         }
89     }
90
91     @Override
92     public String toString() {
93         StringBuilder sb = new StringBuilder("MappingData [");
94
95         sb.append("merge=");
96         sb.append(mergeEnabled);
97
98         if (xtrId != null) {
99             sb.append(", xTR-ID=");
100             sb.append(xtrId);
101         }
102
103         if (timestamp != null) {
104             sb.append(", timestamp=");
105             sb.append(timestamp);
106         }
107
108         if (record != null) {
109             sb.append(", record=");
110             sb.append(record);
111         }
112
113         return sb.append(']').toString();
114     }
115
116     public String getString() {
117         StringBuilder sb = new StringBuilder();
118
119         sb.append("Merge: ");
120         sb.append(mergeEnabled ? "on" : "off");
121
122         if (xtrId != null) {
123             sb.append(", xTR-ID: ");
124             sb.append(LispAddressStringifier.getString(xtrId));
125         }
126
127         if (timestamp != null) {
128             sb.append(", timestamp: ");
129             sb.append(timestamp.toString());
130         }
131
132         if (record != null) {
133             sb.append("\n");
134             sb.append(Stringifier.getString(record));
135             sb.append("\n");
136         }
137
138         return sb.append(']').toString();
139     }
140 }