aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-service/src/main/java/org/onap/optf/cmso/ticketmgt/TmClient.java
blob: 1864da75bb3e1621c7dc0e86e8495fef3057ca57 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
 * Copyright © 2017-2018 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.ticketmgt;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
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.Entity;
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 javax.ws.rs.core.Response.Status;
import org.apache.commons.text.StringSubstitutor;
import org.joda.time.format.ISODateTimeFormat;
import org.onap.observations.Mdc;
import org.onap.observations.Observation;
import org.onap.optf.cmso.common.BasicAuthenticatorFilter;
import org.onap.optf.cmso.common.CmHelpers;
import org.onap.optf.cmso.common.LogMessages;
import org.onap.optf.cmso.common.PropertiesManagement;
import org.onap.optf.cmso.common.exceptions.CmsoException;
import org.onap.optf.cmso.filters.CmsoClientFilters;
import org.onap.optf.cmso.model.ChangeManagementGroup;
import org.onap.optf.cmso.model.ChangeManagementSchedule;
import org.onap.optf.cmso.model.DomainData;
import org.onap.optf.cmso.model.Schedule;
import org.onap.optf.cmso.model.dao.ChangeManagementScheduleDao;
import org.onap.optf.cmso.service.rs.models.CmDomainDataEnum;
import org.onap.optf.cmso.service.rs.models.HealthCheckComponent;
import org.onap.optf.cmso.ticketmgt.TmEndpoints.Endpoint;
import org.onap.optf.cmso.ticketmgt.TmStatusClient.ClosureCode;
import org.onap.optf.cmso.ticketmgt.bean.BuildCreateRequest;
import org.onap.optf.cmso.ticketmgt.bean.BuildCreateRequest.Variables;
import org.onap.optf.cmso.ticketmgt.bean.TmAsset;
import org.onap.optf.cmso.ticketmgt.bean.TmChangeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * The Class TmClient.
 */
@Component
public class TmClient {
    private static EELFLogger debug = EELFManager.getInstance().getDebugLogger();

    @Autowired
    Environment env;

    @Autowired
    PropertiesManagement pm;

    @Autowired
    ChangeManagementScheduleDao cmScheduleDao;

    @Autowired
    BuildCreateRequest buildCreateRequest;

    @Autowired
    TmEndpoints tmEndpoints;

    /**
     * Creates the change ticket.
     *
     * @param schedule the schedule
     * @param group the group
     * @param vnfNames the vnf names
     * @param domainData the domain data
     * @return the string
     * @throws CmsoException the CMS exception
     */
    public String createChangeTicket(Schedule schedule, ChangeManagementGroup group, List<String> vnfNames,
                    List<DomainData> domainData) throws CmsoException {

        String changeId = "";
        String workflowName = CmHelpers.getDomainData(domainData, CmDomainDataEnum.WorkflowName);
        Map<String, Object> variables = getVariables(schedule, group, vnfNames, domainData);
        List<TmAsset> assetList = getAssetList(vnfNames);
        JsonNode createChangeRecord = buildCreateRequest.createChangeRecordRequest(variables, assetList, workflowName);
        debug.debug("createChangeRecord=" + createChangeRecord.toString());
        changeId = postCreateChangeTicket(createChangeRecord, schedule.getScheduleId());
        return changeId;
    }

    /**
     * Close ticket.
     *
     * @param schedule the schedule
     * @param group the group
     * @param cmSchedules the cm schedules
     * @param changeId the change id
     * @param closureCode the closure code
     * @param closingComments the closing comments
     * @throws CmsoException the CMS exception
     */
    public void closeTicket(Schedule schedule, ChangeManagementGroup group, List<ChangeManagementSchedule> cmSchedules,
                    String changeId, ClosureCode closureCode, String closingComments) throws CmsoException {
        Map<String, Object> variables =
                        getCloseVariables(schedule, group, cmSchedules, changeId, closureCode, closingComments);
        JsonNode closeChangeRecord = buildCreateRequest.createCloseCancelChangeRecord(variables);
        debug.debug("closeChangeRecord=" + closeChangeRecord.toString());
        postCloseChangeTicket(closeChangeRecord, schedule.getScheduleId(), changeId);
    }

