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