aboutsummaryrefslogtreecommitdiffstats
path: root/participant/participant-impl/participant-impl-dcae/src/main/java/org/onap/policy/clamp/controlloop/participant/dcae/httpclient/ClampHttpClient.java
blob: eb805054da3b961354328ce2c767d4a49a76d154 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.controlloop.participant.dcae.httpclient;

import org.apache.http.HttpStatus;
import org.onap.policy.clamp.controlloop.participant.dcae.model.ExternalComponent;
import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClampHttpClient extends AbstractHttpClient {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClampHttpClient.class);

    private static final String STATUS = "/restservices/clds/v2/loop/getstatus/";
    private static final String CREATE = "/restservices/clds/v2/loop/create/%s?templateName=%s";
    private static final String UPDATE = "/restservices/clds/v2/loop/updateMicroservicePolicy/";
    private static final String DEPLOY = "/restservices/clds/v2/loop/deploy/";
    private static final String STOP = "/restservices/clds/v2/loop/stop/";
    private static final String DELETE = "/restservices/clds/v2/loop/delete/";
    private static final String UNDEPLOY = "/restservices/clds/v2/loop/undeploy/";
    public static final String STATUS_NOT_FOUND = "STATUS_NOT_FOUND";
    public static final String POLICY_NOT_FOUND = "POLICY_NOT_FOUND";

    /**
     * Constructor.
     */
    public ClampHttpClient(RestServerParameters restServerParameters) {
        super(restServerParameters);
    }

    /**
     * Create.
     *
     * @param loopName the loopName
     * @param templateName the templateName
     * @return the Loop object or null if error occurred
     */
    public Loop create(String loopName, String templateName) {
        return executePost(String.format(CREATE, loopName, templateName), HttpStatus.SC_OK);
    }

    /**
     * Update.
     *
     * @param loopName the loopName
     * @param jsonEntity the Json entity
     * @return true
     */
    public boolean update(String loopName, String jsonEntity) {
        return executePost(UPDATE + loopName, HttpStatus.SC_OK) != null;
    }

    /**
     * Deploy.
     *
     * @param loopName the loopName
     * @return true
     */
    public boolean deploy(String loopName) { // DCAE
        return executePut(DEPLOY + loopName, HttpStatus.SC_ACCEPTED);
    }

    /**
     * Get Status.
     *
     * @param loopName the loopName
     * @return the Loop object or null if error occurred
     */
    public Loop getstatus(String loopName) {
        return executeGet(STATUS + loopName, HttpStatus.SC_OK);
    }

    /**
     * Undeploy.
     *
     * @param loopName the loopName
     * @return true
     */
    public boolean undeploy(String loopName) {
        return executePut(UNDEPLOY + loopName, HttpStatus.SC_ACCEPTED);
    }

    /**
     * Stop.
     *
     * @param loopName the loopName
     * @return true
     */
    public boolean stop(String loopName) {
        return executePut(STOP + loopName, HttpStatus.SC_OK);
    }

    /**
     * Delete.
     *
     * @param loopName the loopName
     * @return true
     */
    public boolean delete(String loopName) {
        return executePut(DELETE + loopName, HttpStatus.SC_OK);
    }

    /**
     * return status from Loop object.
     *
     * @param loop Loop
     * @return status
     */
    public static String getStatusCode(Loop loop) {
        if (loop == null || loop.getComponents() == null || loop.getComponents().isEmpty()) {
            return STATUS_NOT_FOUND;
        }
        ExternalComponent externalComponent = loop.getComponents().get("DCAE");
        if (externalComponent == null || externalComponent.getComponentState() == null) {
            return STATUS_NOT_FOUND;
        }

        return externalComponent.getComponentState().getStateName();
    }
}