aba13fb4063e0516778af2c474d29e630bf41a9a
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / register / RegistrationCreator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  */
20
21 package org.onap.aaf.cadi.register;
22
23 import java.net.UnknownHostException;
24 import java.util.List;
25
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.aaf.Defaults;
29 import org.onap.aaf.cadi.config.Config;
30 import org.onap.aaf.cadi.config.RegistrationPropHolder;
31 import org.onap.aaf.cadi.util.Split;
32
33 import locate.v1_0.MgmtEndpoint;
34 import locate.v1_0.MgmtEndpoints;
35
36 public class RegistrationCreator {
37         private Access access;
38     
39     public RegistrationCreator(Access access) {
40         this.access = access;
41     }
42     
43     public MgmtEndpoints create(final int port) throws CadiException {
44         MgmtEndpoints me = new MgmtEndpoints();
45         List<MgmtEndpoint> lme = me.getMgmtEndpoint();
46         MgmtEndpoint defData = null;
47         MgmtEndpoint locate = null;
48
49         try {
50                 String dot_le;
51                 String version=null;
52                 
53                 RegistrationPropHolder ph = new RegistrationPropHolder(access, port);
54                 
55                 // Now, loop through by Container
56                 for(String le : Split.splitTrim(',', ph.lcontainer)) {
57                         if(le.isEmpty()) {
58                                 dot_le = le;
59                         } else {
60                                 dot_le = "."+le;
61                         }
62
63                         for(String entry : Split.splitTrim(',', ph.lentries)) {
64                                 if(defData==null) {
65                                         defData = locate = new MgmtEndpoint();
66
67                                         version = access.getProperty(Config.AAF_LOCATOR_VERSION, Defaults.AAF_VERSION);
68                                         locate.setProtocol(access.getProperty(Config.AAF_LOCATOR_PROTOCOL,null));
69                                         List<String> ls = locate.getSubprotocol();
70                                         for(String sp : Split.splitTrim(',', access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL,""))) {
71                                                 ls.add(sp);     
72                                         }
73                                         locate.setLatitude(ph.latitude);
74                                         locate.setLongitude(ph.longitude);
75
76                                 } else {
77                                         locate = copy(defData);
78                                 }
79                                 
80                                 locate.setName(ph.getEntryName(entry,dot_le));
81                                 locate.setHostname(ph.getEntryFQDN(entry,dot_le));
82                                 locate.setPort(ph.getEntryPort(dot_le));
83                                 
84                                 String specificVersion = access.getProperty(Config.AAF_LOCATOR_VERSION + dot_le,null);
85                                 if(specificVersion == null && locate == defData) {
86                                         specificVersion = version;
87                                 }
88                                 if(specificVersion!=null) {
89                                         String split[] = Split.splitTrim('.', specificVersion);
90                                         locate.setPkg(split.length>3?Integer.parseInt(split[3]):0);
91                                         locate.setPatch(split.length>2?Integer.parseInt(split[2]):0);
92                                         locate.setMinor(split.length>1?Integer.parseInt(split[1]):0);
93                                         locate.setMajor(split.length>0?Integer.parseInt(split[0]):0);
94                                 }
95
96                                 String protocol = access.getProperty(Config.AAF_LOCATOR_PROTOCOL + dot_le, null);
97                                 if (protocol!=null) {
98                                         locate.setProtocol(protocol);
99                                         String subprotocols = access.getProperty(Config.AAF_LOCATOR_SUBPROTOCOL + dot_le, null);
100                                         if(subprotocols!=null) {
101                                                 List<String> ls = locate.getSubprotocol();
102                                                 for (String s : Split.split(',', subprotocols)) {
103                                                         ls.add(s);
104                                                 }
105                                         }
106                                 }
107                                 lme.add(locate);
108                         }
109                 }
110         } catch (NumberFormatException | UnknownHostException e) {
111                 throw new CadiException("Error extracting Data from Properties for Registrar",e);
112         }
113
114         return me;
115     }
116         
117     private MgmtEndpoint copy(MgmtEndpoint mep) {
118                 MgmtEndpoint out = new MgmtEndpoint();
119                 out.setName(mep.getName());
120                 out.setHostname(mep.getHostname());
121                 out.setLatitude(mep.getLatitude());
122                 out.setLongitude(mep.getLongitude());
123                 out.setMajor(mep.getMajor());
124                 out.setMinor(mep.getMinor());
125                 out.setPkg(mep.getPkg());
126                 out.setPatch(mep.getPatch());
127                 out.setPort(mep.getPort());
128                 out.setProtocol(mep.getProtocol());
129                 out.getSpecialPorts().addAll(mep.getSpecialPorts());
130                 out.getSubprotocol().addAll(mep.getSubprotocol());
131                 return out;
132         }
133 }