Realize the update thirdparty SDNC API
[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     //TODO
123     return Response.noContent().build();
124   }
125   
126   private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
127     ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
128     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
129     try {
130       String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
131       LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
132       esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
133       sdncRegisterInfo = ThirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
134       return sdncRegisterInfo;
135     } catch (Exception e) {
136       e.printStackTrace();
137       LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
138       return null;
139     }
140   }
141   
142   private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
143     ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
144     ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
145     for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
146       String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
147       sdncInfo = querySdncDetail(sdncId);
148       if (sdncInfo != null) {
149         sdncInfoList.add(sdncInfo);
150       }
151     }
152     return sdncInfoList;
153   }
154   
155   private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
156     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
157     try {
158       String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
159       LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
160       esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
161     } catch (Exception e) {
162       e.printStackTrace();
163       LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e.getMessage());
164     }
165     return esrSdncDetail;
166   }
167   
168 }