a392049a84ffd618ca670ef580566d7964910f3f
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / entityownership / EntityOwnersModel.java
1 /*
2  * Copyright (c) 2015 Brocade Communications 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.controller.cluster.datastore.entityownership;
9
10 import java.util.Map.Entry;
11 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.EntityOwners;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.EntityType;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.Entity;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.clustering.entity.owners.rev150804.entity.owners.entity.type.entity.Candidate;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
21 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedMapNodeBuilder;
28
29 /**
30  * Utility methods for entity-owners yang model.
31  *
32  * @author Thomas Pantelis
33  */
34 public final class EntityOwnersModel {
35     static final QName ENTITY_QNAME = Entity.QNAME;
36     static final QName CANDIDATE_NAME_QNAME = QName.create(Candidate.QNAME, "name");
37     static final QName ENTITY_ID_QNAME = QName.create(ENTITY_QNAME, "id");
38     static final QName ENTITY_OWNER_QNAME = QName.create(ENTITY_QNAME, "owner");
39     static final QName ENTITY_TYPE_QNAME = QName.create(EntityType.QNAME, "type");
40
41     static final NodeIdentifier ENTITY_OWNERS_NODE_ID = new NodeIdentifier(EntityOwners.QNAME);
42     static final NodeIdentifier ENTITY_OWNER_NODE_ID = new NodeIdentifier(ENTITY_OWNER_QNAME);
43     static final NodeIdentifier ENTITY_NODE_ID = new NodeIdentifier(ENTITY_QNAME);
44     static final NodeIdentifier ENTITY_ID_NODE_ID = new NodeIdentifier(ENTITY_ID_QNAME);
45     static final NodeIdentifier ENTITY_TYPE_NODE_ID = new NodeIdentifier(ENTITY_TYPE_QNAME);
46     static final NodeIdentifier CANDIDATE_NODE_ID = new NodeIdentifier(Candidate.QNAME);
47     static final NodeIdentifier CANDIDATE_NAME_NODE_ID = new NodeIdentifier(CANDIDATE_NAME_QNAME);
48     static final YangInstanceIdentifier ENTITY_OWNERS_PATH = YangInstanceIdentifier.of(EntityOwners.QNAME);
49     static final YangInstanceIdentifier ENTITY_TYPES_PATH =
50             YangInstanceIdentifier.of(EntityOwners.QNAME).node(EntityType.QNAME);
51
52     private EntityOwnersModel() {
53     }
54
55     static YangInstanceIdentifier entityPath(String entityType, YangInstanceIdentifier entityId) {
56         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
57                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
58                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).build();
59
60     }
61
62     static YangInstanceIdentifier candidatePath(String entityType, YangInstanceIdentifier entityId,
63             String candidateName) {
64         return YangInstanceIdentifier.builder(ENTITY_OWNERS_PATH).node(EntityType.QNAME)
65                 .nodeWithKey(EntityType.QNAME, ENTITY_TYPE_QNAME, entityType).node(ENTITY_QNAME)
66                         .nodeWithKey(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).node(Candidate.QNAME)
67                                 .nodeWithKey(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
68
69     }
70
71     static YangInstanceIdentifier candidatePath(YangInstanceIdentifier entityPath, String candidateName) {
72         return YangInstanceIdentifier.builder(entityPath).node(Candidate.QNAME).nodeWithKey(
73                 Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName).build();
74     }
75
76     static NodeIdentifierWithPredicates candidateNodeKey(String candidateName) {
77         return new NodeIdentifierWithPredicates(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
78     }
79
80     static NormalizedNode<?, ?> entityOwnersWithCandidate(String entityType, YangInstanceIdentifier entityId,
81             String candidateName) {
82         return entityOwnersWithEntityTypeEntry(entityTypeEntryWithEntityEntry(entityType,
83                 entityEntryWithCandidateEntry(entityId, candidateName)));
84     }
85
86     static ContainerNode entityOwnersWithEntityTypeEntry(MapEntryNode entityTypeNode) {
87         return ImmutableContainerNodeBuilder.create().withNodeIdentifier(
88                 ENTITY_OWNERS_NODE_ID).addChild(ImmutableNodes.mapNodeBuilder(EntityType.QNAME)
89                         .addChild(entityTypeNode).build()).build();
90     }
91
92     static MapEntryNode entityTypeEntryWithEntityEntry(String entityType, MapEntryNode entityNode) {
93         return ImmutableNodes.mapEntryBuilder(EntityType.QNAME,
94                 ENTITY_TYPE_QNAME, entityType).addChild(ImmutableNodes.mapNodeBuilder(
95                         ENTITY_QNAME).addChild(entityNode).build()).build();
96     }
97
98     static MapEntryNode entityEntryWithCandidateEntry(YangInstanceIdentifier entityId, String candidateName) {
99         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
100                 candidateEntry(candidateName)).build();
101     }
102
103     static MapNode candidateEntry(String candidateName) {
104         return ImmutableOrderedMapNodeBuilder.create().withNodeIdentifier(new NodeIdentifier(Candidate.QNAME))
105                 .addChild(candidateMapEntry(candidateName)).build();
106     }
107
108     static MapEntryNode candidateMapEntry(String candidateName) {
109         return ImmutableNodes.mapEntry(Candidate.QNAME, CANDIDATE_NAME_QNAME, candidateName);
110     }
111
112     static MapEntryNode entityEntryWithOwner(YangInstanceIdentifier entityId, String owner) {
113         return ImmutableNodes.mapEntryBuilder(ENTITY_QNAME, ENTITY_ID_QNAME, entityId).addChild(
114                 ImmutableNodes.leafNode(ENTITY_OWNER_QNAME, owner)).build();
115     }
116
117     public static String entityTypeFromEntityPath(YangInstanceIdentifier entityPath) {
118         YangInstanceIdentifier parent = entityPath;
119         while (!parent.isEmpty()) {
120             if (EntityType.QNAME.equals(parent.getLastPathArgument().getNodeType())) {
121                 YangInstanceIdentifier.NodeIdentifierWithPredicates entityTypeLastPathArgument =
122                         (YangInstanceIdentifier.NodeIdentifierWithPredicates) parent.getLastPathArgument();
123                 return (String) entityTypeLastPathArgument.getKeyValues().get(ENTITY_TYPE_QNAME);
124             }
125             parent = parent.getParent();
126         }
127         return null;
128     }
129
130     static DOMEntity createEntity(YangInstanceIdentifier entityPath) {
131         String entityType = null;
132         YangInstanceIdentifier entityId = null;
133         for (PathArgument pathArg: entityPath.getPathArguments()) {
134             if (pathArg instanceof NodeIdentifierWithPredicates) {
135                 NodeIdentifierWithPredicates nodeKey = (NodeIdentifierWithPredicates) pathArg;
136                 Entry<QName, Object> key = nodeKey.getKeyValues().entrySet().iterator().next();
137                 if (ENTITY_TYPE_QNAME.equals(key.getKey())) {
138                     entityType = key.getValue().toString();
139                 } else if (ENTITY_ID_QNAME.equals(key.getKey())) {
140                     entityId = (YangInstanceIdentifier) key.getValue();
141                 }
142             }
143         }
144
145         return new DOMEntity(entityType, entityId);
146     }
147 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.