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