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