Fix the java code style issue.
[aai/esr-server.git] / esr-mgr / src / main / java / org / onap / aai / esr / util / EmsManagerUtil.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.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.onap.aai.esr.common.SystemType;
21 import org.onap.aai.esr.entity.aai.EsrSystemInfo;
22 import org.onap.aai.esr.entity.aai.EsrEmsDetail;
23 import org.onap.aai.esr.entity.rest.AlarmAddr;
24 import org.onap.aai.esr.entity.rest.EmsRegisterInfo;
25 import org.onap.aai.esr.entity.rest.FtpAddr;
26
27 public class EmsManagerUtil {
28     private static ExtsysUtil extsysUtil = new ExtsysUtil();
29
30     public EsrEmsDetail emsRegisterInfo2EsrEms(EmsRegisterInfo emsRegisterInfo) {
31         EsrEmsDetail esrEms = new EsrEmsDetail();
32         esrEms.setEmsId(extsysUtil.generateId());
33         List<EsrSystemInfo> authInfos = getAuthInfosFromRegisterData(emsRegisterInfo);
34         esrEms.setEsrSystemInfoList(extsysUtil.getEsrSystemInfoListFromAuthInfoList(authInfos));
35         return esrEms;
36     }
37
38     private List<EsrSystemInfo> getAuthInfosFromRegisterData(EmsRegisterInfo emsRegisterInfo) {
39         List<EsrSystemInfo> authInfos = new ArrayList<>();
40         EsrSystemInfo resouceAuthInfo = getAuthInfoFromFtpAddr(emsRegisterInfo, SystemType.EMS_RESOUCE.toString());
41         EsrSystemInfo performanceAuthInfo =
42                 getAuthInfoFromFtpAddr(emsRegisterInfo, SystemType.EMS_PERFORMANCE.toString());
43         EsrSystemInfo alarmAuthInfo = getAuthInfoFromAlarmAddr(emsRegisterInfo);
44         authInfos.add(resouceAuthInfo);
45         authInfos.add(performanceAuthInfo);
46         authInfos.add(alarmAuthInfo);
47         return authInfos;
48     }
49
50     private EsrSystemInfo getAuthInfoFromFtpAddr(EmsRegisterInfo emsRegisterInfo, String systemType) {
51         EsrSystemInfo authInfo = new EsrSystemInfo();
52         FtpAddr ftpAddr = new FtpAddr();
53         if (systemType.equals(SystemType.EMS_RESOUCE.toString())) {
54             ftpAddr = emsRegisterInfo.getResourceAddr();
55         } else if (systemType.equals(SystemType.EMS_PERFORMANCE.toString())) {
56             ftpAddr = emsRegisterInfo.getPerformanceAddr();
57         }
58         authInfo.setType(ftpAddr.getFtptype());
59         authInfo.setIpAddress(ftpAddr.getIp());
60         authInfo.setPort(ftpAddr.getPort());
61         authInfo.setUserName(ftpAddr.getUser());
62         authInfo.setPassword(ftpAddr.getPassword());
63         authInfo.setRemotePath(ftpAddr.getRemotepath());
64         authInfo.setPassive(ftpAddr.getPassive());
65         authInfo.setEsrSystemInfoId(extsysUtil.generateId());
66         authInfo.setSystemType(systemType);
67         authInfo.setSystemName(emsRegisterInfo.getName());
68         authInfo.setVendor(emsRegisterInfo.getVendor());
69         authInfo.setVersion(emsRegisterInfo.getVersion());
70         return authInfo;
71     }
72
73     private EsrSystemInfo getAuthInfoFromAlarmAddr(EmsRegisterInfo emsRegisterInfo) {
74         EsrSystemInfo authInfo = new EsrSystemInfo();
75         AlarmAddr alarmAddr = emsRegisterInfo.getAlarmAddr();
76         authInfo.setEsrSystemInfoId(extsysUtil.generateId());
77         authInfo.setIpAddress(alarmAddr.getIp());
78         authInfo.setPort(alarmAddr.getPort());
79         authInfo.setUserName(alarmAddr.getUser());
80         authInfo.setPassword(alarmAddr.getPassword());
81         authInfo.setSystemType(SystemType.EMS_ALARM.toString());
82         authInfo.setSystemName(emsRegisterInfo.getName());
83         authInfo.setVendor(emsRegisterInfo.getVendor());
84         authInfo.setVersion(emsRegisterInfo.getVersion());
85         return authInfo;
86     }
87
88     public EmsRegisterInfo EsrEms2EmsRegisterInfo(EsrEmsDetail esrEms) {
89         EmsRegisterInfo emsRegisterInfo = new EmsRegisterInfo();
90         EsrSystemInfo authInfo = new EsrSystemInfo();
91         List<EsrSystemInfo> esrSystemInfo = esrEms.getEsrSystemInfoList().getEsrSystemInfo();
92         emsRegisterInfo.setEmsId(esrEms.getEmsId());
93
94         for (int i = 0; i < esrSystemInfo.size(); i++) {
95             authInfo = esrSystemInfo.get(i);
96             if (authInfo.getSystemType().equals(SystemType.EMS_RESOUCE.toString())) {
97                 FtpAddr resourceAddr = new FtpAddr();
98                 resourceAddr = getFtpAddrFromAuthInfo(authInfo);
99                 emsRegisterInfo.setResourceAddr(resourceAddr);
100             } else if (authInfo.getSystemType().equals(SystemType.EMS_PERFORMANCE.toString())) {
101                 FtpAddr performanceAddr = new FtpAddr();
102                 performanceAddr = getFtpAddrFromAuthInfo(authInfo);
103                 emsRegisterInfo.setPerformanceAddr(performanceAddr);
104             } else if (authInfo.getSystemType().equals(SystemType.EMS_ALARM.toString())) {
105                 AlarmAddr alarmAddr = new AlarmAddr();
106                 alarmAddr = getAlarmAddrFromAuthInfo(authInfo);
107                 emsRegisterInfo.setAlarmAddr(alarmAddr);
108             }
109         }
110         emsRegisterInfo.setName(authInfo.getSystemName());
111         emsRegisterInfo.setVendor(authInfo.getVendor());
112         emsRegisterInfo.setVersion(authInfo.getVersion());
113         return emsRegisterInfo;
114     }
115
116     private FtpAddr getFtpAddrFromAuthInfo(EsrSystemInfo authInfo) {
117         FtpAddr ftpAddr = new FtpAddr();
118         ftpAddr.setFtptype(authInfo.getType());
119         ftpAddr.setIp(authInfo.getIpAddress());
120         ftpAddr.setPassive(authInfo.getPassive());
121         ftpAddr.setPassword(authInfo.getPassword());
122         ftpAddr.setPort(authInfo.getPort());
123         ftpAddr.setRemotepath(authInfo.getRemotePath());
124         ftpAddr.setUser(authInfo.getUserName());
125         return ftpAddr;
126     }
127
128     private AlarmAddr getAlarmAddrFromAuthInfo(EsrSystemInfo authInfo) {
129         AlarmAddr alarmAddr = new AlarmAddr();
130         alarmAddr.setIp(authInfo.getIpAddress());
131         alarmAddr.setPassword(authInfo.getPassword());
132         alarmAddr.setPort(authInfo.getPort());
133         alarmAddr.setUser(authInfo.getUserName());
134         return alarmAddr;
135     }
136 }