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