fc0c373c3e960b2bb9b88dc637f78c072454d210
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / main / java / org / opendaylight / mdsal / binding / dom / adapter / RpcResultUtil.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import java.util.Collection;
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.eclipse.jdt.annotation.Nullable;
13 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
14 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18
19 /**
20  * Utility methods for converting {@link RpcResult} to/from {@link DOMRpcResult}.
21  */
22 @NonNullByDefault
23 final class RpcResultUtil {
24     private RpcResultUtil() {
25         // Hidden on purpose
26     }
27
28     /**
29      * DOMRpcResult does not have a notion of success, hence we have to reverse-engineer it by looking at reported
30      * errors and checking whether they are just warnings.
31      */
32     static <T> RpcResult<T> rpcResultFromDOM(final Collection<? extends RpcError> errors, final @Nullable T result) {
33         return RpcResultBuilder.<T>status(errors.stream().noneMatch(err -> err.getSeverity() == ErrorSeverity.ERROR))
34             .withResult(result)
35             .withRpcErrors(errors)
36             .build();
37     }
38 }