2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.bgpcep.pcep.topology.provider;
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;
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);
30 private static final Function<Errors, Error> CONVERT_ERRORS = input -> new ErrorBuilder(input).build();
32 private final FailureType failure;
33 private final List<Error> error;
35 private OperationResults(final FailureType failure) {
36 this.failure = failure;
40 private OperationResults(final FailureType failure, final List<Error> error) {
41 this.failure = failure;
45 ListenableFuture<OperationResult> future() {
46 return Futures.immediateFuture(this);
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()));
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()));
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();
69 public FailureType getFailure() {
74 public List<Error> getError() {
80 public Class<OperationResult> implementedInterface() {
81 return OperationResult.class;