Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / AaiResponseTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.vid.aai;
22
23 import com.fasterxml.jackson.databind.JsonNode;
24 import org.apache.commons.lang3.StringUtils;
25 import org.springframework.stereotype.Component;
26
27 import java.util.Optional;
28
29 @Component
30 public class AaiResponseTranslator {
31
32     public PortMirroringConfigData extractPortMirroringConfigData(AaiResponse<JsonNode> aaiResponse) {
33         return extractErrorResponseIfHttpError(aaiResponse).orElseGet(() -> extractPortMirroringConfigData(aaiResponse.getT()));
34     }
35
36     public PortMirroringConfigData extractPortMirroringConfigData(JsonNode cloudRegionAndSourceFromConfigurationResponse) {
37         final JsonNode payload = cloudRegionAndSourceFromConfigurationResponse;
38         if (payload == null) {
39             return new PortMirroringConfigDataError("Response payload is null", null);
40         }
41
42         final JsonNode results = payload.path("results");
43         if (results.isMissingNode()) {
44             return new PortMirroringConfigDataError("Root node 'results' is missing", payload.toString());
45         }
46
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);
51             }
52         }
53         return new PortMirroringConfigDataError("Root node 'results' has no node where 'node-TYPE' is 'cloud-region'", payload.toString());
54     }
55
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());
61         }
62
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());
66         }
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());
69         }
70
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());
74         }
75
76         return new PortMirroringConfigDataOk(cloudRegionId);
77     }
78
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)
85             );
86         } else {
87             return Optional.empty();
88         }
89     }
90
91     public abstract static class PortMirroringConfigData {
92     }
93
94     public static class PortMirroringConfigDataOk extends PortMirroringConfigData {
95         private final String cloudRegionId;
96
97         public PortMirroringConfigDataOk(String cloudRegionId) {
98             this.cloudRegionId = cloudRegionId;
99         }
100
101         public String getCloudRegionId() {
102             return cloudRegionId;
103         }
104     }
105
106     public static class PortMirroringConfigDataError extends PortMirroringConfigData {
107         private final String errorDescription;
108         private final String rawAaiResponse;
109
110         public PortMirroringConfigDataError(String errorDescription, String rawAaiResponse) {
111             this.errorDescription = errorDescription;
112             this.rawAaiResponse = rawAaiResponse;
113         }
114
115         public String getErrorDescription() {
116             return errorDescription;
117         }
118
119         public String getRawAaiResponse() {
120             return rawAaiResponse;
121         }
122     }
123 }