Use yang-data-codec-binfmt
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / ActionRoutingTable.java
1 /*
2  * Copyright (c) 2019 Nordix Foundation.  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.controller.remote.rpc.registry;
9
10 import akka.actor.ActorRef;
11 import akka.serialization.JavaSerializer;
12 import akka.serialization.Serialization;
13 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
14 import java.io.Externalizable;
15 import java.io.IOException;
16 import java.io.ObjectInput;
17 import java.io.ObjectOutput;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.Set;
22 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeDataOutput;
23 import org.opendaylight.controller.cluster.datastore.node.utils.stream.NormalizedNodeInputOutput;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public final class ActionRoutingTable extends AbstractRoutingTable<ActionRoutingTable, DOMActionInstance> {
32     private static final class Proxy implements Externalizable {
33         private static final long serialVersionUID = 1L;
34         private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
35
36         @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
37         private Collection<DOMActionInstance> actions;
38         private ActorRef opsInvoker;
39
40         // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
41         // be able to create instances via reflection.
42         @SuppressWarnings("checkstyle:RedundantModifier")
43         public Proxy() {
44             // For Externalizable
45         }
46
47         Proxy(final ActionRoutingTable table) {
48             actions = table.getItems();
49             opsInvoker = table.getInvoker();
50         }
51
52         @Override
53         public void writeExternal(final ObjectOutput out) throws IOException {
54             LOG.debug("serializing ActionRoutingTable.");
55             out.writeObject(Serialization.serializedActorPath(opsInvoker));
56
57             final NormalizedNodeDataOutput nnout = NormalizedNodeInputOutput.newDataOutput(out);
58             nnout.writeInt(actions.size());
59             for (DOMActionInstance id : actions) {
60                 nnout.writeSchemaPath(id.getType());
61                 YangInstanceIdentifier actionPath = YangInstanceIdentifier.create(
62                         new YangInstanceIdentifier.NodeIdentifier(id.getType().getLastComponent()));
63                 nnout.writeYangInstanceIdentifier(actionPath);
64             }
65         }
66
67         @Override
68         public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
69             LOG.debug("deserializing ActionRoutingTable");
70             opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
71
72             final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
73             final int size = nnin.readInt();
74             actions = new ArrayList<>(size);
75             for (int i = 0; i < size; ++i) {
76                 actions.add(DOMActionInstance.of(nnin.readSchemaPath(), LogicalDatastoreType.OPERATIONAL,
77                         nnin.readYangInstanceIdentifier()));
78             }
79         }
80
81         private Object readResolve() {
82             return new ActionRoutingTable(opsInvoker, actions);
83         }
84     }
85
86     private static final long serialVersionUID = 1L;
87     private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
88
89     ActionRoutingTable(final ActorRef invoker, final Collection<DOMActionInstance> actions) {
90         super(invoker, actions);
91     }
92
93     ActionRoutingTable updateActions(final Collection<DOMActionInstance> toAdd,
94                                      final Collection<DOMActionInstance> toRemove) {
95         LOG.debug("Updating actions in ActionRoutingTable");
96         final Set<DOMActionInstance> newActions = new HashSet<>(getItems());
97         newActions.addAll(toAdd);
98         newActions.removeAll(toRemove);
99         return new ActionRoutingTable(getInvoker(), newActions);
100     }
101
102     @Override
103     Object writeReplace() {
104         return new Proxy(this);
105     }
106 }