Fix the nonstandard coding.
[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
21 import javax.ws.rs.core.Response;
22
23 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
24 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
25 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
26 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
27 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
28 import org.onap.aai.esr.exception.ExceptionUtil;
29 import org.onap.aai.esr.exception.ExtsysException;
30 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
31 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import com.google.gson.Gson;
36
37 public class ThirdpartySdncWrapper {
38
39   private static ThirdpartySdncWrapper thirdpatySdncWrapper;
40   private static final Logger LOG = LoggerFactory.getLogger(ThirdpartySdncWrapper.class);
41   private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
42
43   /**
44    * get ThirdpatySdncWrapper instance.
45    * @return ThirdpatySdnc manager wrapper instance
46    */
47   public static ThirdpartySdncWrapper getInstance() {
48     if (thirdpatySdncWrapper == null) {
49       thirdpatySdncWrapper = new ThirdpartySdncWrapper();
50     }
51     return thirdpatySdncWrapper;
52   }
53   
54   public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
55     CommonRegisterResponse result = new CommonRegisterResponse();
56     EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
57     String sdncId = esrSdncDetail.getThirdpartySdncId();
58     try {
59       ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
60       result.setId(sdncId);
61       return Response.ok(result).build();
62     } catch (ExtsysException e) {
63       LOG.error("Register thirdParty SDNC failed !" , e);
64       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
65     }
66     
67   }
68
69   public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
70     CommonRegisterResponse result = new CommonRegisterResponse();
71     EsrThirdpartySdncDetail originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
72     EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
73     String resourceVersion = originalEsrSdncDetail.getResourceVersion();
74     esrSdncDetail.setResourceVersion(resourceVersion);
75     esrSdncDetail.setThirdpartySdncId(sdncId);
76     EsrSystemInfo originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
77     esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
78         .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
79     esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
80         .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
81     try {
82       ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
83       result.setId(sdncId);
84       return Response.ok(result).build();
85     } catch (ExtsysException e) {
86       LOG.error("Update VNFM failed !" , e);
87       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
88     }
89   }
90   
91   public Response queryThirdpartySdncList() {
92     List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
93     EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
94     try {
95       String esrSdncStr = ExternalSystemProxy.querySdncList();
96       esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
97       LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
98       sdncList = getSdncDetailList(esrSdnc);
99     } catch (ExtsysException e) {
100       LOG.error("Query thirdparty SDNC list failed !", e);
101     }
102     return Response.ok(sdncList).build();
103   }
104   
105   public Response queryThirdpartySdncById(String thirdpartySdncId) {
106     ThirdpartySdncRegisterInfo thirdpartySdnc = querySdncDetail(thirdpartySdncId);
107     return Response.ok(thirdpartySdnc).build();
108   }
109   
110   public Response delThirdpartySdnc(String thirdpartySdncId) {
111     EsrThirdpartySdncDetail thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
112     String resourceVersion = thirdpartySdncDetail.getResourceVersion();
113     try {
114       ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
115       return Response.noContent().build();
116     } catch (ExtsysException e) {
117       LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId
118           + "resouce-version:" + resourceVersion, e);
119       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
120     }
121   }
122   
123   private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
124     ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
125     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
126     try {
127       String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
128       LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
129       esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
130       sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
131       return sdncRegisterInfo;
132     } catch (ExtsysException e) {
133       LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
134       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
135     }
136   }
137   
138   private List<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
139     List<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<>();
140     for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
141       String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
142       ThirdpartySdncRegisterInfo sdncInfo = querySdncDetail(sdncId);
143       if (sdncInfo != null) {
144         sdncInfoList.add(sdncInfo);
145       }
146     }
147     return sdncInfoList;
148   }
149   
150   private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
151     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
152     try {
153       String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
154       LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
155       esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
156     } catch (ExtsysException e) {
157       LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
158       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
159     }
160     return esrSdncDetail;
161   }
162   
163 }