215cdaecc4c4bee32444781bc9d1737d18c7f0de
[policy/drools-pdp.git] / feature-pooling-dmaap / src / main / java / org / onap / policy / drools / pooling / message / Message.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 com.fasterxml.jackson.annotation.JsonSubTypes;
25 import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
26 import com.fasterxml.jackson.annotation.JsonTypeInfo;
27 import org.onap.policy.drools.pooling.PoolingFeatureException;
28
29 /**
30  * Messages sent on the internal topic.
31  */
32 @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
33 @JsonSubTypes({@Type(value = Forward.class, name = "forward"), @Type(value = Heartbeat.class, name = "heartbeat"),
34                 @Type(value = Identification.class, name = "identification"),
35                 @Type(value = Leader.class, name = "leader"), @Type(value = Offline.class, name = "offline"),
36                 @Type(value = Query.class, name = "query")})
37 public class Message {
38
39     /**
40      * Name of the administrative channel.
41      */
42     public static final String ADMIN = "_admin";
43
44     /**
45      * Host that originated the message.
46      */
47     private String source;
48
49     /**
50      * Channel on which the message is routed, which is either the target host
51      * or {@link #ADMIN}.
52      */
53     private String channel;
54
55     /**
56      * Constructor.
57      */
58     public Message() {
59         super();
60     }
61
62     /**
63      * Constructor.
64      * 
65      * @param source host on which the message originated
66      */
67     public Message(String source) {
68         this.source = source;
69     }
70
71     public String getSource() {
72         return source;
73     }
74
75     public void setSource(String source) {
76         this.source = source;
77     }
78
79     public String getChannel() {
80         return channel;
81     }
82
83     public void setChannel(String channel) {
84         this.channel = channel;
85     }
86
87     /**
88      * Checks the validity of the message, including verifying that required
89      * fields are not missing.
90      * 
91      * @throws PoolingFeatureException if the message is invalid
92      */
93     @JsonIgnore
94     public void checkValidity() throws PoolingFeatureException {
95         if (source == null || source.isEmpty()) {
96             throw new PoolingFeatureException("missing message source");
97         }
98
99         if (channel == null || channel.isEmpty()) {
100             throw new PoolingFeatureException("missing message channel");
101         }
102     }
103
104 }