fd7dc438735b8c3c9a060728631900bab03bf537
[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.exception.ExceptionUtil;
28 import org.onap.aai.esr.exception.ExtsysException;
29 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
30 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
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 (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 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 (ExtsysException e) {
89       LOG.error("Update VNFM failed !" , e);
90       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
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     } catch (ExtsysException e) {
103       LOG.error("Query thirdparty SDNC list failed !", e);
104     }
105     return Response.ok(sdncList).build();
106   }
107   
108   public Response queryThirdpartySdncById(String thirdpartySdncId) {
109     ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
110     thirdpartySdnc = querySdncDetail(thirdpartySdncId);
111     return Response.ok(thirdpartySdnc).build();
112   }
113   
114   public Response delThirdpartySdnc(String thirdpartySdncId) {
115     EsrThirdpartySdncDetail thirdpartySdncDetail = new EsrThirdpartySdncDetail();
116     thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
117     String resourceVersion = thirdpartySdncDetail.getResourceVersion();
118     try {
119       ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
120       return Response.noContent().build();
121     } catch (ExtsysException e) {
122       LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId
123           + "resouce-version:" + resourceVersion, e);
124       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
125     }
126   }
127   
128   private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
129     ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
130     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
131     try {
132       String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
133       LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
134       esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
135       sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
136       return sdncRegisterInfo;
137     } catch (ExtsysException e) {
138       LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
139       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
140     }
141   }
142   
143   private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
144     ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
145     ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
146     for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
147       String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
148       sdncInfo = querySdncDetail(sdncId);
149       if (sdncInfo != null) {
150         sdncInfoList.add(sdncInfo);
151       }
152     }
153     return sdncInfoList;
154   }
155   
156   private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
157     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
158     try {
159       String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
160       LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
161       esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
162     } catch (ExtsysException e) {
163       LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
164       throw ExceptionUtil.buildExceptionResponse(e.getMessage());
165     }
166     return esrSdncDetail;
167   }
168   
169 }