2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers;
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;
39 public class MdSalAuthorizationStore {
41 private static final Logger LOG = LoggerFactory.getLogger(MdSalAuthorizationStore.class.getName());
43 private final DataBroker dataBroker;
45 public MdSalAuthorizationStore(DataBroker dataBroker) {
46 this.dataBroker = dataBroker;
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();
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);
57 if (odata.isEmpty()) {
58 return Optional.empty();
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();
66 List<Permissions> permissions = entry.get().getPermissions();
67 if (permissions == null) {
68 return Optional.empty();
70 Optional<Permissions> rolePm = permissions.stream().filter((e) -> userRoles.contains(e.getRole())).findFirst();
71 if (rolePm.isEmpty()) {
72 return Optional.empty();
74 return Optional.of(mapPolicy(path, rolePm.get().getActions()));
77 private OdlPolicy mapPolicy(String path, List<Actions> actions) {
78 PolicyMethods methods = new PolicyMethods();
80 for (Actions a : actions) {
81 action = a.getName().toLowerCase();
87 methods.setPost(true);
93 methods.setDelete(true);
96 methods.setPatch(true);
99 LOG.warn("unknown http method {}",action);
103 return new OdlPolicy(path, methods);