e9190f26702bf93e4d1ebeca4cd7055ed7fa3558
[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.DOMActionResult;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
17 import org.opendaylight.yangtools.yang.common.RpcResult;
18 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
19
20 /**
21  * Utility methods for converting {@link RpcResult} to/from {@link DOMActionResult} and {@link DOMRpcResult}.
22  */
23 @NonNullByDefault
24 final class RpcResultUtil {
25     private RpcResultUtil() {
26
27     }
28
29     /**
30      * DOMRpcResult does not have a notion of success, hence we have to reverse-engineer it by looking at reported
31      * errors and checking whether they are just warnings.
32      */
33     static <T> RpcResult<T> rpcResultFromDOM(final Collection<? extends RpcError> errors, final @Nullable T result) {
34         return RpcResultBuilder.<T>status(errors.stream().noneMatch(err -> err.getSeverity() == ErrorSeverity.ERROR))
35                 .withResult(result).withRpcErrors(errors).build();
36     }
37 }