Fix nullpointer issue in pdpx heartbeat
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / comm / XacmlPdpMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 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  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdpx.main.comm;
24
25 import lombok.Getter;
26 import org.onap.policy.common.utils.network.NetworkUtil;
27 import org.onap.policy.models.pdp.concepts.PdpStateChange;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
31 import org.onap.policy.models.pdp.enums.PdpState;
32 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
33 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 @Getter
38 public class XacmlPdpMessage {
39
40     // The logger for this class
41     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpMessage.class);
42     private String pdpGroup;
43     private String pdpSubGroup;
44     private PdpState pdpState;
45     private String pdpName = NetworkUtil.getHostname();
46
47     /**
48      * Method used to format the initial registration status message.
49      *
50      * @param state of the PDP
51      * @return status message of the PDP
52      */
53     public PdpStatus formatInitialStatusMessage(PdpState state) {
54         PdpStatus status = new PdpStatus();
55         status.setName(pdpName);
56
57         if (XacmlPdpActivator.getCurrent().isAlive()) {
58             status.setHealthy(PdpHealthStatus.HEALTHY);
59         } else {
60             status.setHealthy(PdpHealthStatus.NOT_HEALTHY);
61         }
62
63         status.setPdpType("xacml");
64         status.setState(state);
65         status.setSupportedPolicyTypes(XacmlPdpApplicationManager.getToscaPolicyTypeIdents());
66
67         LOGGER.debug("formatStatusMessage state {} status{}", state, status);
68
69         return status;
70
71     }
72
73     /**
74      * Method used to format the PdpStatus message for heartbeat and PDP Updates.
75      *
76      * @return status message of the PDP
77      */
78     public PdpStatus formatPdpStatusMessage() {
79         PdpStatus status = new PdpStatus();
80         status.setName(pdpName);
81
82         if (XacmlPdpActivator.getCurrent() != null && XacmlPdpActivator.getCurrent().isAlive()) {
83             status.setHealthy(PdpHealthStatus.HEALTHY);
84         } else {
85             status.setHealthy(PdpHealthStatus.NOT_HEALTHY);
86         }
87
88         status.setPdpType("xacml");
89         status.setState(pdpState);
90         status.setPdpGroup(pdpGroup);
91         status.setPdpSubgroup(pdpSubGroup);
92         status.setSupportedPolicyTypes(XacmlPdpApplicationManager.getToscaPolicyTypeIdents());
93         status.setPolicies(XacmlPdpApplicationManager.getToscaPolicyIdentifiers());
94
95         return status;
96     }
97
98     /**
99      * Method used to update PDP status attributes from PdpStateChange.
100      */
101     public void updateInternalStatus(PdpStateChange message) {
102         pdpGroup = message.getPdpGroup();
103         pdpSubGroup = message.getPdpSubgroup();
104         pdpState = message.getState();
105     }
106
107     /**
108      * Method used to update PDP status attributes from PdpUpdate.
109      */
110     public void updateInternalStatus(PdpUpdate message) {
111         pdpGroup = message.getPdpGroup();
112         pdpSubGroup = message.getPdpSubgroup();
113     }
114 }