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