Bump MDSAL to 4.0.0
[bgpcep.git] / pcep / topology / topology-provider / src / main / java / org / opendaylight / bgpcep / pcep / topology / provider / OperationResults.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.bgpcep.pcep.topology.provider;
9
10 import com.google.common.base.Function;
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.stream.Collectors;
16 import org.opendaylight.protocol.pcep.spi.PCEPErrors;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcep.error.object.ErrorObjectBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.Errors;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev181109.pcerr.message.pcerr.message.ErrorsBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.FailureType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.OperationResult;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.operation.result.Error;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.topology.pcep.rev181109.operation.result.ErrorBuilder;
24
25 final class OperationResults implements OperationResult {
26     static final OperationResults NOACK = new OperationResults(FailureType.NoAck);
27     static final OperationResults SUCCESS = new OperationResults((FailureType)null);
28     static final OperationResults UNSENT = new OperationResults(FailureType.Unsent);
29
30     private static final Function<Errors, Error> CONVERT_ERRORS = input -> new ErrorBuilder(input).build();
31
32     private final FailureType failure;
33     private final List<Error> error;
34
35     private OperationResults(final FailureType failure) {
36         this.failure = failure;
37         this.error = null;
38     }
39
40     private OperationResults(final FailureType failure, final List<Error> error) {
41         this.failure = failure;
42         this.error = error;
43     }
44
45     ListenableFuture<OperationResult> future() {
46         return Futures.immediateFuture(this);
47     }
48
49     public static OperationResults createFailed(final List<Errors> errors) {
50         final List<Errors> e = errors != null ? errors : Collections.emptyList();
51         return new OperationResults(FailureType.Failed, e.stream().map(CONVERT_ERRORS).collect(Collectors.toList()));
52     }
53
54     public static OperationResults createUnsent(final PCEPErrors error) {
55         final List<Errors> e = error != null ? Collections.singletonList(getErrorFor(error))
56                 : Collections.emptyList();
57         return new OperationResults(FailureType.Unsent, e.stream().map(CONVERT_ERRORS)
58                 .collect(Collectors.toList()));
59     }
60
61     private static Errors getErrorFor(final PCEPErrors error) {
62         final ErrorsBuilder builder = new ErrorsBuilder();
63         builder.setErrorObject(new ErrorObjectBuilder().setType(error.getErrorType())
64                 .setValue(error.getErrorValue()).build());
65         return builder.build();
66     }
67
68     @Override
69     public FailureType getFailure() {
70         return this.failure;
71     }
72
73     @Override
74     public List<Error> getError() {
75         return this.error;
76
77     }
78
79     @Override
80     public Class<OperationResult> implementedInterface() {
81         return OperationResult.class;
82     }
83 }