Bug-4827: BGP Add-Path OpenConfig Support II
[bgpcep.git] / bgp / openconfig-impl / src / main / java / org / opendaylight / protocol / bgp / openconfig / impl / moduleconfig / AddPathFunction.java
1 /*
2  * Copyright (c) 2016 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.protocol.bgp.openconfig.impl.moduleconfig;
10
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Predicate;
14 import com.google.common.collect.FluentIterable;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
23 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
25 import org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil;
26 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
27 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.use.multiple.paths.neighbor.UseMultiplePaths;
28 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.use.multiple.paths.neighbor.use.multiple.paths.Config;
29 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.rev151009.AfiSafi2;
30 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.AddPath;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.AddPathImpl;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.BgpTableType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.BgpTableTypeImpl;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServiceRef;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.Service;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.ServiceKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.service.Instance;
42 import org.opendaylight.yangtools.yang.binding.ChildOf;
43
44 final class AddPathFunction {
45
46     private AddPathFunction() {
47         throw new UnsupportedOperationException();
48     }
49
50     public static <T extends ServiceRef & ChildOf<Module>> List<T> getAddPath(final ReadOnlyTransaction rTx, final BGPConfigModuleProvider
51             configModuleWriter, final Function<String, T> function, final List<AfiSafi> afiSafis) {
52         final ImmutableList<AfiSafi> afiSafisMultipath = FluentIterable.from(afiSafis).filter(new Predicate<AfiSafi>() {
53             @Override
54             public boolean apply(final AfiSafi afisafi) {
55                 final AfiSafi2 afiSafi2 = afisafi.getAugmentation(AfiSafi2.class);
56                 if (afiSafi2 != null) {
57                     final UseMultiplePaths useMultiplePaths = afiSafi2.getUseMultiplePaths();
58                     if (useMultiplePaths != null) {
59                         final Config useMultiplePathsConfig = useMultiplePaths.getConfig();
60                         if (useMultiplePathsConfig != null && useMultiplePathsConfig.isEnabled() != null) {
61                             return useMultiplePathsConfig.isEnabled();
62                         }
63                     }
64                 }
65                 return false;
66             }}).toList();
67         try {
68             final Optional<Service> maybeService = configModuleWriter.readConfigService(new ServiceKey(AddPath.class), rTx);
69             if (maybeService.isPresent()) {
70                 final Service service = maybeService.get();
71                 final List<Module> modules = new ArrayList<>();
72                 final Map<String, String> moduleNameToService = new HashMap<>();
73                 for (final Instance instance : service.getInstance()) {
74                     final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
75                     final ModuleKey moduleKey = new ModuleKey(moduleName, AddPathImpl.class);
76                     final Optional<Module> moduleConfig = configModuleWriter.readModuleConfiguration(moduleKey, rTx);
77                     if (moduleConfig.isPresent()) {
78                         modules.add(moduleConfig.get());
79                     }
80                     moduleNameToService.put(moduleName, instance.getName());
81                 }
82
83                 return TableTypesFunction.toServices(function, afiSafisMultipath, afiSafiToModuleName(afiSafisMultipath, modules, configModuleWriter, rTx), moduleNameToService);
84             }
85             return Collections.emptyList();
86         } catch (final ReadFailedException e) {
87             throw new IllegalStateException(OpenConfigUtil.FAILED_TO_READ_SERVICE, e);
88         }
89     }
90
91     private static Map<Class<? extends AfiSafiType>, String> afiSafiToModuleName(final List<AfiSafi> afiSafis, final List<Module> modules, final BGPConfigModuleProvider configModuleWriter, final ReadTransaction rTx) throws ReadFailedException {
92         final Map<Class<? extends AfiSafiType>, String> afiSafiToModuleName = new HashMap<>(afiSafis.size());
93         for (final Module module : modules) {
94             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.AddPathImpl config =
95                 ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.AddPathImpl) module.getConfiguration());
96             if (config.getSendReceive() == SendReceive.Both) {
97                 final Optional<AfiSafi> tryFind = Iterables.tryFind(afiSafis, new Predicate<AfiSafi>() {
98                     @Override
99                     public boolean apply(final AfiSafi input) {
100                         final Optional<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType> bgpTableType = OpenConfigUtil.toBgpTableType(input.getAfiSafiName());
101                         return bgpTableType.isPresent() && tableTypeExists(configModuleWriter, rTx,
102                                 (String) config.getAddressFamily().getName(), bgpTableType.get());
103                     }
104                 });
105                 if (tryFind.isPresent()) {
106                     afiSafiToModuleName.put(tryFind.get().getAfiSafiName(), module.getName());
107                 }
108             }
109         }
110         return afiSafiToModuleName;
111     }
112
113     public static boolean tableTypeExists(final BGPConfigModuleProvider configModuleWriter, final ReadTransaction rTx,
114             final String instanceName, final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType tableType) {
115         try {
116             final Optional<Service> maybeService = configModuleWriter.readConfigService(new ServiceKey(BgpTableType.class), rTx);
117             if (maybeService.isPresent()) {
118                 for (final Instance instance : maybeService.get().getInstance()) {
119                     final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
120                     if (moduleName.equals(instanceName)) {
121                         final ModuleKey moduleKey = new ModuleKey(moduleName, BgpTableTypeImpl.class);
122                         final Optional<Module> moduleConfig = configModuleWriter.readModuleConfiguration(moduleKey, rTx);
123                         if (moduleConfig.isPresent()) {
124                             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.BgpTableTypeImpl tableTypeImpl = (org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.BgpTableTypeImpl)moduleConfig.get().getConfiguration();
125                             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType moduleTableType = new org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl(tableTypeImpl.getAfi(), tableTypeImpl.getSafi());
126                             if (moduleTableType.equals(tableType)) {
127                                 return true;
128                             }
129                         }
130                     }
131                 }
132             }
133             return false;
134         } catch (final ReadFailedException e) {
135             throw new IllegalStateException(OpenConfigUtil.FAILED_TO_READ_SERVICE, e);
136         }
137     }
138
139 }