61178951f3d12b5fd43ffab7c827bd2f654de44b
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / mapper / Mapper_1_1.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
22 package org.onap.aaf.auth.locate.mapper;
23
24 import java.util.List;
25
26 import org.onap.aaf.auth.dao.cass.LocateDAO.Data;
27 import org.onap.aaf.auth.layer.Result;
28 import org.onap.aaf.cadi.util.Vars;
29 import org.onap.aaf.misc.env.util.Split;
30
31 import locate.v1_0.Endpoint;
32 import locate.v1_0.Endpoints;
33 import locate.v1_0.MgmtEndpoint;
34 import locate.v1_0.MgmtEndpoints;
35 import locate_local.v1_0.Error;
36 import locate_local.v1_0.InRequest;
37 import locate_local.v1_0.Out;
38 import locate_local.v1_1.Configuration;
39
40 public class Mapper_1_1 implements Mapper<InRequest,Out,Endpoints,MgmtEndpoints,Configuration,Error> {
41         
42         @Override
43         public Class<?> getClass(API api) {
44                 switch(api) {
45                         case IN_REQ: return InRequest.class;
46                         case OUT: return Out.class;
47                         case ERROR: return Error.class;
48                         case VOID: return Void.class;
49                         case ENDPOINTS: return Endpoints.class;
50                         case MGMT_ENDPOINTS: return MgmtEndpoints.class;
51                         case CONFIG: return Configuration.class;
52                 }
53                 return null;
54         }
55
56         @SuppressWarnings("unchecked")
57         @Override
58         public <A> A newInstance(API api) {
59                 switch(api) {
60                         case IN_REQ: return (A) new InRequest();
61                         case OUT: return (A) new Out();
62                         case ERROR: return (A)new Error();
63                         case ENDPOINTS: return (A) new Endpoints();
64                         case MGMT_ENDPOINTS: return (A) new MgmtEndpoints();
65                         case CONFIG: return (A) new Configuration();
66                         case VOID: return null;
67                 }
68                 return null;
69         }
70
71         //////////////  Mapping Functions /////////////
72         @Override
73         public locate_local.v1_0.Error errorFromMessage(StringBuilder holder, String msgID, String text,String... var) {
74                 Error err = new Error();
75                 err.setMessageId(msgID);
76                 // AT&T Restful Error Format requires numbers "%" placements
77                 err.setText(Vars.convert(holder, text, var));
78                 for(String s : var) {
79                         err.getVariables().add(s);
80                 }
81                 return err;
82         }
83
84         /* (non-Javadoc)
85          * @see org.onap.aaf.auth.locate.mapper.Mapper#endpoints(org.onap.aaf.auth.layer.test.Result, java.lang.String, java.lang.String)
86          */
87         @Override
88         public Result<Endpoints> endpoints(Result<List<Data>> resultDB, String version, String other) {
89                 if(resultDB.notOK()) {
90                         return Result.err(resultDB);
91                 }
92                 int major=-1, minor=-1, patch=-1, pkg=-1;
93                 if(version!=null) {
94                         try { 
95                                 String[] v = Split.split('.',version);
96                                 if(v.length>0) {major = Integer.parseInt(v[0]);}
97                                 if(v.length>1) {minor = Integer.parseInt(v[1]);}
98                                 if(v.length>2) {patch = Integer.parseInt(v[2]);}
99                                 if(v.length>3) {pkg   = Integer.parseInt(v[3]);}
100                         } catch (NumberFormatException e) {
101                                 return Result.err(Result.ERR_BadData,"Invalid Version String " + version);
102                         }
103                 }
104                 Endpoints eps = new Endpoints();
105                 List<Endpoint> leps = eps.getEndpoint();
106                 for(Data d : resultDB.value) {
107                         if((major<0 || major==d.major) &&
108                            (minor<0 || minor<=d.minor) &&
109                            (patch<0 || patch==d.patch) &&
110                            (pkg<0   || pkg  ==d.pkg)) {
111                                 Endpoint ep = new Endpoint();
112                                 ep.setName(d.name);
113                                 ep.setHostname(d.hostname);
114                                 ep.setPort(d.port);
115                                 ep.setMajor(d.major);
116                                 ep.setMinor(d.minor);
117                                 ep.setPatch(d.patch);
118                                 ep.setPkg(d.pkg);
119                                 ep.setLatitude(d.latitude);
120                                 ep.setLongitude(d.longitude);
121                                 ep.setProtocol(d.protocol);
122                                 for(String s : d.subprotocol(false)) {
123                                         ep.getSubprotocol().add(s);
124                                 }
125                                 leps.add(ep);
126                         }
127                 }
128                 return Result.ok(eps);
129         }
130
131         /* (non-Javadoc)
132          * @see org.onap.aaf.auth.locate.mapper.Mapper#locateData(locate.v1_0.MgmtEndpoint)
133          */
134         @Override
135         public Data locateData(MgmtEndpoint me) {
136                 Data data = new Data();
137                 data.name = me.getName();
138                 data.port = me.getPort();
139                 data.hostname = me.getHostname();
140                 data.major = me.getMajor();
141                 data.minor = me.getMinor();
142                 data.patch = me.getPatch();
143                 data.pkg   = me.getPkg();
144                 data.latitude = me.getLatitude();
145                 data.longitude = me.getLongitude();
146                 data.protocol = me.getProtocol();
147                 for(String s : me.getSubprotocol()) {
148                         data.subprotocol(true).add(s);
149                 }
150                 return data;
151         }
152
153 }