38acb8c96acc1f9ea5e68d3c52d09f6c38c44b23
[policy/drools-pdp.git] / feature-pooling-dmaap / src / main / java / org / onap / policy / drools / pooling / message / Forward.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.pooling.message;
22
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
25 import org.onap.policy.drools.pooling.PoolingFeatureException;
26
27 /**
28  * Message to forward an event to another host.
29  */
30 public class Forward extends Message {
31
32     /**
33      * Number of hops (i.e., number of times it's been forwarded) so far.
34      */
35     private int numHops;
36
37     /**
38      * Time, in milliseconds, at which the message was created.
39      */
40     private long createTimeMs;
41
42     /**
43      * Protocol of the receiving topic.
44      */
45     private CommInfrastructure protocol;
46
47     /**
48      * Topic on which the event was received.
49      */
50     private String topic;
51
52     /**
53      * The event pay load that was received on the topic.
54      */
55     private String payload;
56
57     /**
58      * The request id that was extracted from the event.
59      */
60     private String requestId;
61
62     /**
63      * Constructor.
64      */
65     public Forward() {
66         super();
67     }
68
69     /**
70      * Constructor.
71      * 
72      * @param source host on which the message originated
73      * @param protocol protocol
74      * @param topic topic
75      * @param payload the actual event data received on the topic
76      * @param requestId request id
77      */
78     public Forward(String source, CommInfrastructure protocol, String topic, String payload, String requestId) {
79         super(source);
80
81         this.numHops = 0;
82         this.createTimeMs = System.currentTimeMillis();
83         this.protocol = protocol;
84         this.topic = topic;
85         this.payload = payload;
86         this.requestId = requestId;
87     }
88
89     /**
90      * Increments {@link #numHops}.
91      */
92     public void bumpNumHops() {
93         ++numHops;
94     }
95
96     public int getNumHops() {
97         return numHops;
98     }
99
100     public void setNumHops(int numHops) {
101         this.numHops = numHops;
102     }
103
104     public long getCreateTimeMs() {
105         return createTimeMs;
106     }
107
108     public void setCreateTimeMs(long createTimeMs) {
109         this.createTimeMs = createTimeMs;
110     }
111
112     public CommInfrastructure getProtocol() {
113         return protocol;
114     }
115
116     public void setProtocol(CommInfrastructure protocol) {
117         this.protocol = protocol;
118     }
119
120     public String getTopic() {
121         return topic;
122     }
123
124     public void setTopic(String topic) {
125         this.topic = topic;
126     }
127
128     public String getPayload() {
129         return payload;
130     }
131
132     public void setPayload(String payload) {
133         this.payload = payload;
134     }
135
136     public String getRequestId() {
137         return requestId;
138     }
139
140     public void setRequestId(String requestId) {
141         this.requestId = requestId;
142     }
143
144     @JsonIgnore
145     public boolean isExpired(long minCreateTimeMs) {
146         return (createTimeMs < minCreateTimeMs);
147
148     }
149
150     @Override
151     @JsonIgnore
152     public void checkValidity() throws PoolingFeatureException {
153
154         super.checkValidity();
155
156         if (protocol == null) {
157             throw new PoolingFeatureException("missing message protocol");
158         }
159
160         if (topic == null || topic.isEmpty()) {
161             throw new PoolingFeatureException("missing message topic");
162         }
163
164         /*
165          * Note: an empty pay load is OK, as an empty message could have been
166          * received on the topic.
167          */
168
169         if (payload == null) {
170             throw new PoolingFeatureException("missing message payload");
171         }
172
173         if (requestId == null || requestId.isEmpty()) {
174             throw new PoolingFeatureException("missing message requestId");
175         }
176
177         if (numHops < 0) {
178             throw new PoolingFeatureException("invalid message hop count");
179         }
180     }
181
182 }