Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / main / java / org / onap / vid / aai / model / PortDetailsTranslator.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.model;
22
23
24 import com.google.common.collect.ImmutableList;
25 import org.onap.vid.aai.AaiResponse;
26
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Optional;
30 import java.util.function.Predicate;
31 import java.util.stream.Collectors;
32
33 public class PortDetailsTranslator {
34
35     public static class PortDetailsOk implements PortDetails {
36
37         private final String interfaceId;
38         private final String interfaceName;
39         private final boolean isPortMirrored;
40
41         public PortDetailsOk(String interfaceId, String interfaceName, boolean isPortMirrored) {
42             this.interfaceId = interfaceId;
43             this.interfaceName = interfaceName;
44             this.isPortMirrored = isPortMirrored;
45         }
46
47         public String getInterfaceId() {
48             return interfaceId;
49         }
50
51         public String getInterfaceName() {
52             return interfaceName;
53         }
54
55         public boolean getIsPortMirrored() {
56             return isPortMirrored;
57         }
58     }
59
60     public interface PortDetails {
61     }
62
63     public static class PortDetailsError implements PortDetails {
64         private final String errorDescription;
65         private final String rawAaiResponse;
66
67         public PortDetailsError(String errorDescription, String rawAaiResponse) {
68             this.errorDescription = errorDescription;
69             this.rawAaiResponse = rawAaiResponse;
70         }
71
72         public String getErrorDescription() {
73             return errorDescription;
74         }
75
76         public String getRawAaiResponse() {
77             return rawAaiResponse;
78         }
79     }
80
81     public static PortDetails extractPortDetailsFromProperties(Properties properties, String rawPayload) {
82         List<String> errorDescriptions = new LinkedList<>();
83         describeIfNullOrEmpty("interface-id", properties.getInterfaceId(), errorDescriptions);
84         describeIfNullOrEmpty("interface-name", properties.getInterfaceName(), errorDescriptions);
85         describeIfNullOrEmpty("is-port-mirrored", properties.getIsPortMirrored(), errorDescriptions);
86
87         if (errorDescriptions.isEmpty()) {
88             return new PortDetailsOk(properties.getInterfaceId(), properties.getInterfaceName(), properties.getIsPortMirrored());
89         } else {
90             return new PortDetailsError(String.join(" ", errorDescriptions), rawPayload);
91         }
92     }
93
94     private static void describeIfNullOrEmpty(String name, Object value, List<String> errorDescriptions) {
95         if (value == null) {
96             errorDescriptions.add("Value of '" + name + "' is missing.");
97         } else if (value.toString().isEmpty()) {
98             errorDescriptions.add("Value of '" + name + "' is empty.");
99         }
100     }
101
102     private static Optional<List<PortDetails>> extractErrorResponseIfHttpError(AaiResponse aaiResponse, String rawPayload) {
103         if (aaiResponse.getHttpCode() != org.springframework.http.HttpStatus.OK.value()) {
104             final String errorMessage = aaiResponse.getErrorMessage();
105             return Optional.of(ImmutableList.of(new PortDetailsError(
106                     "Got " + aaiResponse.getHttpCode() + " from aai",
107                     errorMessage != null ? errorMessage : rawPayload)
108             ));
109         } else {
110             return Optional.empty();
111         }
112     }
113
114     public List<PortDetails> extractPortDetailsInternal(CustomQuerySimpleResult aaiGetPortsResponse, String rawPayload) {
115         List<SimpleResult> filteredResult = getFilteredPortList(aaiGetPortsResponse.getResults());
116
117         return filteredResult.stream()
118                 .map(SimpleResult::getProperties)
119                 .map(p -> extractPortDetailsFromProperties(p, rawPayload))
120                 .collect(Collectors.toList());
121     }
122
123     public List<SimpleResult> getFilteredPortList(List<SimpleResult> results) {
124         final String LINTERFACE = "l-interface";
125
126         final Predicate<SimpleResult> ifIsPort = (SimpleResult r) -> LINTERFACE.equals(r.getNodeType());
127         Predicate<SimpleResult> ifIsSource = getIsSourcePredicate();
128
129         return results.stream()
130                 .filter(ifIsPort)
131                 .filter(ifIsSource)
132                 .collect(Collectors.toList());
133     }
134
135     private Predicate<SimpleResult> getIsSourcePredicate() {
136         final String PORT_LABEL = "org.onap.relationships.inventory.Source";
137         return (SimpleResult r) -> r.getRelatedTo().stream()
138                 .anyMatch(relatedTo -> PORT_LABEL.equalsIgnoreCase(relatedTo.getRelationshipLabel()));
139     }
140
141     public List<PortDetails> extractPortDetails(AaiResponse<CustomQuerySimpleResult> aaiGetPortsResponse, String rawPayload) {
142         return extractErrorResponseIfHttpError(aaiGetPortsResponse, rawPayload).orElseGet(() -> extractPortDetailsInternal(aaiGetPortsResponse.getT(), rawPayload));
143
144     }
145
146 }