summaryrefslogtreecommitdiffstats
path: root/esr-mgr/src/main/java/org/onap/aai/esr/wrapper/ThirdpatySdncWrapper.java
blob: effc244634263b5ddaf0c769a6f8276dc02027e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * Copyright 2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.onap.aai.esr.wrapper;

import java.util.ArrayList;

import javax.ws.rs.core.Response;

import org.onap.aai.esr.entity.aai.EsrSystemInfo;
import org.onap.aai.esr.entity.aai.EsrThirdpartySdncDetail;
import org.onap.aai.esr.entity.aai.EsrThirdpartySdncList;
import org.onap.aai.esr.entity.aai.EsrVnfmDetail;
import org.onap.aai.esr.entity.rest.CommonRegisterResponse;
import org.onap.aai.esr.entity.rest.ThirdpartySdncRegisterInfo;
import org.onap.aai.esr.externalservice.aai.ExternalSystemProxy;
import org.onap.aai.esr.util.ThirdpartySdncManagerUtil;
import org.onap.aai.esr.util.VnfmManagerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;

public class ThirdpatySdncWrapper {

  private static ThirdpatySdncWrapper thirdpatySdncWrapper;
  private static final Logger LOG = LoggerFactory.getLogger(ThirdpatySdncWrapper.class);
  private static ThirdpartySdncManagerUtil thirdpartySdncManagerUtil = new ThirdpartySdncManagerUtil();

  /**
   * get ThirdpatySdncWrapper instance.
   * @return ThirdpatySdnc manager wrapper instance
   */
  public static ThirdpatySdncWrapper getInstance() {
    if (thirdpatySdncWrapper == null) {
      thirdpatySdncWrapper = new ThirdpatySdncWrapper();
    }
    return thirdpatySdncWrapper;
  }
  
