f9246f7435af039da25b50af21e44ff54cf6e76b
[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.rest.CommonRegisterResponse;
24 import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
25 import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
26 import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.gson.Gson;
31
32 public class ThirdpatySdncWrapper {
33
34   private static ThirdpatySdncWrapper thirdpatySdncWrapper;
35   private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
36
37   /**
38    * get ThirdpatySdncWrapper instance.
39    * @return ThirdpatySdnc manager wrapper instance
40    */
41   public static ThirdpatySdncWrapper getInstance() {
42     if (thirdpatySdncWrapper == null) {
43       thirdpatySdncWrapper = new ThirdpatySdncWrapper();
44     }
45     return thirdpatySdncWrapper;
46   }
47   
48   public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
49     CommonRegisterResponse result = new CommonRegisterResponse();
50     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
51     esrSdncDetail = ThirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
52     String sdncId = esrSdncDetail.getThirdpartySdncId();
53     try {
54       ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
55       result.setId(sdncId);
56       return Response.ok(result).build();
57     } catch (Exception e) {
58       e.printStackTrace();
59       LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
60       return Response.serverError().build();
61     }
62     
63   }
64
65   public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
66     //TODO
67     return Response.ok().build();
68   }
69   
70   public Response queryThirdpartySdncList() {
71     //TODO
72     ArrayList<ThirdpartySdncRegisterInfo> thirdpartySdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
73     return Response.ok(thirdpartySdncList).build();
74   }
75   
76   public Response queryThirdpartySdncById(String thirdpartySdncId) {
77     ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
78     thirdpartySdnc = querySdncDetail(thirdpartySdncId);
79     if(thirdpartySdnc != null) {
80       return Response.ok(thirdpartySdnc).build();
81     } else {
82       return Response.serverError().build();
83     }
84   }
85   
86   public Response delThirdpartySdnc(String thirdpartySdncId) {
87     //TODO
88     return Response.noContent().build();
89   }
90   
91   private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
92     ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
93     EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
94     try {
95       String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
96       LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
97       esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
98       sdncRegisterInfo = ThirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
99       return sdncRegisterInfo;
100     } catch (Exception e) {
101       e.printStackTrace();
102       LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
103       return null;
104     }
105   }
106 }