summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/datarouter/query/ChameleonRouter.java
blob: 2df25241de8db9ac20b8d6213ba96771ed73aabe (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * 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.
 * ============LICENSE_END=========================================================
 */
package  org.onap.aai.datarouter.query;

import java.security.InvalidParameterException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.ws.rs.BadRequestException;

import org.apache.camel.Exchange;
import org.apache.camel.RuntimeCamelException;
import org.onap.aai.rest.RestClientEndpoint;
import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;

public class ChameleonRouter extends QueryRouter {

  Logger logger = LoggerFactory.getInstance().getLogger(ChameleonRouter.class.getName());

  private String chameleonBaseURL;

  private enum ChameleonAction {
    GET_OBJECT_BY_ID, GET_REL_BY_ID, GET_OBJECT_RELS, GET_OBJECTS_BY_FILTER, GET_RELS_BY_FILTER
  };

  private static final Pattern QUERY_OBJECT_FILTER_URL_MATCH = Pattern.compile("/objects/filter(.*)");
  private static final Pattern QUERY_REL_FILTER_URL_MATCH = Pattern.compile("/relationships/filter(.*)");
  private static final Pattern QUERY_OBJECT_REL_URL_MATCH = Pattern.compile("/objects/relationships/(.*)");
  private static final Pattern QUERY_OBJECT_ID_URL_MATCH = Pattern.compile("/objects/(.*)");
  private static final Pattern QUERY_REL_ID_URL_MATCH = Pattern.compile("/relationships/(.*)");

  private static final String ECOMP_QUERY_ID = "ECOMP_QUERY_ID";
  private static final String ECOMP_QUERY_TYPE = "ECOMP_QUERY_TYPE";

  public ChameleonRouter(String chameleonBaseURL) {
    String baseURL = chameleonBaseURL.endsWith("/") ? chameleonBaseURL.substring(0, chameleonBaseURL.length() - 1)
        : chameleonBaseURL;
    if (checkRecursion(baseURL)) {
      logger.error(QueryMsgs.QUERY_ERROR,
          "Invalid chameleonBaseURL : Can't re-route back to DataRouter " + chameleonBaseURL);
      throw new InvalidParameterException(
          "Invalid chameleonBaseURL : Can't re-route back to DataRouter " + chameleonBaseURL);
    }
    this.chameleonBaseURL = baseURL;
  }

  public void setQueryRequest(Exchange exchange) {
    setMDC(exchange);
    ChameleonAction action = resolveChameleonAction(exchange);
    String ecompUrl = buildUrl(exchange, action);
    logger.info(QueryMsgs.QUERY_INFO, "Routing request to Chameleon service URL: " + ecompUrl);
    exchange.getIn().setHeader(RestClientEndpoint.IN_HEADER_URL, ecompUrl);
    exchange.getIn().setHeader("X-FromAppId", SERVICE_NAME);
    exchange.getIn().setHeader("X-TransactionId", getTxId(exchange));

  }

  private boolean urlMatcher(Pattern p, String url) {
    Matcher m = p.matcher(url);
    if (m.matches() && !m.group(1).contains("/")) {
      return true;
    } else {
      return false;
    }
  }

  private ChameleonAction resolveChameleonAction(Exchange exchange) {
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
    ChameleonAction action;

    if (urlMatcher(QUERY_OBJECT_FILTER_URL_MATCH, path)) {
      action = ChameleonAction.GET_OBJECTS_BY_FILTER;
    } else if (urlMatcher(QUERY_REL_FILTER_URL_MATCH, path)) {
      action = ChameleonAction.GET_RELS_BY_FILTER;
    } else if (urlMatcher(QUERY_OBJECT_REL_URL_MATCH, path)) {
      action = ChameleonAction.GET_OBJECT_RELS;
    } else if (urlMatcher(QUERY_OBJECT_ID_URL_MATCH, path)) {
      action = ChameleonAction.GET_OBJECT_BY_ID;
    } else if (urlMatcher(QUERY_REL_ID_URL_MATCH, path)) {
      action = ChameleonAction.GET_REL_BY_ID;
    } else {
      exchange.getIn().setHeader(ChameleonErrorProcessor.ECOMP_QUERY_ERROR_CODE, 404);
      throw new RuntimeCamelException();
    }
    return action;
  }

  private String buildUrl(Exchange exchange, ChameleonAction action) {
    String path = exchange.getIn().getHeader(Exchange.HTTP_PATH, String.class);
    path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
    String queryParams = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
    String ecompUrl = "";
    String ID = "";

    switch (action) {
    case GET_OBJECT_BY_ID:
      ID = path.substring(path.lastIndexOf("/") + 1, path.length());
      if (ID == null || ID.isEmpty()) {
        throw new IllegalArgumentException("Invalid URI path with no Object ID: " + path);
      } else {
        if (queryParams != null && !queryParams.isEmpty()) {
          ecompUrl = chameleonBaseURL + "/" + ID + "?" + queryParams;

        } else {
          ecompUrl = chameleonBaseURL + "/" + ID;
        }
      }
      exchange.getIn().setHeader(ECOMP_QUERY_ID, ID);
      exchange.getIn().setHeader(ECOMP_QUERY_TYPE, ChameleonAction.GET_OBJECT_BY_ID);
      break;

    case GET_REL_BY_ID:
      ID = path.substring(path.lastIndexOf("/") + 1, path.length());
      if (ID == null || ID.isEmpty()) {
        throw new IllegalArgumentException("Invalid URI path with no Relationship ID: " + path);
      } else {
        if (queryParams != null && !queryParams.isEmpty()) {
          ecompUrl = chameleonBaseURL + "/" + ID + "?" + queryParams;

        } else {
          ecompUrl = chameleonBaseURL + "/" + ID;
        }
      }
      exchange.getIn().setHeader(ECOMP_QUERY_ID, ID);
      exchange.getIn().setHeader(ECOMP_QUERY_TYPE, ChameleonAction.GET_REL_BY_ID);
      break;

    case GET_OBJECT_RELS:
      ID = path.substring(path.lastIndexOf("/") + 1, path.length());
      if (ID == null || ID.isEmpty()) {
        throw new IllegalArgumentException("Invalid URI path with no Object ID: " + path);
      } else {
        if (queryParams != null && !queryParams.isEmpty()) {
          // TODO: Fix the URL for getting object relations when Chameloen
          // supports this API
          ecompUrl = chameleonBaseURL + "/relations" + ID + "?" + queryParams;

        } else {
          ecompUrl = chameleonBaseURL + "/relations" + ID;
        }
      }
      exchange.getIn().setHeader(ECOMP_QUERY_ID, ID);
      exchange.getIn().setHeader(ECOMP_QUERY_TYPE, ChameleonAction.GET_OBJECT_RELS);
      break;

    case GET_OBJECTS_BY_FILTER:
      if (queryParams != null && !queryParams.isEmpty()) {
        // TODO: Fix the URL for getting object filter when Chameloen
        // supports this API
        ecompUrl = chameleonBaseURL + "/filter?" + queryParams;
      } else {
        ecompUrl = chameleonBaseURL + "/filter";
      }
      exchange.getIn().setHeader(ECOMP_QUERY_TYPE, ChameleonAction.GET_OBJECTS_BY_FILTER);
      break;

    case GET_RELS_BY_FILTER:
      if (queryParams != null && !queryParams.isEmpty()) {
        // TODO: Fix the URL for getting rel filter when Chameloen
        // supports this API
        ecompUrl = chameleonBaseURL + "/filter?" + queryParams;
      } else {
        ecompUrl = chameleonBaseURL + "/filter";
      }
      exchange.getIn().setHeader(ECOMP_QUERY_TYPE, ChameleonAction.GET_RELS_BY_FILTER);
      break;

    }

    return ecompUrl;
  }

  public void setQueryResponse(Exchange exchange) {
    parseResponse(exchange);
    adjustHeaders(exchange);
  }

  private void adjustHeaders(Exchange exchange) {
    // Remove the internal heders
    exchange.getIn().removeHeader(ECOMP_QUERY_ID);
    exchange.getIn().removeHeader(ECOMP_QUERY_TYPE);
  }

  private void parseResponse(Exchange exchange) throws BadRequestException {

    ChameleonAction action = exchange.getIn().getHeader(ECOMP_QUERY_TYPE, ChameleonAction.class);
    Integer httpResponseCode = exchange.getIn().getHeader(RestClientEndpoint.OUT_HEADER_RESPONSE_CODE, Integer.class);
    String ID = "";

    switch (action) {
    case GET_OBJECT_BY_ID:
      if (httpResponseCode >= 200 && httpResponseCode <= 299) {
        ID = exchange.getIn().getHeader(ECOMP_QUERY_ID, String.class);
        if (ID == null || ID.isEmpty()) {
          exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
        } else {
          ChameleonResponseBuiler.buildEntity(exchange, ID);
        }
      } else {
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, httpResponseCode);
      }
      break;
    case GET_REL_BY_ID:
      if (httpResponseCode >= 200 && httpResponseCode <= 299) {
        ID = exchange.getIn().getHeader(ECOMP_QUERY_ID, String.class);
        if (ID == null || ID.isEmpty()) {
          exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
        } else {
          ChameleonResponseBuiler.buildEntity(exchange, ID);
        }
      } else {
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, httpResponseCode);
      }
      break;
    case GET_OBJECT_RELS:
      if (httpResponseCode >= 200 && httpResponseCode <= 299) {
        ID = exchange.getIn().getHeader(ECOMP_QUERY_ID, String.class);
        if (ID == null || ID.isEmpty()) {
          exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 400);
        } else {
          ChameleonResponseBuiler.buildObjectRelationship(exchange, ID);
        }
      } else {
        // TODO:Return 200 with empty body for now until chameleon supports this
        // query
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
        exchange.getIn().setBody("[]");
      }
      break;
    case GET_OBJECTS_BY_FILTER:
      if (httpResponseCode >= 200 && httpResponseCode <= 299) {
        ChameleonResponseBuiler.buildCollection(exchange);
      } else {
        // TODO:Return 200 with empty body for now until chameleon supports this
        // query
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
        exchange.getIn().setBody("[]");
      }
      break;
    case GET_RELS_BY_FILTER:
      if (httpResponseCode >= 200 && httpResponseCode <= 299) {
        ChameleonResponseBuiler.buildCollection(exchange);
      } else {
        // TODO:Return 200 with empty body for now until chameleon supports this
        // query
        exchange.getIn().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
        exchange.getIn().setBody("[]");
      }
      break;

    }

  }

}