eb805054da3b961354328ce2c767d4a49a76d154
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.participant.dcae.httpclient;
22
23 import org.apache.http.HttpStatus;
24 import org.onap.policy.clamp.controlloop.participant.dcae.model.ExternalComponent;
25 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
26 import org.onap.policy.common.endpoints.parameters.RestServerParameters;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 public class ClampHttpClient extends AbstractHttpClient {
31
32     private static final Logger LOGGER = LoggerFactory.getLogger(ClampHttpClient.class);
33
34     private static final String STATUS = "/restservices/clds/v2/loop/getstatus/";
35     private static final String CREATE = "/restservices/clds/v2/loop/create/%s?templateName=%s";
36     private static final String UPDATE = "/restservices/clds/v2/loop/updateMicroservicePolicy/";
37     private static final String DEPLOY = "/restservices/clds/v2/loop/deploy/";
38     private static final String STOP = "/restservices/clds/v2/loop/stop/";
39     private static final String DELETE = "/restservices/clds/v2/loop/delete/";
40     private static final String UNDEPLOY = "/restservices/clds/v2/loop/undeploy/";
41     public static final String STATUS_NOT_FOUND = "STATUS_NOT_FOUND";
42     public static final String POLICY_NOT_FOUND = "POLICY_NOT_FOUND";
43
44     /**
45      * Constructor.
46      */
47     public ClampHttpClient(RestServerParameters restServerParameters) {
48         super(restServerParameters);
49     }
50
51     /**
52      * Create.
53      *
54      * @param loopName the loopName
55      * @param templateName the templateName
56      * @return the Loop object or null if error occurred
57      */
58     public Loop create(String loopName, String templateName) {
59         return executePost(String.format(CREATE, loopName, templateName), HttpStatus.SC_OK);
60     }
61
62     /**
63      * Update.
64      *
65      * @param loopName the loopName
66      * @param jsonEntity the Json entity
67      * @return true
68      */
69     public boolean update(String loopName, String jsonEntity) {
70         return executePost(UPDATE + loopName, HttpStatus.SC_OK) != null;
71     }
72
73     /**
74      * Deploy.
75      *
76      * @param loopName the loopName
77      * @return true
78      */
79     public boolean deploy(String loopName) { // DCAE
80         return executePut(DEPLOY + loopName, HttpStatus.SC_ACCEPTED);
81     }
82
83     /**
84      * Get Status.
85      *
86      * @param loopName the loopName
87      * @return the Loop object or null if error occurred
88      */
89     public Loop getstatus(String loopName) {
90         return executeGet(STATUS + loopName, HttpStatus.SC_OK);
91     }
92
93     /**
94      * Undeploy.
95      *
96      * @param loopName the loopName
97      * @return true
98      */
99     public boolean undeploy(String loopName) {
100         return executePut(UNDEPLOY + loopName, HttpStatus.SC_ACCEPTED);
101     }
102
103     /**
104      * Stop.
105      *
106      * @param loopName the loopName
107      * @return true
108      */
109     public boolean stop(String loopName) {
110         return executePut(STOP + loopName, HttpStatus.SC_OK);
111     }
112
113     /**
114      * Delete.
115      *
116      * @param loopName the loopName
117      * @return true
118      */
119     public boolean delete(String loopName) {
120         return executePut(DELETE + loopName, HttpStatus.SC_OK);
121     }
122
123     /**
124      * return status from Loop object.
125      *
126      * @param loop Loop
127      * @return status
128      */
129     public static String getStatusCode(Loop loop) {
130         if (loop == null || loop.getComponents() == null || loop.getComponents().isEmpty()) {
131             return STATUS_NOT_FOUND;
132         }
133         ExternalComponent externalComponent = loop.getComponents().get("DCAE");
134         if (externalComponent == null || externalComponent.getComponentState() == null) {
135             return STATUS_NOT_FOUND;
136         }
137
138         return externalComponent.getComponentState().getStateName();
139     }
140 }