20b8eab345355da7b53d491b8adbaaf2e4157e15
[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.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.externalservice.aai.ExternalSystemProxy;
27 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.gson.Gson;
32
33 public class ThirdpatySdncWrapper {
34
35   private static ThirdpatySdncWrapper thirdpatySdncWrapper;
36   private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
37
38   /**
39    * get ThirdpatySdncWrapper instance.
40    * @return ThirdpatySdnc manager wrapper instance
41    */
42   public static ThirdpatySdncWrapper getInstance() {
43     if (thirdpatySdncWrapper == null) {
44       thirdpatySdncWrapper = new ThirdpatySdncWrapper();
45     }
46     return thirdpatySdncWrapper;
47   }
48   
49   public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
50     CommonRegisterResponse result = new CommonRegisterResponse();
51     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
52     esrSdncDetail = ThirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
53     String sdncId = esrSdncDetail.getThirdpartySdncId();
54     try {
55       ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
56       result.setId(sdncId);
57       return Response.ok(result).build();
58     } catch (Exception e) {
59       e.printStackTrace();
60       LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
61       return Response.serverError().build();
62     }
63     
64   }
65
66   public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
67     //TODO
68     return Response.ok().build();
69   }
70   
71   public Response queryThirdpartySdncList() {
72     ArrayList<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
73     EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
74     try {
75       String esrSdncStr = ExternalSystemProxy.querySdncList();
76       esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
77       LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
78       sdncList = getSdncDetailList(esrSdnc);
79       return Response.ok(sdncList).build();
80     } catch (Exception e) {
81       e.printStackTrace();
82       LOG.error("Query thirdparty SDNC list failed !");
83       return Response.serverError().build();
84     }
85   }
86   
87   public Response queryThirdpartySdncById(String thirdpartySdncId) {
88     ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
89     thirdpartySdnc = querySdncDetail(thirdpartySdncId);
90     if(thirdpartySdnc != null) {
91       return Response.ok(thirdpartySdnc).build();
92     } else {
93       return Response.serverError().build();
94     }
95   }
96   
97   public Response delThirdpartySdnc(String thirdpartySdncId) {
98     //TODO
99     return Response.noContent().build();
100   }
101   
102   private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
103     ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
104     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
105     try {
106       String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
107       LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
108       esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
109       sdncRegisterInfo = ThirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
110       return sdncRegisterInfo;
111     } catch (Exception e) {
112       e.printStackTrace();
113       LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
114       return null;
115     }
116   }
117   
118   private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
119     ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
120     ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
121     for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
122       String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
123       sdncInfo = querySdncDetail(sdncId);
124       if (sdncInfo != null) {
125         sdncInfoList.add(sdncInfo);
126       }
127     }
128     return sdncInfoList;
129   }
130 }