7840548861ca1ad04bdf4e7b6d82a17c3254f628
[policy/models.git] / models-interactions / model-actors / actor.vfc / src / main / java / org / onap / policy / controlloop / actor / vfc / Restart.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 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.controlloop.actor.vfc;
22
23 import java.util.Map;
24 import java.util.concurrent.CompletableFuture;
25 import javax.ws.rs.client.Entity;
26 import javax.ws.rs.core.MediaType;
27 import org.apache.commons.lang3.tuple.Pair;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
29 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
31 import org.onap.policy.vfc.VfcRequest;
32
33 public class Restart extends VfcOperation {
34     public static final String NAME = "Restart";
35
36     public Restart(ControlLoopOperationParams params, HttpConfig config) {
37         super(params, config);
38     }
39
40     @Override
41     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
42
43         // starting a whole new attempt - reset the count
44         resetPollCount();
45
46         Pair<String, VfcRequest> pair = makeRequest();
47         Entity<VfcRequest> entity = Entity.entity(pair.getRight(), MediaType.APPLICATION_JSON);
48         String path = getPath() + pair.getLeft();
49         String url = getClient().getBaseUrl() + path;
50
51         Map<String, Object> headers = makeHeaders();
52         headers.put("Accept", MediaType.APPLICATION_JSON);
53
54         return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
55     }
56
57     /**
58      * Makes a request.
59      *
60      * @return a pair containing the request URL and the new request
61      */
62     protected Pair<String, VfcRequest> makeRequest() {
63
64         var request = super.constructVfcRequest();
65         String requestUrl = "/" + request.getNsInstanceId() + "/heal";
66         return Pair.of(requestUrl, request);
67     }
68 }