2 * Copyright (c) 2019 Nordix Foundation. All rights reserved.
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
8 package org.opendaylight.controller.remote.rpc.registry;
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.InvalidObjectException;
17 import java.io.ObjectInput;
18 import java.io.ObjectOutput;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMActionInstance;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataInput;
27 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeDataOutput;
28 import org.opendaylight.yangtools.yang.data.codec.binfmt.NormalizedNodeStreamVersion;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
34 public final class ActionRoutingTable extends AbstractRoutingTable<ActionRoutingTable, DOMActionInstance> {
35 private static final class Proxy implements Externalizable {
36 private static final long serialVersionUID = 1L;
37 private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
39 @SuppressFBWarnings(value = "SE_BAD_FIELD", justification = "We deal with the field in serialization methods.")
40 private Collection<DOMActionInstance> actions;
41 private ActorRef opsInvoker;
43 // checkstyle flags the public modifier as redundant however it is explicitly needed for Java serialization to
44 // be able to create instances via reflection.
45 @SuppressWarnings("checkstyle:RedundantModifier")
50 Proxy(final ActionRoutingTable table) {
51 actions = table.getItems();
52 opsInvoker = table.getInvoker();
56 public void writeExternal(final ObjectOutput out) throws IOException {
57 LOG.debug("serializing ActionRoutingTable.");
58 out.writeObject(Serialization.serializedActorPath(opsInvoker));
60 final NormalizedNodeDataOutput nnout = NormalizedNodeStreamVersion.current().newDataOutput(out);
61 nnout.writeInt(actions.size());
62 for (DOMActionInstance id : actions) {
63 final Absolute type = id.getType();
64 nnout.writeSchemaNodeIdentifier(type);
65 nnout.writeYangInstanceIdentifier(YangInstanceIdentifier.of(type.lastNodeIdentifier()));
70 public void readExternal(final ObjectInput in) throws IOException, ClassNotFoundException {
71 LOG.debug("deserializing ActionRoutingTable");
72 opsInvoker = JavaSerializer.currentSystem().value().provider().resolveActorRef((String) in.readObject());
74 final NormalizedNodeDataInput nnin = NormalizedNodeDataInput.newDataInput(in);
75 final int size = nnin.readInt();
76 actions = new ArrayList<>(size);
77 for (int i = 0; i < size; ++i) {
78 final SchemaNodeIdentifier sni = nnin.readSchemaNodeIdentifier();
79 if (!(sni instanceof Absolute absolute)) {
80 throw new InvalidObjectException("Non-absolute type " + sni);
83 actions.add(DOMActionInstance.of(absolute, LogicalDatastoreType.OPERATIONAL,
84 nnin.readYangInstanceIdentifier()));
88 private Object readResolve() {
89 return new ActionRoutingTable(opsInvoker, actions);
93 private static final long serialVersionUID = 1L;
94 private static final Logger LOG = LoggerFactory.getLogger(ActionRoutingTable.class);
96 ActionRoutingTable(final ActorRef invoker, final Collection<DOMActionInstance> actions) {
97 super(invoker, actions);
100 ActionRoutingTable updateActions(final Collection<DOMActionInstance> toAdd,
101 final Collection<DOMActionInstance> toRemove) {
102 LOG.debug("Updating actions in ActionRoutingTable");
103 final Set<DOMActionInstance> newActions = new HashSet<>(getItems());
104 newActions.addAll(toAdd);
105 newActions.removeAll(toRemove);
106 return new ActionRoutingTable(getInvoker(), newActions);
110 Object writeReplace() {
111 return new Proxy(this);