  public Response registerThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc) {
    CommonRegisterResponse result = new CommonRegisterResponse();
    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
    esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
    String sdncId = esrSdncDetail.getThirdpartySdncId();
    try {
      ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
      result.setId(sdncId);
      return Response.ok(result).build();
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error("Register thirdParty SDNC failed !" + e.getMessage());
      return Response.serverError().build();
    }
    
  }

  public Response updateThirdpartySdnc(ThirdpartySdncRegisterInfo thirdpartySdnc, String sdncId) {
    CommonRegisterResponse result = new CommonRegisterResponse();
    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
    EsrThirdpartySdncDetail originalEsrSdncDetail = new EsrThirdpartySdncDetail();
    EsrSystemInfo originalEsrSystemInfo = new EsrSystemInfo();
    originalEsrSdncDetail = queryEsrThirdpartySdncDetail(sdncId);
    esrSdncDetail = thirdpartySdncManagerUtil.sdncRegisterInfo2EsrSdnc(thirdpartySdnc);
    String resourceVersion = originalEsrSdncDetail.getResourceVersion();
    esrSdncDetail.setResourceVersion(resourceVersion);
    esrSdncDetail.setThirdpartySdncId(sdncId);
    originalEsrSystemInfo = originalEsrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0);
    esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
        .setEsrSystemInfoId(originalEsrSystemInfo.getEsrSystemInfoId());
    esrSdncDetail.getEsrSystemInfoList().getEsrSystemInfo().get(0)
        .setResouceVersion(originalEsrSystemInfo.getResouceVersion());
    try {
      ExternalSystemProxy.registerSdnc(sdncId, esrSdncDetail);
      result.setId(sdncId);
      return Response.ok(result).build();
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error("Update VNFM failed !" + e.getMessage());
      return Response.serverError().build();
    }
  }
  
  public Response queryThirdpartySdncList() {
    ArrayList<ThirdpartySdncRegisterInfo> sdncList = new ArrayList<ThirdpartySdncRegisterInfo>();
    EsrThirdpartySdncList esrSdnc = new EsrThirdpartySdncList();
    try {
      String esrSdncStr = ExternalSystemProxy.querySdncList();
      esrSdnc = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncList.class);
      LOG.info("Response from AAI by query thirdparty SDNC list: " + esrSdnc);
      sdncList = getSdncDetailList(esrSdnc);
      return Response.ok(sdncList).build();
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error("Query thirdparty SDNC list failed !");
      return Response.ok(sdncList).build();
    }
  }
  
  public Response queryThirdpartySdncById(String thirdpartySdncId) {
    ThirdpartySdncRegisterInfo thirdpartySdnc = new ThirdpartySdncRegisterInfo();
    thirdpartySdnc = querySdncDetail(thirdpartySdncId);
    if(thirdpartySdnc != null) {
      return Response.ok(thirdpartySdnc).build();
    } else {
      return Response.ok(thirdpartySdnc).build();
    }
  }
  
  public Response delThirdpartySdnc(String thirdpartySdncId) {
    EsrThirdpartySdncDetail thirdpartySdncDetail = new EsrThirdpartySdncDetail();
    thirdpartySdncDetail = queryEsrThirdpartySdncDetail(thirdpartySdncId);
    String resourceVersion = thirdpartySdncDetail.getResourceVersion();
    if (resourceVersion != null) {
      try {
        ExternalSystemProxy.deleteThirdpartySdnc(thirdpartySdncId, resourceVersion);
        return Response.noContent().build();
      } catch (Exception e) {
        e.printStackTrace();
        LOG.error("Delete VNFM from A&AI failed! thirdparty SDNC ID: " + thirdpartySdncId + "resouce-version:"
            + resourceVersion, e.getMessage());
        return Response.serverError().build();
      }
    } else {
      LOG.error("resouce-version is null ! Can not delete resouce from A&AI. ");
      return Response.serverError().build();
    }
  }
  
  private ThirdpartySdncRegisterInfo querySdncDetail(String sdncId) {
    ThirdpartySdncRegisterInfo sdncRegisterInfo = new ThirdpartySdncRegisterInfo();
    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
    try {
      String esrSdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
      LOG.info("Response from AAI by query thirdparty SDNC: " + esrSdncStr);
      esrSdncDetail = new Gson().fromJson(esrSdncStr, EsrThirdpartySdncDetail.class);
      sdncRegisterInfo = thirdpartySdncManagerUtil.esrSdnc2SdncRegisterInfo(esrSdncDetail);
      return sdncRegisterInfo;
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error("Query VNFM detail failed! thirdpaty SDNC ID: " + sdncId, e.getMessage());
      return null;
    }
  }
  
  private ArrayList<ThirdpartySdncRegisterInfo> getSdncDetailList(EsrThirdpartySdncList esrThirdPartySdnc) {
    ArrayList<ThirdpartySdncRegisterInfo> sdncInfoList = new ArrayList<ThirdpartySdncRegisterInfo>();
    ThirdpartySdncRegisterInfo sdncInfo = new ThirdpartySdncRegisterInfo();
    for (int i = 0; i < esrThirdPartySdnc.getEsrThirdpartySdnc().size(); i++) {
      String sdncId = esrThirdPartySdnc.getEsrThirdpartySdnc().get(i).getThirdpartySdncId();
      sdncInfo = querySdncDetail(sdncId);
      if (sdncInfo != null) {
        sdncInfoList.add(sdncInfo);
      }
    }
    return sdncInfoList;
  }
  
  private EsrThirdpartySdncDetail queryEsrThirdpartySdncDetail (String sdncId) {
    EsrThirdpartySdncDetail esrSdncDetail = new EsrThirdpartySdncDetail();
    try {
      String esrThirdpartySdncStr = ExternalSystemProxy.queryThirdpartySdncDetail(sdncId);
      LOG.info("Response from AAI by query thirdparty SDNC: " + esrThirdpartySdncStr);
      esrSdncDetail = new Gson().fromJson(esrThirdpartySdncStr, EsrThirdpartySdncDetail.class);
    } catch (Exception e) {
      e.printStackTrace();
      LOG.error("Query VNFM detail failed! VNFM ID: " + sdncId, e.getMessage());
    }
    return esrSdncDetail;
  }
  
}