BUG-731 : removed more sonar warnings
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / ApplicationPeer.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.protocol.bgp.rib.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.net.InetAddresses;
12 import java.util.Arrays;
13 import java.util.Map.Entry;
14 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
15 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
16 import org.opendaylight.protocol.bgp.rib.spi.AbstractAdjRIBs;
17 import org.opendaylight.protocol.bgp.rib.spi.AdjRIBsIn;
18 import org.opendaylight.protocol.bgp.rib.spi.Peer;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.UpdateBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev130919.update.PathAttributesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.BgpTableType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes1Builder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.PathAttributes2Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpUnreachNlriBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.ApplicationRibId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.Route;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.Tables;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.rib.TablesKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.route.Attributes;
34 import org.opendaylight.yangtools.yang.binding.DataObject;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class ApplicationPeer implements AutoCloseable, Peer, DataChangeListener {
40
41     private static final Logger LOG = LoggerFactory.getLogger(ApplicationPeer.class);
42
43     private final byte[] rawIdentifier;
44     private final RIBImpl targetRib;
45     private final String name;
46
47     public ApplicationPeer(final ApplicationRibId applicationRibId, final Ipv4Address ipAddress, final RIBImpl targetRib) {
48         this.name = applicationRibId.getValue().toString();
49         this.targetRib = Preconditions.checkNotNull(targetRib);
50         this.rawIdentifier = InetAddresses.forString(ipAddress.getValue()).getAddress();
51     }
52
53     @Override
54     public void onDataChanged(final AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
55         final UpdateBuilder ub = new UpdateBuilder();
56         for (final Entry<InstanceIdentifier<?>, DataObject> data : change.getCreatedData().entrySet()) {
57             fillMpReach(ub, data);
58         }
59         for (final Entry<InstanceIdentifier<?>, DataObject> data : change.getUpdatedData().entrySet()) {
60             fillMpReach(ub, data);
61         }
62         for (final InstanceIdentifier<?> data : change.getRemovedPaths()) {
63             final MpUnreachNlriBuilder unreachBuilder = new MpUnreachNlriBuilder();
64             final TablesKey key = data.firstKeyOf(Tables.class, TablesKey.class);
65             unreachBuilder.setAfi(key.getAfi());
66             unreachBuilder.setSafi(key.getSafi());
67             final AbstractAdjRIBs<?,?,?> ribsIn = (AbstractAdjRIBs<?,?,?>)this.targetRib.getTable(key);
68             ribsIn.addWith(unreachBuilder, data);
69             ub.setPathAttributes(new PathAttributesBuilder().addAugmentation(PathAttributes2.class, new PathAttributes2Builder().setMpUnreachNlri(unreachBuilder.build()).build()).build());
70             LOG.debug("Updating RIB with {}", ub.build());
71             this.targetRib.updateTables(this, ub.build());
72         }
73
74     }
75
76     private void fillMpReach(final UpdateBuilder ub, final Entry<InstanceIdentifier<?>, DataObject> data) {
77         if (data.getValue() instanceof Route) {
78             final Route r = (Route) data.getValue();
79             final MpReachNlriBuilder reachBuilder = new MpReachNlriBuilder();
80             final TablesKey key = data.getKey().firstKeyOf(Tables.class, TablesKey.class);
81             reachBuilder.setAfi(key.getAfi());
82             reachBuilder.setSafi(key.getSafi());
83             final AdjRIBsIn<?,Route> ribsIn = this.targetRib.getTable(key);
84             ribsIn.addAdvertisement(reachBuilder, (Route)data.getValue());
85             final PathAttributesBuilder pa = new PathAttributesBuilder();
86             pa.addAugmentation(PathAttributes1.class, new PathAttributes1Builder().setMpReachNlri(reachBuilder.build()).build());
87             this.addAttributes(pa, r.getAttributes());
88             pa.setCNextHop(reachBuilder.getCNextHop());
89             ub.setPathAttributes(pa.build());
90             LOG.debug("Updating RIB with {}", ub.build());
91             this.targetRib.updateTables(this, ub.build());
92         }
93     }
94
95     private void addAttributes(final PathAttributesBuilder pa, final Attributes a) {
96         if (a != null) {
97             pa.setAggregator(a.getAggregator());
98             pa.setAsPath(a.getAsPath());
99             pa.setAtomicAggregate(a.getAtomicAggregate());
100             pa.setClusterId(a.getClusterId());
101             pa.setCommunities(a.getCommunities());
102             pa.setExtendedCommunities(a.getExtendedCommunities());
103             pa.setLocalPref(a.getLocalPref());
104             pa.setMultiExitDisc(a.getMultiExitDisc());
105             pa.setOrigin(a.getOrigin());
106             pa.setOriginatorId(a.getOriginatorId());
107         }
108     }
109
110     @Override
111     public String getName() {
112         return this.name;
113     }
114
115     @Override
116     public void close() {
117         for (final BgpTableType t : this.targetRib.getLocalTables()) {
118             this.targetRib.clearTable(this, new TablesKey(t.getAfi(), t.getSafi()));
119         }
120     }
121
122     @Override
123     public byte[] getRawIdentifier() {
124         return Arrays.copyOf(this.rawIdentifier, this.rawIdentifier.length);
125     }
126 }