Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM Core
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 Netsia
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.external.aai;
|
| 24 |
|
| 25 | import lombok.extern.slf4j.Slf4j;
|
| 26 | import org.onap.osam.common.exception.BadFormatException;
|
| 27 | import org.onap.osam.common.exception.InvalidOperationException;
|
| 28 | import org.onap.osam.common.exception.NotFoundException;
|
| 29 | import org.onap.osam.external.aai.config.AaiConfig;
|
| 30 | import org.onap.osam.external.aai.exception.ExternalSystemException;
|
| 31 | import org.onap.osam.external.aai.model.PNF;
|
| 32 | import org.onap.osam.external.aai.util.AaiHeaderUtil;
|
| 33 | import org.springframework.beans.factory.annotation.Autowired;
|
| 34 | import org.springframework.http.HttpStatus;
|
| 35 | import org.springframework.stereotype.Service;
|
| 36 | import org.springframework.web.reactive.function.client.ClientResponse;
|
| 37 |
|
| 38 | import javax.net.ssl.SSLException;
|
| 39 |
|
| 40 |
|
| 41 | /**
|
| 42 | * Created by cemturker on 12.04.2018.
|
| 43 | */
|
| 44 |
|
| 45 | @Service
|
| 46 | @Slf4j
|
| 47 | public class AaiClientImpl implements AaiClient {
|
| 48 |
|
| 49 | private AaiConfig aaiConfig;
|
| 50 |
|
| 51 | @Autowired
|
| 52 | public AaiClientImpl(AaiConfig aaiConfig) {
|
| 53 | this.aaiConfig = aaiConfig;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public PNF queryPnf(String name) {
|
| 58 | try {
|
| 59 | ClientResponse response = AaiWebClient.webClient(aaiConfig).build().queryPNF(name);
|
| 60 | commonErrorCheck(name,response);
|
| 61 | log.info("Pnf query response code {} for {} id",response.statusCode(),name);
|
| 62 | if (response.statusCode().is2xxSuccessful()) {
|
| 63 | return AaiHeaderUtil.convertToPnf(response.bodyToMono(String.class).block());
|
| 64 | }
|
| 65 | } catch (SSLException e) {
|
| 66 | log.error("",e);
|
| 67 | }
|
| 68 | throw new InvalidOperationException("");
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public void putPnf(PNF pnf) {
|
| 73 | try {
|
| 74 | ClientResponse response = AaiWebClient.webClient(aaiConfig).build().putPNF(pnf);
|
| 75 | commonErrorCheck(pnf,response);
|
| 76 | log.info("Pnf query response code {} for {} ",response.statusCode(),pnf);
|
| 77 | if (response.statusCode().is2xxSuccessful()) {
|
| 78 | return;
|
| 79 | }
|
| 80 | } catch (SSLException e) {
|
| 81 | log.error("",e);
|
| 82 | }
|
| 83 | throw new InvalidOperationException("");
|
| 84 | }
|
| 85 |
|
| 86 | private <T> void commonErrorCheck(T t, ClientResponse response){
|
| 87 | if (response.statusCode().is5xxServerError()) {
|
| 88 | throw new ExternalSystemException("Aai error code:"+response.statusCode().value());
|
| 89 | }
|
| 90 | if (HttpStatus.BAD_REQUEST.equals(response.statusCode())) {
|
| 91 | throw new BadFormatException("Bad format exception is received from AAI");
|
| 92 | }
|
| 93 | if (HttpStatus.NOT_FOUND.equals(response.statusCode())) {
|
| 94 | throw new NotFoundException(t+" is not found in AAI");
|
| 95 | }
|
| 96 | }
|
| 97 | }
|