fd19d9d3ad568401be26dc85304350a151a18ff9
[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.main.parameters.ParticipantDcaeParameters;
25 import org.onap.policy.clamp.controlloop.participant.dcae.model.ExternalComponent;
26 import org.onap.policy.clamp.controlloop.participant.dcae.model.Loop;
27 import org.springframework.stereotype.Component;
28
29 @Component
30 public class ClampHttpClient extends AbstractHttpClient {
31
32     private static final String STATUS = "/restservices/clds/v2/loop/getstatus/";
33     private static final String CREATE = "/restservices/clds/v2/loop/create/%s?templateName=%s";
34     private static final String DEPLOY = "/restservices/clds/v2/loop/deploy/";
35     private static final String STOP = "/restservices/clds/v2/loop/stop/";
36     private static final String DELETE = "/restservices/clds/v2/loop/delete/";
37     private static final String UNDEPLOY = "/restservices/clds/v2/loop/undeploy/";
38     public static final String STATUS_NOT_FOUND = "STATUS_NOT_FOUND";
39     public static final String POLICY_NOT_FOUND = "POLICY_NOT_FOUND";
40
41     /**
42      * Constructor.
43      *
44      * @param parameters the DCAE parameters
45      */
46     public ClampHttpClient(ParticipantDcaeParameters parameters) {
47         super(parameters.getClampClientParameters());
48     }
49
50     /**
51      * Create.
52      *
53      * @param loopName the loopName
54      * @param templateName the templateName
55      * @return the Loop object or null if error occurred
56      */
57     public Loop create(String loopName, String templateName) {
58         return executePost(String.format(CREATE, loopName, templateName), HttpStatus.SC_OK);
59     }
60
61     /**
62      * Deploy.
63      *
64      * @param loopName the loopName
65      * @return true
66      */
67     public boolean deploy(String loopName) {
68         return executePut(DEPLOY + loopName, HttpStatus.SC_ACCEPTED);
69     }
70
71     /**
72      * Get Status.
73      *
74      * @param loopName the loopName
75      * @return the Loop object or null if error occurred
76      */
77     public Loop getstatus(String loopName) {
78         return executeGet(STATUS + loopName, HttpStatus.SC_OK);
79     }
80
81     /**
82      * Undeploy.
83      *
84      * @param loopName the loopName
85      * @return true
86      */
87     public boolean undeploy(String loopName) {
88         return executePut(UNDEPLOY + loopName, HttpStatus.SC_ACCEPTED);
89     }
90
91     /**
92      * Stop.
93      *
94      * @param loopName the loopName
95      * @return true
96      */
97     public boolean stop(String loopName) {
98         return executePut(STOP + loopName, HttpStatus.SC_OK);
99     }
100
101     /**
102      * Delete.
103      *
104      * @param loopName the loopName
105      * @return true
106      */
107     public boolean delete(String loopName) {
108         return executePut(DELETE + loopName, HttpStatus.SC_OK);
109     }
110
111     /**
112      * return status from Loop object.
113      *
114      * @param loop Loop
115      * @return status
116      */
117     public static String getStatusCode(Loop loop) {
118         if (loop == null || loop.getComponents() == null || loop.getComponents().isEmpty()) {
119             return STATUS_NOT_FOUND;
120         }
121         ExternalComponent externalComponent = loop.getComponents().get("DCAE");
122         if (externalComponent == null || externalComponent.getComponentState() == null) {
123             return STATUS_NOT_FOUND;
124         }
125
126         return externalComponent.getComponentState().getStateName();
127     }
128 }