Merge "Modify JAVA Env Setting"
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / wrapper / ThirdpartySdncWrapper.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 import java.util.List;
20 import javax.ws.rs.core.Response;
21 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
22 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
23 import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
24 import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
25 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
26 import org.onap.aai.esr.exception.ExceptionUtil;
27 import org.onap.aai.esr.exception.ExtsysException;
28 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
29 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import com.google.gson.Gson;
33
34 public class ThirdpartySdncWrapper {
35
36     private static ThirdpartySdncWrapper thirdpatySdncWrapper;
37     private static final Logger LOG = LoggerFactory.getLogger(ThirdpartySdncWrapper.class);
38     private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();
39     private static ExternalSystemProxy externalSystemProxy = new ExternalSystemProxy();
40
41     /**
42      * get ThirdpatySdncWrapper instance.
43      * 
44      * @return ThirdpatySdnc manager wrapper instance
45      */
46     public static ThirdpartySdncWrapper getInstance() {
47         if (thirdpatySdncWrapper == null) {
48             thirdpatySdncWrapper = new ThirdpartySdncWrapper(externalSystemProxy);
49         }
50         return thirdpatySdncWrapper;
51     }
52     
53     public ThirdpartySdncWrapper(ExternalSystemProxy externalSystemProxy){
54         ThirdpartySdncWrapper.externalSystemProxy = externalSystemProxy;
55     }
56
57     public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
58         CommonRegisterResponse result = new CommonRegisterResponse();
59         EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
60         String sdncId = esrSdncDetail.getThirdpartySdncId();
61         try {
62             externalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
63             result.setId(sdncId);
64             return Response.ok(result).build();
65         } catch (ExtsysException e) {
66             LOG.error("Register thirdParty SDNC failed !", e);
67             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
68         }
69
70     }
71
72     public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
73         CommonRegisterResponse result = new CommonRegisterResponse();
74         EsrThirdpartySdncDetail originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
75         EsrThirdpartySdncDetail esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
76         String resourceVersion = originalEsrSdncDetail.getResourceVersion();
77         esrSdncDetail.setResourceVersion(resourceVersion);
78         esrSdncDetail.setThirdpartySdncId(sdncId);
79         EsrSystemInfo 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         List<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<>();
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 = querySdncDetail(thirdpartySdncId);
110         return Response.ok(thirdpartySdnc).build();
111     }
112
113     public Response delThirdpartySdnc(String thirdpartySdncId) {
114         EsrThirdpartySdncDetail thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
115         String resourceVersion = thirdpartySdncDetail.getResourceVersion();
116         try {
117             externalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
118             return Response.noContent().build();
119         } catch (ExtsysException e) {
120             LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
121                     + resourceVersion, e);
122             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
123         }
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 (ExtsysException e) {
136             LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e);
137             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
138         }
139     }
140
141     private List<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
142         List<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<>();
143         for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
144             String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
145             ThirdpartySdncRegisterInfo sdncInfo = querySdncDetail(sdncId);
146             if (sdncInfo != null) {
147                 sdncInfoList.add(sdncInfo);
148             }
149         }
150         return sdncInfoList;
151     }
152
153     private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail(String sdncId) {
154         EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
155         try {
156             String esrThirdpartySdncStr = externalSystemProxy.queryThirdpartySdncDetail(sdncId);
157             LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
158             esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
159         } catch (ExtsysException e) {
160             LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e);
161             throw ExceptionUtil.buildExceptionResponse(e.getMessage());
162         }
163         return esrSdncDetail;
164     }
165
166 }