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