aboutsummaryrefslogtreecommitdiffstats
path: root/cmso-service/src/main/java/org/onap/optf/cmso/aaf/AafClient.java
blob: e52d295c02d154dad181b2539bceb75fe974ba7e (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * Copyright (c) 2019 AT&T Intellectual Property.
 * Modifications Copyright © 2018 IBM.
 *
 * 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.
 *
 *
 * Unless otherwise specified, all documentation contained herein is licensed
 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
 * you may not use this documentation except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         https://creativecommons.org/licenses/by/4.0/
 *
 * Unless required by applicable law or agreed to in writing, documentation
 * 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.
*/

package org.onap.optf.cmso.aaf;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.onap.observations.Mdc;
import org.onap.observations.Observation;
import org.onap.optf.cmso.SpringProfiles;
import org.onap.optf.cmso.common.BasicAuthenticatorFilter;
import org.onap.optf.cmso.common.LogMessages;
import org.onap.optf.cmso.common.PropertiesManagement;
import org.onap.optf.cmso.common.exceptions.CmsoException;
import org.onap.optf.cmso.filters.CmsoClientFilters;
import org.onap.optf.cmso.service.rs.models.HealthCheckComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
@Profile(SpringProfiles.AAF_AUTHENTICATION)
public class AafClient {
    private static EELFLogger debug = EELFManager.getInstance().getDebugLogger();

    @Autowired
    Environment env;

    @Autowired
    PropertiesManagement pm;

    @Autowired
    AafEndpoints aafEndpoints;

    /**
     * Gets the authz.
     *
     * @param auth the auth
     * @return the authz
     * @throws CmsoException the cmso exception
     */
    public Response getAuthz(Map<String, String> auth) throws CmsoException {
        Response response = null;
        List<String> endpoints = new ArrayList<>();
        String url = aafEndpoints.getEndpoint(AafEndpoints.Endpoint.AUTHZ, endpoints);
        String user = auth.get("user");
        if (!user.contains("@")) {
            user += env.getProperty(AafProperties.aafDefaultUserDomain.toString(), "@csp.att.com");
        }
        String pass = auth.get("password");
        while (url != null) {
            try {
                // Cannot provide changeId. Interesting.
                // This should be replaced by fetch
                // For now, make a best effort to get the passed changeId
                if (!url.endsWith("/")) {
                    url += "/";
                }
                url += user;
                response = get(url, user, pass);
                return response;
            } catch (ProcessingException e) {
                Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                url = aafEndpoints.getNextEndpoint(AafEndpoints.Endpoint.AUTHZ, endpoints);
                if (url == null || !tryNextUrl(e)) {
                    throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.UNEXPECTED_EXCEPTION, user,
                                    e.getMessage());
                }
            } catch (Exception e) {
                Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                throw new CmsoException(Status.INTERNAL_SERVER_ERROR, LogMessages.UNEXPECTED_EXCEPTION, user,
                                e.getMessage());
            }
        }
        return response;
    }

    /**
     * Gets the.
     *
     * @param url the url
     * @param user the user
     * @param pass the pass
     * @return the response
     */
    public Response get(String url, String user, String pass) {
        Client client = ClientBuilder.newClient();
        client.register(new BasicAuthenticatorFilter(user, pass));
        client.register(new CmsoClientFilters());
        WebTarget target = client.target(url);
        Invocation.Builder invocationBuilder = target.request(MediaType.APPLICATION_JSON);
        debug.debug("AAF URL = " + url);
        Response response = invocationBuilder.get();
        debug.debug("AAF URL = " + url + " user=" + user + ":" + response.getStatusInfo().toString());
        return response;
    }

    private boolean tryNextUrl(ProcessingException exc) {
        if (exc.getCause() instanceof UnknownHostException) {
            return true;
        }
        return true;
    }

    /**
     * Health check.
     *
     * @return the health check component
     */
    public HealthCheckComponent healthCheck() {
        Map<String, String> mdcSave = Mdc.save();
        HealthCheckComponent hcc = new HealthCheckComponent();
        hcc.setName("AAF");
        hcc.setHealthy(false);
        List<String> endpoints = new ArrayList<>();
        try {
            String url = aafEndpoints.getEndpoint(AafEndpoints.Endpoint.HEALTHCHECK, endpoints);
            String user = "";
            String pass = "";

            while (url != null) {
                try {
                    hcc.setUrl(url);
                    Response response = get(url, user, pass);
                    hcc.setHealthy(true);
                    hcc.setStatus(response.getStatusInfo().toString());
                } catch (ProcessingException e) {
                    Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                    url = aafEndpoints.getNextEndpoint(AafEndpoints.Endpoint.HEALTHCHECK, endpoints);
                    if (url == null || !tryNextUrl(e)) {
                        hcc.setStatus(e.getMessage());
                    }
                } catch (Exception e) {
                    Observation.report(LogMessages.UNEXPECTED_EXCEPTION, e, e.toString());
                    hcc.setStatus(e.getMessage());
                }
            }
        } finally {
            Mdc.restore(mdcSave);
        }
        return hcc;
    }
}