Update MRI projects for Aluminium
[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.Collection;
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.rev200120.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     private KeyConstructorUtil() {
20         // Hidden on purpose
21     }
22
23     public static KeyMapping constructKeys(final Collection<MonitoredRouter> mrs) {
24         final KeyMapping ret = KeyMapping.getKeyMapping();
25         if (mrs != null) {
26             mrs.stream().filter(Objects::nonNull).filter(KeyConstructorUtil::isNotNullorEmpty)
27                 .forEach(mr -> {
28                     final Rfc2385Key rfc2385KeyPassword = mr.getPassword();
29                     ret.put(IetfInetUtil.INSTANCE.inetAddressForNoZone(mr.getAddress()),
30                         rfc2385KeyPassword.getValue().getBytes(StandardCharsets.US_ASCII));
31                 });
32         }
33
34         return ret;
35     }
36
37     private static boolean isNotNullorEmpty(final MonitoredRouter mr) {
38         final Rfc2385Key password = mr.getPassword();
39         return password != null && !password.getValue().isEmpty();
40     }
41 }