2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.aai;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import org.apache.commons.lang3.StringUtils;
25 import org.springframework.stereotype.Component;
27 import java.util.Optional;
30 public class AaiResponseTranslator {
32 public PortMirroringConfigData extractPortMirroringConfigData(AaiResponse<JsonNode> aaiResponse) {
33 return extractErrorResponseIfHttpError(aaiResponse).orElseGet(() -> extractPortMirroringConfigData(aaiResponse.getT()));
36 public PortMirroringConfigData extractPortMirroringConfigData(JsonNode cloudRegionAndSourceFromConfigurationResponse) {
37 final JsonNode payload = cloudRegionAndSourceFromConfigurationResponse;
38 if (payload == null) {
39 return new PortMirroringConfigDataError("Response payload is null", null);
42 final JsonNode results = payload.path("results");
43 if (results.isMissingNode()) {
44 return new PortMirroringConfigDataError("Root node 'results' is missing", payload.toString());
47 for (JsonNode resultNode : results) {
48 final JsonNode nodeType = resultNode.path("node-type");
49 if (nodeType.isTextual() && "cloud-region".equals(nodeType.textValue())) {
50 return getPortMirroringConfigData(payload, resultNode);
53 return new PortMirroringConfigDataError("Root node 'results' has no node where 'node-TYPE' is 'cloud-region'", payload.toString());
56 private PortMirroringConfigData getPortMirroringConfigData(JsonNode payload, JsonNode resultNode) {
57 final JsonNode properties = resultNode.path("properties");
58 if (properties.isMissingNode()) {
59 final String message = "The node-type 'cloud-region' does not contain a 'properties' node";
60 return new PortMirroringConfigDataError(message, payload.toString());
63 final JsonNode cloudRegionIdNode = properties.path("cloud-region-id");
64 if (cloudRegionIdNode.isMissingNode()) {
65 return new PortMirroringConfigDataError("The node-type 'cloud-region' does not contain the property 'cloud-region-id'", payload.toString());
67 if (!cloudRegionIdNode.isTextual()) {
68 return new PortMirroringConfigDataError("The node-type 'cloud-region' contains a non-textual value for the property 'cloud-region-id'", payload.toString());
71 final String cloudRegionId = cloudRegionIdNode.asText();
72 if (StringUtils.isBlank(cloudRegionId)) {
73 return new PortMirroringConfigDataError("Node 'properties.cloud-region-id' of node-type 'cloud-region' is blank", payload.toString());
76 return new PortMirroringConfigDataOk(cloudRegionId);
79 private Optional<PortMirroringConfigData> extractErrorResponseIfHttpError(AaiResponse aaiResponse) {
80 if (aaiResponse.getHttpCode() != org.springframework.http.HttpStatus.OK.value()) {
81 final String errorMessage = aaiResponse.getErrorMessage();
82 return Optional.of(new PortMirroringConfigDataError(
83 "Got " + aaiResponse.getHttpCode() + " from aai",
84 errorMessage != null ? errorMessage : null)
87 return Optional.empty();
91 public abstract static class PortMirroringConfigData {
94 public static class PortMirroringConfigDataOk extends PortMirroringConfigData {
95 private final String cloudRegionId;
97 public PortMirroringConfigDataOk(String cloudRegionId) {
98 this.cloudRegionId = cloudRegionId;
101 public String getCloudRegionId() {
102 return cloudRegionId;
106 public static class PortMirroringConfigDataError extends PortMirroringConfigData {
107 private final String errorDescription;
108 private final String rawAaiResponse;
110 public PortMirroringConfigDataError(String errorDescription, String rawAaiResponse) {
111 this.errorDescription = errorDescription;
112 this.rawAaiResponse = rawAaiResponse;
115 public String getErrorDescription() {
116 return errorDescription;
119 public String getRawAaiResponse() {
120 return rawAaiResponse;