b181af040462ce4c5917e3bd3ff158cb95a14cf3
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers;
23
24 import java.util.List;
25 import java.util.Optional;
26 import java.util.concurrent.ExecutionException;
27 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.OdlPolicy;
28 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.OdlPolicy.PolicyMethods;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.HttpAuthorization;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.authorization.Policies;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.permission.Permissions;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.permission.Permissions.Actions;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class MdSalAuthorizationStore {
40
41     private static final Logger LOG = LoggerFactory.getLogger(MdSalAuthorizationStore.class.getName());
42
43     private final DataBroker dataBroker;
44
45     public MdSalAuthorizationStore(DataBroker dataBroker) {
46         this.dataBroker = dataBroker;
47     }
48
49     public Optional<OdlPolicy> getPolicy(String path, List<String> userRoles) {
50         InstanceIdentifier<Policies> iif = InstanceIdentifier.create(HttpAuthorization.class).child(Policies.class);
51         Optional<Policies> odata = Optional.empty();
52         try {
53             odata = this.dataBroker.newReadOnlyTransaction().read(LogicalDatastoreType.CONFIGURATION, iif).get();
54         } catch (InterruptedException | ExecutionException e) {
55             LOG.warn("unable to read policies from mdsal: ", e);
56         }
57         if (odata.isEmpty()) {
58             return Optional.empty();
59         }
60
61         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.authorization.policies.Policies> entry =
62                 odata.get().getPolicies().stream().filter((e) -> path.equals(e.getResource())).findFirst();
63         if (entry.isEmpty()) {
64             return Optional.empty();
65         }
66         List<Permissions> permissions = entry.get().getPermissions();
67         if (permissions == null) {
68             return Optional.empty();
69         }
70         Optional<Permissions> rolePm = permissions.stream().filter((e) -> userRoles.contains(e.getRole())).findFirst();
71         if (rolePm.isEmpty()) {
72             return Optional.empty();
73         }
74         return Optional.of(mapPolicy(path, rolePm.get().getActions()));
75     }
76
77     private OdlPolicy mapPolicy(String path, List<Actions> actions) {
78         PolicyMethods methods = new PolicyMethods();
79         String action;
80         for (Actions a : actions) {
81             action = a.getName().toLowerCase();
82             switch (action) {
83                 case "get":
84                     methods.setGet(true);
85                     break;
86                 case "post":
87                     methods.setPost(true);
88                     break;
89                 case "put":
90                     methods.setPut(true);
91                     break;
92                 case "delete":
93                     methods.setDelete(true);
94                     break;
95                 case "patch":
96                     methods.setPatch(true);
97                     break;
98                 default:
99                     LOG.warn("unknown http method {}",action);
100                     break;
101             }
102         }
103         return new OdlPolicy(path, methods);
104     }
105
106 }