Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / openecomp / aai / champ / event / ChampEvent.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.openecomp.aai.champ.event;
23
24
25 import java.io.IOException;
26
27 import org.openecomp.aai.champ.model.ChampObject;
28 import org.openecomp.aai.champ.model.ChampObjectIndex;
29 import org.openecomp.aai.champ.model.ChampPartition;
30 import org.openecomp.aai.champ.model.ChampRelationship;
31 import org.openecomp.aai.champ.model.ChampRelationshipIndex;
32
33 import com.fasterxml.jackson.annotation.JsonInclude.Include;
34 import com.fasterxml.jackson.core.JsonParseException;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.JsonMappingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38
39
40 public class ChampEvent {
41
42   public enum ChampOperation {
43     STORE,
44     REPLACE,
45     DELETE
46   }
47   
48   private static ObjectMapper mapper = new ObjectMapper();
49   
50   private ChampOperation         operation;
51   private long                   timestamp;
52   private ChampObject            vertex            = null;
53   private ChampRelationship      relationship      = null;
54   private ChampPartition         partition         = null;
55   private ChampObjectIndex       objectIndex       = null;
56   private ChampRelationshipIndex relationshipIndex = null;
57   
58   
59   public static Builder builder() {
60     return new Builder();
61   }
62   
63   public ChampOperation getOperation() {
64     return operation;
65   }
66   
67   public void setOperation(ChampOperation operation) {
68     this.operation = operation;
69   }
70     
71   public long getTimestamp() {
72     return timestamp;
73   }
74   
75   public void setTimestamp(long timestamp) {
76     this.timestamp = timestamp;
77   }
78   
79   public ChampObject getVertex() {
80     return vertex;
81   }
82   
83   public void setVertex(ChampObject vertex) {
84     this.vertex = vertex;
85   }
86   
87   public ChampRelationship getRelationship() {
88     return relationship;
89   }
90   
91   public void setRelationship(ChampRelationship relationship) {
92     this.relationship = relationship;
93   }
94   
95   public ChampPartition getPartition() {
96     return partition;
97   }
98   
99   public void setPartition(ChampPartition partition) {
100     this.partition = partition;
101   }
102   
103   public ChampObjectIndex getObjectIndex() {
104     return objectIndex;
105   }
106   
107   public void setObjectIndex(ChampObjectIndex index) {
108     this.objectIndex = index;
109   }
110   
111   public ChampRelationshipIndex getRelationshipIndex() {
112     return relationshipIndex;
113   }
114   
115   public void setRelationshipIndex(ChampRelationshipIndex relationshipIndex) {
116     this.relationshipIndex = relationshipIndex;
117   }
118   
119   public String toJson() {
120     
121     ObjectMapper mapper = new ObjectMapper();
122     mapper.setSerializationInclusion(Include.NON_NULL);
123     
124     try {
125       return mapper.writeValueAsString(this);
126     } catch (JsonProcessingException e) {
127       return "Unmarshallable: " + e.getMessage();
128     }
129   }
130   
131   public static ChampEvent fromJson(String json) throws JsonParseException, JsonMappingException, IOException {
132
133     mapper.setSerializationInclusion(Include.NON_NULL);
134     return mapper.readValue(json, ChampEvent.class);
135   }
136   @Override
137   public String toString() {
138     return toJson();
139   }
140   
141   public static class Builder {
142     
143     ChampEvent event = null;
144     
145     
146     public Builder() {
147       event = new ChampEvent();
148     }
149     
150     public Builder operation(ChampOperation operation) {
151       event.setOperation(operation);
152       return this;
153     }
154     
155     public Builder entity(ChampObject entity) {
156       event.setVertex(entity);
157       return this;
158     }
159     
160     public Builder entity(ChampRelationship relationship) {
161       event.relationship = relationship;
162       return this;
163     }
164     
165     public Builder entity(ChampPartition partition) {
166       event.partition = partition;
167       return this;
168     }
169     
170     public Builder entity(ChampObjectIndex index) {
171       event.objectIndex = index;
172       return this;
173     }
174     
175     public Builder entity(ChampRelationshipIndex relationshipIndex) {
176       event.relationshipIndex = relationshipIndex;
177       return this;
178     }
179    
180     public ChampEvent build() {
181       
182       event.setTimestamp(System.currentTimeMillis());
183       return event;
184     }
185   }
186 }