Bug-4827: BGP Add-Path OpenConfig Support II
[bgpcep.git] / bgp / openconfig-impl / src / main / java / org / opendaylight / protocol / bgp / openconfig / impl / moduleconfig / TableTypesFunction.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.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 java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
22 import org.opendaylight.protocol.bgp.openconfig.impl.util.OpenConfigUtil;
23 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.multiprotocol.rev151009.bgp.common.afi.safi.list.AfiSafi;
24 import org.opendaylight.yang.gen.v1.http.openconfig.net.yang.bgp.types.rev151009.AfiSafiType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.BgpTableType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.BgpTableTypeImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.ServiceRef;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.Module;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.modules.ModuleKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.Service;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.ServiceKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.rev130405.services.service.Instance;
33 import org.opendaylight.yangtools.yang.binding.ChildOf;
34
35 final class TableTypesFunction {
36     private TableTypesFunction() {
37         throw new UnsupportedOperationException();
38     }
39
40     public static <T extends ServiceRef & ChildOf<Module>> List<T> getLocalTables(final ReadOnlyTransaction rTx, final BGPConfigModuleProvider
41         configModuleWriter, final Function<String, T> function, final List<AfiSafi> afiSafis) {
42         try {
43             final Optional<Service> maybeService = configModuleWriter.readConfigService(new ServiceKey(BgpTableType.class), rTx);
44             if (maybeService.isPresent()) {
45                 final Service service = maybeService.get();
46                 final List<Optional<Module>> maybeModules = new ArrayList<>();
47                 final Map<String, String> moduleNameToService = new HashMap<>();
48                 for (final Instance instance : service.getInstance()) {
49                     final String moduleName = OpenConfigUtil.getModuleName(instance.getProvider());
50                     final ModuleKey moduleKey = new ModuleKey(moduleName, BgpTableTypeImpl.class);
51                     final Optional<Module> moduleConfig = configModuleWriter.readModuleConfiguration(moduleKey, rTx);
52                     maybeModules.add(moduleConfig);
53                     moduleNameToService.put(moduleName, instance.getName());
54                 }
55
56                 final ImmutableList<Module> modules = FluentIterable.from(maybeModules)
57                     .filter(new Predicate<Optional<Module>>() {
58                         @Override
59                         public boolean apply(final Optional<Module> input) {
60                             return input.isPresent();
61                         }
62                     }).transform(new Function<Optional<Module>, Module>() {
63                         @Override
64                         public Module apply(final Optional<Module> input) {
65                             return input.get();
66                         }
67                     }).toList();
68
69                 return toServices(function, afiSafis, afiSafiToModuleName(afiSafis, modules), moduleNameToService);
70             }
71             throw new IllegalStateException("No BgpTableType service present in configuration.");
72         } catch (final ReadFailedException e) {
73             throw new IllegalStateException("Failed to read service.", e);
74         }
75     }
76
77     public static <T extends ServiceRef & ChildOf<Module>> List<T> toServices(final Function<String, T> function, final List<AfiSafi> afiSafis,
78         final Map<Class<? extends AfiSafiType>, String> afiSafiToModuleName, final Map<String, String> moduleNameToService) {
79         final List<T> serives = new ArrayList<>(afiSafis.size());
80         for (final AfiSafi afiSafi : afiSafis) {
81             final String moduleName = afiSafiToModuleName.get(afiSafi.getAfiSafiName());
82             if (moduleName != null) {
83                 serives.add(function.apply(moduleNameToService.get(moduleName)));
84             }
85         }
86         return serives;
87     }
88
89     private static Map<Class<? extends AfiSafiType>, String> afiSafiToModuleName(final List<AfiSafi> afiSafis, final List<Module> modules) {
90         final Map<Class<? extends AfiSafiType>, String> afiSafiToModuleName = new HashMap<>(afiSafis.size());
91         for (final Module module : modules) {
92             final org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.BgpTableTypeImpl config =
93                 ((org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.bgp.rib.impl.rev160330.modules.module.configuration.BgpTableTypeImpl) module.getConfiguration());
94             final Optional<AfiSafi> afiSafi = OpenConfigUtil.toAfiSafi(new org.opendaylight.protocol.bgp.parser.BgpTableTypeImpl(config.getAfi(), config.getSafi()));
95             if (afiSafi.isPresent()) {
96                 afiSafiToModuleName.put(afiSafi.get().getAfiSafiName(), module.getName());
97             }
98         }
99         return afiSafiToModuleName;
100     }
101 }