Provide Add Path support for all AFI/SAFI
[bgpcep.git] / bmp / bmp-impl / src / main / java / org / opendaylight / protocol / bmp / impl / app / KeyConstructorUtil.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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 package org.opendaylight.protocol.bmp.impl.app;
9
10 import java.nio.charset.StandardCharsets;
11 import java.util.List;
12 import java.util.Objects;
13 import org.opendaylight.protocol.concepts.KeyMapping;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bmp.monitor.config.rev180329.odl.bmp.monitors.bmp.monitor.config.MonitoredRouter;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
17
18 public final class KeyConstructorUtil {
19
20     private KeyConstructorUtil() {
21         throw new UnsupportedOperationException();
22     }
23
24     public static KeyMapping constructKeys(final List<MonitoredRouter> mrs) {
25         final KeyMapping ret = KeyMapping.getKeyMapping();
26         if (mrs != null) {
27             mrs.stream().filter(Objects::nonNull).filter(KeyConstructorUtil::isNotNullorEmpty)
28                 .forEach(mr -> {
29                     final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
30                     ret.put(IetfInetUtil.INSTANCE.inetAddressFor(mr.getAddress()),
31                         rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
32                 });
33         }
34
35         return ret;
36     }
37
38     private static boolean isNotNullorEmpty(final MonitoredRouter mr) {
39         final Rfc2385Key password = mr.getPassword();
40         return password != null && !password.getValue().isEmpty();
41     }
42 }