Remove yang-test
[controller.git] / opendaylight / config / config-manager-facade-xml / src / main / java / org / opendaylight / controller / config / facade / xml / mapping / attributes / mapping / ObjectNameAttributeMappingStrategy.java
1 /*
2  * Copyright (c) 2015, 2017 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.config.facade.xml.mapping.attributes.mapping;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import javax.management.ObjectName;
14 import javax.management.openmbean.SimpleType;
15 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
16 import org.opendaylight.controller.config.facade.xml.util.Util;
17 import org.opendaylight.yangtools.yang.common.QName;
18
19 public class ObjectNameAttributeMappingStrategy
20         extends AbstractAttributeMappingStrategy<ObjectNameAttributeMappingStrategy.MappedDependency, SimpleType<?>> {
21
22     private final String namespace;
23
24     public ObjectNameAttributeMappingStrategy(final SimpleType<?> openType, final String namespace) {
25         super(openType);
26         this.namespace = namespace;
27     }
28
29     @Override
30     public Optional<MappedDependency> mapAttribute(final Object value) {
31         if (value == null) {
32             return Optional.absent();
33         }
34
35         String expectedClass = getOpenType().getClassName();
36         String realClass = value.getClass().getName();
37         Preconditions.checkArgument(realClass.equals(expectedClass),
38                 "Type mismatch, expected " + expectedClass + " but was " + realClass);
39         Util.checkType(value, ObjectName.class);
40
41         ObjectName on = (ObjectName) value;
42
43         String refName = ObjectNameUtil.getReferenceName(on);
44
45         // we want to use the exact service name that was configured in xml so services
46         // that are referencing it can be resolved
47         return Optional.of(new MappedDependency(namespace,
48                 QName.create(ObjectNameUtil.getServiceQName(on)).getLocalName(), refName));
49     }
50
51     public static class MappedDependency {
52         private final String namespace;
53         private final String serviceName;
54         private final String refName;
55
56         public MappedDependency(final String namespace, final String serviceName, final String refName) {
57             this.serviceName = serviceName;
58             this.refName = refName;
59             this.namespace = namespace;
60         }
61
62         public String getServiceName() {
63             return serviceName;
64         }
65
66         public String getRefName() {
67             return refName;
68         }
69
70         public String getNamespace() {
71             return namespace;
72         }
73
74         @Override
75         public String toString() {
76             return "MappedDependency{"
77                     + "namespace='" + namespace + '\''
78                     + ", serviceName='" + serviceName + '\''
79                     + ", refName='" + refName + '\''
80                     + '}';
81         }
82     }
83 }