Merge "Reduce/enhance logging in AbstractLeader"
[controller.git] / opendaylight / netconf / netconf-testtool / src / main / java / org / opendaylight / controller / netconf / test / tool / FakeModuleBuilderCapability.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.test.tool;
10
11 import com.google.common.base.Optional;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15 import org.opendaylight.controller.netconf.api.Capability;
16 import org.opendaylight.controller.netconf.confignetconfconnector.util.Util;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
19
20 /**
21  * Can be passed instead of ModuleBuilderCapability when building capabilities
22  * in NetconfDeviceSimulator when testing various schema resolution related exceptions.
23  */
24 public class FakeModuleBuilderCapability implements Capability{
25     private static final Date NO_REVISION = new Date(0);
26     private static final List<String> NETCONF = Collections.singletonList("NETCONF");
27     private final ModuleBuilder input;
28     private final Optional<String> content;
29
30     public FakeModuleBuilderCapability(final ModuleBuilder input, final String inputStream) {
31         this.input = input;
32         this.content = Optional.of(inputStream);
33     }
34
35     @Override
36     public String getCapabilityUri() {
37         // FIXME capabilities in Netconf-impl need to check for NO REVISION
38         final String withoutRevision = getModuleNamespace().get() + "?module=" + getModuleName().get();
39         return hasRevision() ? withoutRevision + "&revision=" + Util.writeDate(input.getRevision()) : withoutRevision;
40     }
41
42     @Override
43     public Optional<String> getModuleNamespace() {
44         return Optional.of(input.getNamespace().toString());
45     }
46
47     @Override
48     public Optional<String> getModuleName() {
49         return Optional.of(input.getName());
50     }
51
52     @Override
53     public Optional<String> getRevision() {
54         return Optional.of(hasRevision() ? QName.formattedRevision(input.getRevision()) : "");
55     }
56
57     private boolean hasRevision() {
58         return !input.getRevision().equals(NO_REVISION);
59     }
60
61     /**
62      *
63      * @return empty schema source to trigger schema resolution exception.
64      */
65     @Override
66     public Optional<String> getCapabilitySchema() {
67         return Optional.absent();
68     }
69
70     @Override
71     public List<String> getLocation() {
72         return NETCONF;
73     }
74
75 }