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