aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-service/src/main/java/org/onap/optf/cmso/sostatus/MsoStatusClient.java
blob: e760b854b753103d6868fc3e0512a013206a89e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * Copyright © 2017-2019 AT&T Intellectual Property. Modifications Copyright © 2018 IBM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 *
 * Unless otherwise specified, all documentation contained herein is licensed under the Creative
 * Commons License, Attribution 4.0 Intl. (the "License"); you may not use this documentation except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.optf.cmso.sostatus;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.onap.observations.Mdc;
import org.onap.observations.Observation;
import org.onap.optf.cmso.common.BasicAuthenticatorFilter;
import org.onap.optf.cmso.common.CMSStatusEnum;
import org.onap.optf.cmso.common.LogMessages;
import org.onap.optf.cmso.common.PropertiesManagement;
import org.onap.optf.cmso.filters.CmsoClientFilters;
import org.onap.optf.cmso.model.ChangeManagementSchedule;
import org.onap.optf.cmso.model.dao.ChangeManagementGroupDAO;
import org.onap.optf.cmso.model.dao.ChangeManagementScheduleDAO;
import org.onap.optf.cmso.model.dao.ScheduleDAO;
import org.onap.optf.cmso.service.rs.models.HealthCheckComponent;
import org.onap.optf.cmso.so.bean.MsoOrchestrationQueryResponse;
import org.onap.optf.cmso.so.bean.MsoOrchestrationQueryResponse.MsoStatus;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class MsoStatusClient {
    private static EELFLogger debug = EELFManager.getInstance().getDebugLogger();

    @Autowired
    ChangeManagementScheduleDAO cmScheduleDao;

    @Autowired
    ChangeManagementGroupDAO cmGroupdao;

    @Autowired
    ScheduleDAO scheduleDao;

    @Autowired
    Environment env;

    @Autowired
    PropertiesManagement pm;

    /**
     * Execute.
     *
     * @param id the id
     * @throws JobExecutionException the job execution exception
     */
    public void execute(String id) throws JobExecutionException {
        debug.debug(LogMessages.MSO_STATUS_JOB, "Entered", id.toString());
        try {
            UUID uuid = UUID.fromString(id);
            ChangeManagementSchedule cmSchedule = cmScheduleDao.lockOne(uuid);
            if (cmSchedule == null) {
                Observation.report(LogMessages.MSO_POLLING_MISSING_SCHEDULE, id.toString());
                return;
            }
            poll(cmSchedule);
        } catch (Exception e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
        }
        debug.debug(LogMessages.MSO_STATUS_JOB, "Exited", id.toString());
    }

    /**
     * Poll.
     *
     * @param cmSchedule the cm schedule
     */
    @Transactional
    public void poll(ChangeManagementSchedule cmSchedule) {
        Map<String, String> mdcSave = Mdc.save();
        try {
            // Re-fetch schedule inside transaction??
            String requestId = cmSchedule.getMsoRequestId();
            String url = env.getProperty("so.url");
            String user = env.getProperty("so.user");
            String pass = pm.getProperty("so.pass", "");
            if (!url.endsWith("/")) {
                url = url + "/";
            }
            url = url + requestId;
            Client client = ClientBuilder.newClient();
            client.register(new BasicAuthenticatorFilter(user, pass));
            client.register(new CmsoClientFilters());
            WebTarget target = client.target(url);
            Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
            Response response = null;
            cmSchedule.setMsoTimeMillis(System.currentTimeMillis());
            response = invocationBuilder.get();
            switch (response.getStatus()) {
                case 200: {
                    String respString = response.readEntity(String.class);
                    debug.debug("MSO response {}", respString);
                    MsoOrchestrationQueryResponse resp = getResponseStatus(respString);
                    if (resp != null) {
                        cmSchedule.setMsoStatus(resp.getRequestState());
                        cmSchedule.setMsoMessage(resp.getStatusMessage());
                        MsoStatus msoStatus = MsoStatus.UNKNOWN;
                        try {
                            msoStatus = MsoStatus.valueOf(resp.getRequestState());
                        } catch (Exception e) {
                            Observation.report(LogMessages.UNRECOGNIZED_MSO_STATUS, resp.getRequestState());
                        }
                        long finishTime = getFinishTime(resp);
                        switch (msoStatus) {
                            case COMPLETE:
                                cmSchedule.setExecutionCompletedTimeMillis(finishTime);
                                cmSchedule.setStatus(CMSStatusEnum.Completed.toString());
                                break;
                            case FAILED:
                                cmSchedule.setExecutionCompletedTimeMillis(finishTime);
                                cmSchedule.setStatus(CMSStatusEnum.Failed.toString());
                                break;
                            case UNKNOWN:
                            default:
                        }
                    } else {
                        // Do not keep polling...
                        cmSchedule.setStatus(CMSStatusEnum.Error.toString());
                        cmSchedule.setMsoStatus("Bad Response");
                        cmSchedule.setMsoMessage("Unable to parse :" + respString);

                    }

                }
                    break;
                case 404: { // Not found
                    // Do not keep polling...
                    cmSchedule.setStatus(CMSStatusEnum.Failed.toString());
                    cmSchedule.setMsoStatus("Not found");
                    cmSchedule.setMsoMessage("Call to MSO Failed :" + response.toString());
                }
                    break;
                case 400: { // Bad request
                    // Do not keep polling...
                    cmSchedule.setStatus(CMSStatusEnum.Error.toString());
                    cmSchedule.setMsoStatus("Bad Request");
                    cmSchedule.setMsoMessage("Call to MSO Failed :" + response.toString());
                }
                    break;
                case 500:
                default: {
                    cmSchedule.setStatus(CMSStatusEnum.Error.toString());
                    cmSchedule.setMsoStatus("Failed");
                    cmSchedule.setMsoMessage("Call to MSO Failed :" + response.toString());
                }
            }
        } catch (ProcessingException e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
            // Probably a transient error... Keep polling
            cmSchedule.setMsoTimeMillis(System.currentTimeMillis());
            cmSchedule.setMsoStatus("ConnectionException");
            cmSchedule.setMsoMessage("Could not call MSO:" + e.getMessage());
        } catch (Exception e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
            // Probably a transient error... Keep polling
            cmSchedule.setMsoTimeMillis(System.currentTimeMillis());
            cmSchedule.setMsoStatus("Exception");
            cmSchedule.setMsoMessage("Could not call MSO:" + e.getMessage());
        } finally {
            Mdc.restore(mdcSave);
        }
        // Propagate final MSO status to top level
        cmScheduleDao.save(cmSchedule);
        propagateStatus(cmSchedule);

    }

    private long getFinishTime(MsoOrchestrationQueryResponse resp) {
        // Just in case we cannot parse the time returned by MSO as a UTC time.
        long finishTime = System.currentTimeMillis();
        String timestr = resp.getFinishTime();
        if (timestr != null) {
            try {
                Date dateTime = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z").parse(timestr);
                finishTime = dateTime.getTime();
            } catch (Exception e) {
                Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e,
                                "Unable to parse MSO finish timestamp: " + timestr);
            }
        }
        return finishTime;
    }

    private void propagateStatus(ChangeManagementSchedule cmSchedule) {
        // TODO Auto-generated method stub

    }

    private MsoOrchestrationQueryResponse getResponseStatus(String resp) {
        try {
            ObjectMapper om = new ObjectMapper();
            ObjectNode json = (ObjectNode) om.readTree(resp);
            ObjectNode request = (ObjectNode) json.get("request");
            ObjectNode requestStatus = (ObjectNode) request.get("requestStatus");
            MsoOrchestrationQueryResponse msoResponse =
                            om.treeToValue(requestStatus, MsoOrchestrationQueryResponse.class);
            return msoResponse;
        } catch (Exception e) {
            Observation.report(LogMessages.UNABLE_TO_PARSE_MSO_RESPONSE, e, e.getMessage(), resp);
        }
        return null;
    }

    /**
     * Health check.
     *
     * @return the health check component
     */
    public HealthCheckComponent healthCheck() {
        Map<String, String> mdcSave = Mdc.save();
        String requestId = "healthCheck";
        String url = env.getProperty("so.url", "");
        if (!url.endsWith("/")) {
            url = url + "/";
        }
        url = url + "healthcheck";

        HealthCheckComponent hcc = new HealthCheckComponent();
        hcc.setName("MSO Interface");
        hcc.setUrl(url);

        Client client = ClientBuilder.newClient();
        String user = env.getProperty("so.user", "");
        String pass = pm.getProperty("so.pass", "");

        client.register(new BasicAuthenticatorFilter(user, pass));
        client.register(new CmsoClientFilters());

        WebTarget target = client.target(url);
        Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
        Response response = null;
        try {
            response = invocationBuilder.get();
            switch (response.getStatus()) {
                case 200:
                case 204:
                    hcc.setHealthy(true);
                    hcc.setStatus("OK");
                    break;
                default: {
                    String respString = response.readEntity(String.class);
                    hcc.setStatus(respString);
                }
            }
        } catch (Exception e) {
            hcc.setStatus(e.getMessage());
        } finally {
            Mdc.restore(mdcSave);
        }
        return hcc;
    }
}