Create pcep topology parent aggregator
[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 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 = input -> new ErrorBuilder(input).build();
35
36     private final FailureType failure;
37     private final List<Error> error;
38
39     private OperationResults(final FailureType failure) {
40         this.failure = failure;
41         this.error = null;
42     }
43
44     private OperationResults(final FailureType failure, final List<Error> error) {
45         this.failure = failure;
46         this.error = error;
47     }
48
49     ListenableFuture<OperationResult> future() {
50         return Futures.immediateFuture(this);
51     }
52
53     public static OperationResults createFailed(final List<Errors> errors) {
54         final List<Errors> e = errors != null ? errors : Collections.emptyList();
55         return new OperationResults(FailureType.Failed, Lists.transform(e, CONVERT_ERRORS));
56     }
57
58     public static OperationResults createUnsent(final PCEPErrors error) {
59         final List<Errors> e = error != null ? Collections.singletonList(getErrorFor(error)) : Collections.emptyList();
60         return new OperationResults(FailureType.Unsent, Lists.transform(e, CONVERT_ERRORS));
61     }
62
63     private static Errors getErrorFor(final PCEPErrors error) {
64         final ErrorsBuilder builder = new ErrorsBuilder();
65         builder.setErrorObject(new ErrorObjectBuilder().setType(error.getErrorType()).setValue(error.getErrorValue()).build());
66         return builder.build();
67     }
68
69     @Override
70     public FailureType getFailure() {
71         return this.failure;
72     }
73
74     @Override
75     public List<Error> getError() {
76         return this.error;
77
78     }
79     @Override
80     public Class<? extends DataContainer> getImplementedInterface() {
81         return OperationResult.class;
82     }
83 }