    /**
     * Cancel ticket.
     *
     * @param schedule the schedule
     * @param cms the cms
     * @param changeId the change id
     * @throws CmsoException the CMS exception
     */
    public void cancelTicket(Schedule schedule, ChangeManagementSchedule cms, String changeId) throws CmsoException {
        Map<String, Object> variables = getCancelVariables(schedule, changeId);
        JsonNode cancelChangeRecord = buildCreateRequest.createCancelChangeRecord(variables);
        debug.debug("cancelChangeRecord=" + cancelChangeRecord.toString());
        postCloseChangeTicket(cancelChangeRecord, schedule.getScheduleId(), changeId);
    }

    /**
     * Update ticket.
     *
     * @param schedule the schedule
     * @param cms the cms
     * @param changeId the change id
     * @throws CmsoException the CMS exception
     */
    public void updateTicket(Schedule schedule, ChangeManagementSchedule cms, String changeId) throws CmsoException {
        Map<String, Object> variables = getUpdateVariables(schedule, changeId);
        JsonNode updateChangeRecord = buildCreateRequest.createUpdateChangeRecord(variables);
        debug.debug("updateChangeRecord=" + updateChangeRecord.toString());
        postUpdateChangeTicket(updateChangeRecord, schedule.getScheduleId(), changeId);
    }

