4d8e35e718611f6d30dd484bb6c4d2a6d821296c
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / main / java / org / onap / policy / controlloop / actor / sdnc / SdncOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.sdnc;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
32 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
35 import org.onap.policy.sdnc.SdncRequest;
36 import org.onap.policy.sdnc.SdncResponse;
37
38 /**
39  * Superclass for SDNC Operators.
40  */
41 public abstract class SdncOperation extends HttpOperation<SdncResponse> {
42
43     /**
44      * Constructs the object.
45      *
46      * @param params operation parameters
47      * @param config configuration for this operation
48      * @param propertyNames names of properties required by this operation
49      */
50     public SdncOperation(ControlLoopOperationParams params, HttpConfig config, List<String> propertyNames) {
51         super(params, config, SdncResponse.class, propertyNames);
52     }
53
54     /**
55      * Starts the GUARD.
56      */
57     @Override
58     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
59         return startGuardAsync();
60     }
61
62     @Override
63     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
64
65         SdncRequest request = makeRequest(attempt);
66
67         Map<String, Object> headers = makeHeaders();
68
69         headers.put("Accept", MediaType.APPLICATION_JSON);
70         String path = getPath();
71         String url = getClient().getBaseUrl() + path;
72
73         String strRequest = prettyPrint(request);
74         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
75
76         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
77
78         // @formatter:off
79         return handleResponse(outcome, url,
80             callback -> getClient().post(callback, path, entity, headers));
81         // @formatter:on
82     }
83
84     /**
85      * Makes the request.
86      *
87      * @param attempt current attempt, starting with "1"
88      * @return a new request to be posted
89      */
90     protected abstract SdncRequest makeRequest(int attempt);
91
92     /**
93      * Checks that the response has an "output" and that the output indicates success.
94      */
95     @Override
96     protected boolean isSuccess(Response rawResponse, SdncResponse response) {
97         return response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode());
98     }
99 }