ca7f4713886f7b29c4f6500414e8ef9d06a53c69
[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.binding.api.ReadTransaction;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.HttpAuthorization;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.authorization.Policies;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.permission.Permissions;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.permission.Permissions.Actions;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class MdSalAuthorizationStore {
41
42     private static final Logger LOG = LoggerFactory.getLogger(MdSalAuthorizationStore.class.getName());
43
44     private final DataBroker dataBroker;
45
46     public MdSalAuthorizationStore(DataBroker dataBroker) {
47         this.dataBroker = dataBroker;
48     }
49
50     public Optional<OdlPolicy> getPolicy(String path, List<String> userRoles) {
51         InstanceIdentifier<Policies> iif = InstanceIdentifier.create(HttpAuthorization.class).child(Policies.class);
52         Optional<Policies> odata = Optional.empty();
53         // The implicite close is not handled correctly by underlaying opendaylight netconf service
54         ReadTransaction transaction = this.dataBroker.newReadOnlyTransaction();
55         try {
56             odata = transaction.read(LogicalDatastoreType.CONFIGURATION, iif).get();
57         } catch (ExecutionException e) {
58             LOG.warn("unable to read policies from mdsal: ", e);
59         } catch (InterruptedException e) {
60             LOG.warn("Interrupted!", e);
61             // Restore interrupted state...
62             Thread.currentThread().interrupt();
63         }
64         if (odata.isEmpty()) {
65             return Optional.empty();
66         }
67         List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.authorization.policies.Policies> data =
68                 odata.get().getPolicies();
69         if (data == null) {
70             return Optional.empty();
71         }
72         Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.aaa.rev161214.http.authorization.policies.Policies> entry =
73                 data.stream().filter(e -> path.equals(e.getResource())).findFirst();
74         if (entry.isEmpty()) {
75             return Optional.empty();
76         }
77         List<Permissions> permissions = entry.get().getPermissions();
78         if (permissions == null) {
79             return Optional.empty();
80         }
81         Optional<Permissions> rolePm = permissions.stream().filter((e) -> userRoles.contains(e.getRole())).findFirst();
82         if (rolePm.isEmpty()) {
83             return Optional.empty();
84         }
85         return Optional.of(mapPolicy(path, rolePm.get().getActions()));
86     }
87
88     private OdlPolicy mapPolicy(String path, List<Actions> actions) {
89         PolicyMethods methods = new PolicyMethods();
90         String action;
91         for (Actions a : actions) {
92             action = a.getName().toLowerCase();
93             switch (action) {
94                 case "get":
95                     methods.setGet(true);
96                     break;
97                 case "post":
98                     methods.setPost(true);
99                     break;
100                 case "put":
101                     methods.setPut(true);
102                     break;
103                 case "delete":
104                     methods.setDelete(true);
105                     break;
106                 case "patch":
107                     methods.setPatch(true);
108                     break;
109                 default:
110                     LOG.warn("unknown http method {}", action);
111                     break;
112             }
113         }
114         return new OdlPolicy(path, methods);
115     }
116
117 }