Bug 591: Minimize the use of Xtend in sal-binding-broker-impl
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / CapabilityProviderImpl.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
9 package org.opendaylight.controller.netconf.impl;
10
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Maps;
14 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
15 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
16 import org.opendaylight.controller.netconf.mapping.api.Capability;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.TreeSet;
27
28 public class CapabilityProviderImpl implements CapabilityProvider {
29     private final NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
30     private final Set<String> capabilityURIs;
31
32     private static final Logger logger = LoggerFactory.getLogger(DefaultCommitNotificationProducer.class);
33
34     public CapabilityProviderImpl(NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
35         this.netconfOperationServiceSnapshot = netconfOperationServiceSnapshot;
36         Map<String, Capability> urisToCapabilitiesInternalMap = getCapabilitiesInternal(netconfOperationServiceSnapshot);
37         List<String> capabilityURIs = new ArrayList<>(urisToCapabilitiesInternalMap.keySet());
38         Collections.sort(capabilityURIs);
39         this.capabilityURIs = Collections.unmodifiableSet(new TreeSet<>(capabilityURIs));
40     }
41
42     private static Map<String, Capability> getCapabilitiesInternal(
43             NetconfOperationServiceSnapshot netconfOperationServiceSnapshot) {
44         Map<String, Capability> capabilityMap = Maps.newHashMap();
45
46         for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
47             final Set<Capability> caps = netconfOperationService.getCapabilities();
48
49             for (Capability cap : caps) {
50
51                 if(capabilityMap.containsKey(cap.getCapabilityUri())) {
52                     logger.debug("Duplicate capability {} from service {}", cap.getCapabilityUri(), netconfOperationService);
53                 }
54
55                 capabilityMap.put(cap.getCapabilityUri(), cap);
56             }
57         }
58
59         return capabilityMap;
60     }
61
62     @Override
63     public synchronized String getSchemaForCapability(String moduleName, Optional<String> revision) {
64
65         Map<String, Map<String, String>> mappedModulesToRevisionToSchema = Maps.newHashMap();
66
67         for (NetconfOperationService netconfOperationService : netconfOperationServiceSnapshot.getServices()) {
68             final Set<Capability> caps = netconfOperationService.getCapabilities();
69
70             for (Capability cap : caps) {
71                 if (cap.getModuleName().isPresent() == false)
72                     continue;
73                 if (cap.getRevision().isPresent() == false)
74                     continue;
75                 if (cap.getCapabilitySchema().isPresent() == false)
76                     continue;
77
78                 final String currentModuleName = cap.getModuleName().get();
79                 Map<String, String> revisionMap = mappedModulesToRevisionToSchema.get(currentModuleName);
80                 if (revisionMap == null) {
81                     revisionMap = Maps.newHashMap();
82                     mappedModulesToRevisionToSchema.put(currentModuleName, revisionMap);
83                 }
84
85                 String currentRevision = cap.getRevision().get();
86                 revisionMap.put(currentRevision, cap.getCapabilitySchema().get());
87             }
88         }
89
90         Map<String, String> revisionMapRequest = mappedModulesToRevisionToSchema.get(moduleName);
91         Preconditions.checkState(revisionMapRequest != null, "Capability for module %s not present, " + ""
92                 + "available modules : %s", moduleName, capabilityURIs);
93
94         if (revision.isPresent()) {
95             String schema = revisionMapRequest.get(revision.get());
96
97             Preconditions.checkState(schema != null,
98                     "Capability for module %s:%s not present, available revisions for module: %s", moduleName,
99                     revision.get(), revisionMapRequest.keySet());
100
101             return schema;
102         } else {
103             Preconditions.checkState(revisionMapRequest.size() == 1,
104                     "Expected 1 capability for module %s, available revisions : %s", moduleName,
105                     revisionMapRequest.keySet());
106             return revisionMapRequest.values().iterator().next();
107         }
108     }
109
110     @Override
111     public synchronized Set<String> getCapabilities() {
112         return capabilityURIs;
113     }
114
115 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.