AT&T 2.0.19 Code drop, stage 3
[aaf/authz.git] / auth / auth-locate / src / main / java / org / onap / aaf / auth / locate / service / LocateServiceImpl.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.service;
23
24 import java.util.UUID;
25
26 import org.onap.aaf.auth.dao.cass.LocateDAO;
27 import org.onap.aaf.auth.env.AuthzTrans;
28 import org.onap.aaf.auth.layer.Result;
29 import org.onap.aaf.auth.locate.mapper.Mapper;
30 import org.onap.aaf.auth.locate.validation.LocateValidator;
31 import org.onap.aaf.cadi.aaf.AAFPermission;
32 import org.onap.aaf.misc.env.APIException;
33
34 import locate.v1_0.Endpoints;
35 import locate.v1_0.MgmtEndpoint;
36 import locate.v1_0.MgmtEndpoints;
37
38 public class LocateServiceImpl<IN,OUT,ERROR> 
39           implements LocateService<IN,OUT,Endpoints,MgmtEndpoints,ERROR> {
40                 private Mapper<IN,OUT,Endpoints,MgmtEndpoints,ERROR> mapper;
41                 private LocateDAO locateDAO;
42                 private boolean permToRegister;
43         
44                 public LocateServiceImpl(AuthzTrans trans, LocateDAO locateDAO, Mapper<IN,OUT,Endpoints,MgmtEndpoints,ERROR> mapper) throws APIException {
45                         this.mapper = mapper;
46                         this.locateDAO = locateDAO; 
47                         permToRegister = false; //TODO Setup a Configuration for this
48                 }
49                 
50                 public Mapper<IN,OUT,Endpoints,MgmtEndpoints,ERROR> mapper() {return mapper;}
51
52                 @Override
53                 public Result<Endpoints> getEndPoints(AuthzTrans trans, String service, String version, String other) {
54                         return mapper.endpoints(locateDAO.readByName(trans, service), version, other);
55                 }
56
57                 /* (non-Javadoc)
58                  * @see org.onap.aaf.auth.locate.service.GwService#putMgmtEndPoints(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
59                  */
60                 @Override
61                 public Result<Void> putMgmtEndPoints(AuthzTrans trans, MgmtEndpoints meps) {
62                         LocateValidator v = new LocateValidator().mgmt_endpoints(meps, false);
63                         if(v.err()) {
64                                 return Result.err(Result.ERR_BadData,v.errs());
65                         }
66                         int count = 0;
67                         for(MgmtEndpoint me : meps.getMgmtEndpoint()) {
68                                 if(permToRegister) { 
69                                         int dot = me.getName().lastIndexOf('.'); // Note: Validator checks for NS for getName()
70                                         AAFPermission p = new AAFPermission(me.getName().substring(0,dot)+".locator",me.getName(),"write"); 
71                                         if(trans.fish(p)) {
72                                                 LocateDAO.Data data = mapper.locateData(me);
73                                                 locateDAO.update(trans, data, true);
74                                                 ++count;
75                                         } else {
76                                                 return Result.err(Result.ERR_Denied,"May not register service (needs " + p.getKey() + ')');
77                                         }
78                                 } else { //TODO if(MechID is part of Namespace) { 
79                                         LocateDAO.Data data = mapper.locateData(me);
80                                         locateDAO.update(trans, data, true);
81                                         ++count;
82                                 }
83                         }
84                         if(count>0) {
85                                 return Result.ok();
86                         } else {
87                                 return Result.err(Result.ERR_NotFound, "No endpoints found");
88                         }
89                 }
90
91                 /* (non-Javadoc)
92                  * @see org.onap.aaf.auth.locate.service.GwService#removeMgmtEndPoints(org.onap.aaf.auth.env.test.AuthzTrans, java.lang.Object)
93                  */
94                 @Override
95                 public Result<Void> removeMgmtEndPoints(AuthzTrans trans, MgmtEndpoints meps) {
96                         LocateValidator v = new LocateValidator().mgmt_endpoint_key(meps);
97                         if(v.err()) {
98                                 return Result.err(Result.ERR_BadData,v.errs());
99                         }
100                         int count = 0;
101                         for(MgmtEndpoint me : meps.getMgmtEndpoint()) {
102                                 int dot = me.getName().lastIndexOf('.'); // Note: Validator checks for NS for getName()
103                                 AAFPermission p = new AAFPermission(me.getName().substring(0,dot)+".locator",me.getHostname(),"write"); 
104                                 if(trans.fish(p)) {
105                                         LocateDAO.Data data = mapper.locateData(me);
106                                         data.port_key = UUID.randomUUID();
107                                         locateDAO.delete(trans, data, false);
108                                         ++count;
109                                 } else {
110                                         return Result.err(Result.ERR_Denied,"May not register service (needs " + p.getKey() + ')');
111                                 }
112                         }
113                         if(count>0) {
114                                 return Result.ok();
115                         } else {
116                                 return Result.err(Result.ERR_NotFound, "No endpoints found");
117                         }
118                 }
119
120
121 //////////////// APIs ///////////////////
122 };