    /**
     * Gets the change ticket.
     *
     * @param changeId the change id
     * @return the change ticket
     */
    public TmChangeInfo getChangeTicket(String changeId) {
        Map<String, String> mdcSave = Mdc.save();
        try {
            Map<String, Object> variables = new HashMap<>();
            variables.put("changeId", changeId);
            JsonNode getChangeRecord = buildCreateRequest.createUpdateChangeRecord(variables);
            Response response = tmPost(Endpoint.GET, getChangeRecord, UUID.randomUUID().toString());
            switch (response.getStatus()) {
                case 200: {
                    TmChangeInfo resp = response.readEntity(TmChangeInfo.class);
                    if (resp != null) {
                        return resp;
                    }
                }
                    break;
                default: {
                    Observation.report(LogMessages.UNEXPECTED_RESPONSE, "TM", String.valueOf(response.getStatus()),
                                    response.toString());
                }
            }
        } catch (Exception e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e.toString());
        } finally {
            Mdc.restore(mdcSave);
        }
        return null;
    }

    private Map<String, Object> getCloseVariables(Schedule schedule, ChangeManagementGroup group,
                    List<ChangeManagementSchedule> cmSchedules, String changeId, ClosureCode closureCode,
                    String closingComments) {
        String requesterId = schedule.getUserId();
        if (requesterId.length() > Variables.requesterId.getMaxLength()) {
            requesterId = requesterId.substring(0, Variables.requesterId.getMaxLength());
        }
        long actualStartDate = 0;
        long actualEndDate = 0;
        for (ChangeManagementSchedule cms : cmSchedules) {
            if (cms.getDispatchTimeMillis() != null) {
                if (actualStartDate == 0 || cms.getDispatchTimeMillis() < actualStartDate) {
                    actualStartDate = cms.getDispatchTimeMillis();
                }
            }
            if (cms.getExecutionCompletedTimeMillis() != null) {
                if (cms.getExecutionCompletedTimeMillis() > actualEndDate) {
                    actualEndDate = cms.getExecutionCompletedTimeMillis();
                }
            }
        }
        if (closureCode != ClosureCode.Successful) {
            if (actualEndDate == 0) {
                actualEndDate = System.currentTimeMillis();
            }
            if (actualStartDate == 0) {
                actualStartDate = actualEndDate - 1000;
            }

        }
        Map<String, Object> variables = new HashMap<String, Object>();

        variables.put(Variables.status.toString(), "Closed");
        variables.put(Variables.requesterId.toString(), requesterId);
        variables.put(Variables.actualStartDate.toString(), actualStartDate / 1000);
        variables.put(Variables.actualEndDate.toString(), actualEndDate / 1000);
        variables.put(Variables.changeId.toString(), changeId);
        variables.put(Variables.closureCode.toString(), closureCode.toString());
        variables.put(Variables.closingComments.toString(), closingComments);
        return variables;
    }

    private Map<String, Object> getCancelVariables(Schedule schedule, String changeId) {
        String requesterId = schedule.getUserId();
        Map<String, Object> variables = new HashMap<String, Object>();
        if (requesterId.length() > Variables.requesterId.getMaxLength()) {
            requesterId = requesterId.substring(0, Variables.requesterId.getMaxLength());
        }
        variables.put(Variables.requesterId.toString(), requesterId);
        variables.put(Variables.changeId.toString(), changeId);
        return variables;
    }

    private Map<String, Object> getUpdateVariables(Schedule schedule, String changeId) {
        String requesterId = schedule.getUserId();
        Map<String, Object> variables = new HashMap<String, Object>();
        if (requesterId.length() > Variables.requesterId.getMaxLength()) {
            requesterId = requesterId.substring(0, Variables.requesterId.getMaxLength());
        }
        variables.put(Variables.requesterId.toString(), requesterId);
        variables.put(Variables.changeId.toString(), changeId);
        return variables;
    }

    private void postCloseChangeTicket(JsonNode closeChangeRecord, String scheduleId, String changeId)
                    throws CmsoException {
        Map<String, String> mdcSave = Mdc.save();
        try {
            Response response = null;
            debug.debug("postCloseChangeTicket {}", closeChangeRecord.asText());
            Observation.report(LogMessages.TM_CLOSE_CHANGE_RECORD, "Begin", scheduleId, changeId);
            // response = vtmPost(url, closeChangeRecord, scheduleId);
            response = tmPost(Endpoint.CLOSE, closeChangeRecord, scheduleId);
            Observation.report(LogMessages.TM_CLOSE_CHANGE_RECORD, "End", scheduleId, changeId);
            switch (response.getStatus()) {
                case 200: {
                    String resp = response.readEntity(String.class);
                    debug.debug("response=" + resp.toString());
                }
                    break;
                case 400: {
                    String respString = response.readEntity(String.class);
                    debug.debug("response=" + respString);
                    if (!isAlreadyClosed(respString)) {
                        Observation.report(LogMessages.UNEXPECTED_RESPONSE, "vTM", String.valueOf(response.getStatus()),
                                        response.toString() + " : " + respString);
                        throw new CmsoException(Status.PRECONDITION_FAILED, LogMessages.UNABLE_TO_CLOSE_CHANGE_TICKET,
                                        scheduleId, changeId, respString);
                    }
                }
                    break;
                default: {
                    String message = response.readEntity(String.class);
                    Observation.report(LogMessages.UNEXPECTED_RESPONSE, "vTM", String.valueOf(response.getStatus()),
                                    response.toString() + " : " + message);
                    throw new CmsoException(Status.PRECONDITION_FAILED, LogMessages.UNABLE_TO_CLOSE_CHANGE_TICKET,
                                    scheduleId, changeId, message);
                }
            }
        } catch (ProcessingException e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
            throw new CmsoException(Status.PRECONDITION_FAILED, LogMessages.UNABLE_TO_CLOSE_CHANGE_TICKET, scheduleId,
                            changeId, e.toString());
        } finally {
            Mdc.restore(mdcSave);
        }

    }

    private boolean isAlreadyClosed(String respString) {
        try {
            ObjectMapper om = new ObjectMapper();
            ObjectNode resp = om.readValue(respString, ObjectNode.class);
            if (resp != null) {
                debug.debug("resp=" + resp.toString());
                ArrayNode errs = (ArrayNode) resp.get("serviceException");
                if (errs != null) {
                    for (JsonNode jn : errs) {
                        ObjectNode on = (ObjectNode) jn;
                        String messageId = on.get("messageId").asText();
                        String text = on.get("text").asText();
                        if (messageId.equals("SVC40006") && text.contains("is in Closed status")) {
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
        }
        return false;
    }

    private List<TmAsset> getAssetList(List<String> vnfNames) {
        List<TmAsset> assetList = new ArrayList<TmAsset>();
        for (String vnfName : vnfNames) {
            TmAsset asset = new TmAsset();
            asset.setAssetId(vnfName);
            assetList.add(asset);
        }
        return assetList;
    }

    private Map<String, Object> getVariables(Schedule schedule, ChangeManagementGroup group, List<String> vnfNames,
                    List<DomainData> domainData) {
        String requesterId = schedule.getUserId();
        Map<String, Object> variables = new HashMap<String, Object>();

        String vnfList = vnfNames.toString();
        if (vnfList.length() > Variables.vnfList.getMaxLength()) {
            vnfList = vnfList.substring(0, Variables.vnfList.getMaxLength());
        }
        if (requesterId.length() > Variables.requesterId.getMaxLength()) {
            requesterId = requesterId.substring(0, Variables.requesterId.getMaxLength());
        }

        variables.put(Variables.vnfList.toString(), vnfList);
        variables.put(Variables.vnfName.toString(), vnfNames.get(0));
        variables.put(Variables.requesterId.toString(), requesterId);
        Long plannedStartDate = group.getStartTimeMillis();
        Long plannedEndDate = group.getFinishTimeMillis();
        variables.put(Variables.plannedStartDate.toString(), plannedStartDate / 1000);
        variables.put(Variables.plannedEndDate.toString(), plannedEndDate / 1000);
        Long validationStartTime = plannedStartDate + group.getNormalDurationInSecs();
        Long backoutStartTime = plannedEndDate - group.getAdditionalDurationInSecs();
        variables.put(Variables.validationStartTime.toString(), validationStartTime / 1000);
        variables.put(Variables.backoutStartTime.toString(), backoutStartTime / 1000);
        // These will be display UTC -
        variables.put(Variables.validationStartTimeDisplay.toString(),
                        ISODateTimeFormat.dateTimeNoMillis().print(validationStartTime));
        variables.put(Variables.backoutStartTimeDisplay.toString(),
                        ISODateTimeFormat.dateTimeNoMillis().print(backoutStartTime));
        variables.put(Variables.plannedStartTimeDisplay.toString(),
                        ISODateTimeFormat.dateTimeNoMillis().print(plannedStartDate));
        variables.put(Variables.plannedEndTimeDisplay.toString(),
                        ISODateTimeFormat.dateTimeNoMillis().print(plannedEndDate));

        // Ticket field values can be passed in via the DomainData
        JsonNode defaultValues = buildCreateRequest.getYaml("DefaultChangeTicketProperties");
        Iterator<String> names = defaultValues.fieldNames();
        while (names.hasNext()) {
            String name = names.next();
            JsonNode valueNode = defaultValues.get(name);
            String value = valueNode.asText("");
            variables.put("dd." + name, value);
        }

        // Override defaults from the request.
        for (DomainData dd : domainData) {
            String name = dd.getName();
            String value = dd.getValue();
            variables.put("dd." + name, value);
        }
        // Allow values to be templates as well
        // Did this so plans: ${dd.workflowName}
        for (String name : variables.keySet()) {
            Object value = variables.get(name);
            if (value instanceof String) {
                StringSubstitutor sub = new StringSubstitutor(variables);
                value = sub.replace(value.toString());
                variables.put(name, value);
            }
        }
        return variables;

    }

    private String postCreateChangeTicket(JsonNode createChangeRecord, String scheduleId) throws CmsoException {
        String changeId = null;
        Map<String, String> mdcSave = Mdc.save();
        try {
            Response response = null;
            debug.debug("postCreateChangeTicket {}", createChangeRecord.toString());
            Observation.report(LogMessages.TM_CREATE_CHANGE_RECORD, "Begin", scheduleId);
            // response = vtmPost(url, createChangeRecord, scheduleId);
            response = tmPost(Endpoint.CREATE, createChangeRecord, scheduleId);
            Observation.report(LogMessages.TM_CREATE_CHANGE_RECORD, "End", scheduleId);
            switch (response.getStatus()) {
                case 200: {
                    ObjectNode json = response.readEntity(ObjectNode.class);
                    if (json != null) {
                        debug.debug("Message returned by vTM " + json.toString());
                    }
                    if (json != null && json.get("changeId") != null) {
                        changeId = json.get("changeId").textValue();
                        if (changeId != null) {
                            debug.debug("ChangeId=" + changeId);
                        }
                    } else {
                        Observation.report(LogMessages.UNEXPECTED_RESPONSE, "vTM", String.valueOf(response.getStatus()),
                                        response.toString() + " : " + "Response is empty");
                        throw new CmsoException(Status.EXPECTATION_FAILED, LogMessages.UNABLE_TO_CREATE_CHANGE_TICKET,
                                        scheduleId, "Response is empty");
                    }
                }
                    break;
                default: {
                    String message = response.readEntity(String.class);
                    Observation.report(LogMessages.UNEXPECTED_RESPONSE, "vTM", String.valueOf(response.getStatus()),
                                    response.toString() + " : " + message);
                    throw new CmsoException(Status.EXPECTATION_FAILED, LogMessages.UNABLE_TO_CREATE_CHANGE_TICKET,
                                    scheduleId, message);
                }
            }
        } catch (ProcessingException e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
            throw new CmsoException(Status.EXPECTATION_FAILED, LogMessages.UNABLE_TO_CREATE_CHANGE_TICKET, scheduleId,
                            e.toString());
        } finally {
            Mdc.restore(mdcSave);
        }
        return changeId;
    }

    private String postUpdateChangeTicket(JsonNode updateChangeRecord, String scheduleId, String changeId)
                    throws CmsoException {
        Map<String, String> mdcSave = Mdc.save();
        try {
            String url = env.getProperty("vtm.url") + env.getProperty("vtm.updatePath");

            Response response = null;
            debug.debug("postUpdateChangeTicket {}", updateChangeRecord.toString());
            Observation.report(LogMessages.TM_UPDATE_CHANGE_RECORD, "Begin", scheduleId, changeId, url);
            // response = vtmPost(url, updateChangeRecord, scheduleId);
            response = tmPost(Endpoint.UPDATE, updateChangeRecord, scheduleId);
            Observation.report(LogMessages.TM_UPDATE_CHANGE_RECORD, "End", scheduleId, changeId, url);
            switch (response.getStatus()) {
                case 200: {
                    String resp = response.readEntity(String.class);
                    debug.debug("response=" + resp.toString());
                }
                    break;
                default: {
                    String message = response.readEntity(String.class);
                    Observation.report(LogMessages.UNEXPECTED_RESPONSE, "vTM", String.valueOf(response.getStatus()),
                                    response.toString() + " : " + message);
                    throw new CmsoException(Status.PRECONDITION_FAILED, LogMessages.UNABLE_TO_UPDATE_CHANGE_TICKET,
                                    scheduleId, changeId, message);
                }
            }
        } catch (ProcessingException e) {
            Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.getMessage());
            throw new CmsoException(Status.PRECONDITION_FAILED, LogMessages.UNABLE_TO_UPDATE_CHANGE_TICKET, scheduleId,
                            changeId, e.toString());
        } finally {
            Mdc.restore(mdcSave);
        }
        return changeId;
    }


    private Response tmPost(Endpoint ep, Object request, String scheduleId) throws CmsoException {
        Response response = null;
        List<String> endpoints = new ArrayList<>();
        String url = tmEndpoints.getEndpoint(ep, endpoints);
        while (url != null) {
            try {
                String user = env.getProperty("mechid.user");
                String pass = pm.getProperty("mechid.pass", "");
                // Cannot provide changeId. Interesting.
                // This should be replaced by fetch
                // For now, make a best effort to get the passed changeId

                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);
                ObjectMapper mapper = new ObjectMapper();
                String jsonRequest = mapper.writeValueAsString(request);
                debug.debug("TM URL = " + url + " user=" + user + " : " + jsonRequest);
                response = invocationBuilder.post(Entity.json(request));
                // String message = response.readEntity(String.class);
                // debug.debug("Return from " + url + " : " + response.toString() + "\n" +
                // message);
                debug.debug("Return from " + url + " : " + response.toString());
                return response;
            } catch (ProcessingException e) {
                Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                url = tmEndpoints.getNextEndpoint(ep, endpoints);
                if (url == null || !tryNextUrl(e)) {
                    throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.UNABLE_TO_CREATE_CHANGE_TICKET,
                                    scheduleId, e.getMessage());
                }
            } catch (Exception e) {
                Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.UNABLE_TO_CREATE_CHANGE_TICKET,
                                scheduleId, e.getMessage());
            }
        }
        return response;
    }

    private boolean tryNextUrl(ProcessingException exc) {
        if (exc.getCause() instanceof UnknownHostException) {
            return true;
        }
        return true;
    }

    /**
     * Health check.
     *
     * @return the health check component
     */
    public HealthCheckComponent healthCheck() {
        // No op
        HealthCheckComponent hcc = new HealthCheckComponent();
        hcc.setName("TM Interface");
        hcc.setUrl("");
        hcc.setHealthy(true);
        hcc.setStatus("OK");
        return hcc;
    }
}