Fix the java code style issue.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / ThirdpartySdncWrapper.java
1 /**
2  * Copyright 2017 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.aai.esr.wrapper;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import javax.ws.rs.core.Response;
21 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
22 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
23 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
24 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
25 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
26 import org.onap.aai.esr.exception.ExceptionUtil;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
29 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
33
34 public class ThirdpartySdncWrapper {
35
36     private static ThirdpartySdncWrapper thirdpatySdncWrapper;
37     private static final Logger LOG = LoggerFactory.getLogger(ThirdpartySdncWrapper.class);
38     private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
39
40     /**
41      * get ThirdpatySdncWrapper instance.
42      * 
43      * @return ThirdpatySdnc manager wrapper instance
44      */
45     public static ThirdpartySdncWrapper getInstance() {
46         if (thirdpatySdncWrapper == null) {
47             thirdpatySdncWrapper = new ThirdpartySdncWrapper();
48         }
49         return thirdpatySdncWrapper;
50     }
51
52     public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
53         CommonRegisterResponse result = new CommonRegisterResponse();
54         EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
55         String sdncId = esrSdncDetail.getThirdpartySdncId();
56         try {
57             ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
58             result.setId(sdncId);
59             return Response.ok(result).build();
60         } catch (ExtsysException e) {
61             LOG.error("Register thirdParty SDNC failed !", e);
62             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
63         }
64
65     }
66
67     public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
68         CommonRegisterResponse result = new CommonRegisterResponse();
69         EsrThirdpartySdncDetail originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
70         EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
71         String resourceVersion = originalEsrSdncDetail.getResourceVersion();
72         esrSdncDetail.setResourceVersion(resourceVersion);
73         esrSdncDetail.setThirdpartySdncId(sdncId);
74         EsrSystemInfo originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
75         esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
76                 .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
77         esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
78                 .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
79         try {
80             ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
81             result.setId(sdncId);
82             return Response.ok(result).build();
83         } catch (ExtsysException e) {
84             LOG.error("Update VNFM failed !", e);
85             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
86         }
87     }
88
89     public Response queryThirdpartySdncList() {
90         List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
91         EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
92         try {
93             String esrSdncStr = ExternalSystemProxy.querySdncList();
94             esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
95             LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
96             sdncList = getSdncDetailList(esrSdnc);
97         } catch (ExtsysException e) {
98             LOG.error("Query thirdparty SDNC list failed !", e);
99         }
100         return Response.ok(sdncList).build();
101     }
102
103     public Response queryThirdpartySdncById(String thirdpartySdncId) {
104         ThirdpartySdncRegisterInfo thirdpartySdnc = querySdncDetail(thirdpartySdncId);
105         return Response.ok(thirdpartySdnc).build();
106     }
107
108     public Response delThirdpartySdnc(String thirdpartySdncId) {
109         EsrThirdpartySdncDetail thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
110         String resourceVersion = thirdpartySdncDetail.getResourceVersion();
111         try {
112             ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
113             return Response.noContent().build();
114         } catch (ExtsysException e) {
115             LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
116                     + resourceVersion, e);
117             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
118         }
119     }
120
121     private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
122         ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
123         EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
124         try {
125             String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
126             LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
127             esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
128             sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
129             return sdncRegisterInfo;
130         } catch (ExtsysException e) {
131             LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
132             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
133         }
134     }
135
136     private List<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
137         List<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<>();
138         for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
139             String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
140             ThirdpartySdncRegisterInfo sdncInfo = querySdncDetail(sdncId);
141             if (sdncInfo != null) {
142                 sdncInfoList.add(sdncInfo);
143             }
144         }
145         return sdncInfoList;
146     }
147
148     private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail(String sdncId) {
149         EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
150         try {
151             String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
152             LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
153             esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
154         } catch (ExtsysException e) {
155             LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
156             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
157         }
158         return esrSdncDetail;
159     }
160
161 }