Search in sources :

Example 16 with OpStatusMap

use of com.emc.storageos.db.client.model.OpStatusMap in project coprhd-controller by CoprHD.

the class DbClientTest method testOutOfOrderStatusUpdate.

@Test
public void testOutOfOrderStatusUpdate() throws Exception {
    _logger.info("Starting out of order update test");
    DbClient dbClient = _dbClient;
    FileShare fs = new FileShare();
    fs.setId(URIUtil.createId(FileShare.class));
    fs.setLabel("foobar");
    // record ready
    fs.setOpStatus(new OpStatusMap());
    fs.getOpStatus().put("key", new Operation("ready", "xyz"));
    dbClient.persistObject(fs);
    // record pending
    fs = dbClient.queryObject(FileShare.class, fs.getId());
    fs.getOpStatus().put("key", new Operation("pending", "xyz"));
    dbClient.persistObject(fs);
    // verify status is ready
    fs = dbClient.queryObject(FileShare.class, fs.getId());
    Assert.assertEquals(fs.getOpStatus().get("key").getStatus(), "ready");
}
Also used : DbClient(com.emc.storageos.db.client.DbClient) InternalDbClient(com.emc.storageos.db.client.upgrade.InternalDbClient) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) Operation(com.emc.storageos.db.client.model.Operation) FileShare(com.emc.storageos.db.client.model.FileShare) Test(org.junit.Test)

Example 17 with OpStatusMap

use of com.emc.storageos.db.client.model.OpStatusMap in project coprhd-controller by CoprHD.

the class DbClientTest method createDummyFileSharesinDB.

private List<URI> createDummyFileSharesinDB(DbClient dbClient, long size, String label, int count) throws Exception {
    VirtualPool vpool = new VirtualPool();
    vpool.setId(URIUtil.createId(VirtualPool.class));
    vpool.setLabel("GOLD");
    vpool.setType("file");
    vpool.setProtocols(new StringSet());
    dbClient.persistObject(vpool);
    List<URI> fsIds = new ArrayList<URI>();
    for (int i = 0; i < count; i++) {
        FileShare fs = new FileShare();
        fs.setId(URIUtil.createId(FileShare.class));
        if (label == null) {
            fs.setLabel("fileshare" + System.nanoTime());
        } else {
            fs.setLabel(label);
        }
        fs.setCapacity(size);
        fs.setVirtualPool(vpool.getId());
        fs.setOpStatus(new OpStatusMap());
        Operation op = new Operation();
        op.setStatus(Operation.Status.pending.name());
        fs.getOpStatus().put("filesharereq", op);
        dbClient.persistObject(fs);
        fsIds.add(fs.getId());
    }
    return fsIds;
}
Also used : StringSet(com.emc.storageos.db.client.model.StringSet) ArrayList(java.util.ArrayList) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) VirtualPool(com.emc.storageos.db.client.model.VirtualPool) Operation(com.emc.storageos.db.client.model.Operation) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) FileShare(com.emc.storageos.db.client.model.FileShare) PrefixConstraint(com.emc.storageos.db.client.constraint.PrefixConstraint) AlternateIdConstraint(com.emc.storageos.db.client.constraint.AlternateIdConstraint) ContainmentPrefixConstraint(com.emc.storageos.db.client.constraint.ContainmentPrefixConstraint) AggregatedConstraint(com.emc.storageos.db.client.constraint.AggregatedConstraint) ContainmentPermissionsConstraint(com.emc.storageos.db.client.constraint.ContainmentPermissionsConstraint) Constraint(com.emc.storageos.db.client.constraint.Constraint) ContainmentConstraint(com.emc.storageos.db.client.constraint.ContainmentConstraint)

Example 18 with OpStatusMap

use of com.emc.storageos.db.client.model.OpStatusMap in project coprhd-controller by CoprHD.

the class DbClientImpl method setStatus.

@Override
@Deprecated
public void setStatus(Class<? extends DataObject> clazz, URI id, String opId, String status, String message) {
    tracer.newTracer("write");
    try {
        DataObject doobj = clazz.newInstance();
        doobj.setId(id);
        doobj.setOpStatus(new OpStatusMap());
        Operation op = new Operation();
        op.setStatus(status);
        if (message != null) {
            op.setMessage(message);
        }
        doobj.getOpStatus().put(opId, op);
        persistObject(doobj);
    } catch (InstantiationException e) {
        throw new IllegalStateException(e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}
Also used : PropertyListDataObject(com.emc.storageos.db.client.model.PropertyListDataObject) DataObject(com.emc.storageos.db.client.model.DataObject) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) Operation(com.emc.storageos.db.client.model.Operation)

Example 19 with OpStatusMap

use of com.emc.storageos.db.client.model.OpStatusMap in project coprhd-controller by CoprHD.

the class DbClientImpl method serializeTasks.

private void serializeTasks(DataObject dataObject, RowMutator mutator, List<URI> objectsToCleanup) {
    OpStatusMap statusMap = dataObject.getOpStatus();
    if (statusMap == null || statusMap.getChangedKeySet() == null || statusMap.getChangedKeySet().isEmpty()) {
        return;
    }
    Set<String> addedSet = statusMap.getChangedKeySet();
    if (addedSet != null) {
        DataObjectType taskDoType = TypeMap.getDoType(Task.class);
        Iterator<String> it = statusMap.getChangedKeySet().iterator();
        while (it.hasNext()) {
            String requestId = it.next();
            Operation operation = statusMap.get(requestId);
            Task task = TaskUtils.findTaskForRequestId(this, dataObject.getId(), requestId);
            if (task == null) {
                // Task doesn't currently exist for this id, so create it
                task = new Task();
                task.setId(URIUtil.createId(Task.class));
                task.setRequestId(requestId);
                task.setInactive(false);
                task.setServiceCode(operation.getServiceCode());
                task.setLabel(operation.getName());
                task.setStatus(operation.getStatus());
                task.setDescription(operation.getDescription());
                Integer progress = operation.getProgress();
                task.setProgress(progress != null ? progress : 0);
                task.setMessage(operation.getMessage());
                task.setAssociatedResources(operation.rawAssociatedResources());
                task.setCreationTime(Calendar.getInstance());
                task.setInactive(false);
                task.setStartTime(operation.getStartTime());
                task.setEndTime(getEndTime(operation));
                // Often dummy objects are used that just contain an ID, for some things we need access to the entire object
                DataObject loadedObject = dataObject;
                if (StringUtils.isBlank(dataObject.getLabel())) {
                    loadedObject = this.queryObject(URIUtil.getModelClass(dataObject.getId()), dataObject.getId());
                }
                if (loadedObject == null) {
                    throw new RuntimeException("Task created on a resource which doesn't exist " + dataObject.getId());
                }
                task.setResource(new NamedURI(loadedObject.getId(), loadedObject.getLabel()));
                URI tenantId = getTenantURI(loadedObject);
                if (tenantId == null) {
                    task.setTenant(TenantOrg.SYSTEM_TENANT);
                } else {
                    task.setTenant(tenantId);
                }
                _log.info("Created task {}, {}", task.getId() + " (" + task.getRequestId() + ")", task.getLabel());
            } else {
                // Task exists so update it
                task.setServiceCode(operation.getServiceCode());
                task.setStatus(operation.getStatus());
                task.setMessage(operation.getMessage());
                // Some code isn't updating progress to 100 when completed, so fix this here
                if (Objects.equal(task.getStatus(), "pending") || Objects.equal(task.getStatus(), "suspended_no_error") || Objects.equal(task.getStatus(), "suspended_error")) {
                    task.setProgress(operation.getProgress());
                } else {
                    task.setProgress(COMPLETED_PROGRESS);
                }
                task.setStartTime(operation.getStartTime());
                task.setEndTime(getEndTime(operation));
                task.setAssociatedResources(operation.rawAssociatedResources());
                if (!Objects.equal(task.getStatus(), "pending")) {
                    _log.info("Completed task {}, {}", task.getId() + " (" + task.getRequestId() + ")", task.getStatus());
                }
            }
            if (taskDoType.serialize(mutator, task)) {
                objectsToCleanup.add(task.getId());
            }
            operation.addTask(dataObject.getId(), task);
        }
    }
}
Also used : Task(com.emc.storageos.db.client.model.Task) PropertyListDataObject(com.emc.storageos.db.client.model.PropertyListDataObject) DataObject(com.emc.storageos.db.client.model.DataObject) NamedURI(com.emc.storageos.db.client.model.NamedURI) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) Operation(com.emc.storageos.db.client.model.Operation) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI)

Example 20 with OpStatusMap

use of com.emc.storageos.db.client.model.OpStatusMap in project coprhd-controller by CoprHD.

the class DbCli method readXMLAndPersist.

/**
 * Load xml file and save model object into Cassandra.
 *
 * @Param fileName
 */
private <T extends DataObject> void readXMLAndPersist(String fileName, DbCliOperation operation) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = dbf.newDocumentBuilder();
    Document doc = builder.parse(fileName);
    // Read root node
    Element root = doc.getDocumentElement();
    Element dataObjectNode = (Element) root.getElementsByTagName("data_object_schema").item(0);
    // Get column family's name
    String cfName = dataObjectNode.getAttribute("name");
    System.out.println("Column Family based on XML: " + cfName);
    NodeList recordNodes = dataObjectNode.getElementsByTagName("record");
    Class<T> clazz = _cfMap.get(cfName);
    if (clazz == null) {
        System.out.println("Unknown Column Family: " + cfName);
        return;
    }
    // Get class info
    BeanInfo bInfo;
    try {
        bInfo = Introspector.getBeanInfo(clazz);
    } catch (IntrospectionException ex) {
        throw new RuntimeException("Unexpected exception getting bean info", ex);
    }
    PropertyDescriptor[] pds = bInfo.getPropertyDescriptors();
    // get position of xml node
    InputStream xmlIs = new FileInputStream(new File(fileName));
    Document docForPosition = PositionalXMLReader.readXML(xmlIs);
    xmlIs.close();
    for (int i = 0; i < recordNodes.getLength(); i++) {
        Element record = (Element) recordNodes.item(i);
        T object = null;
        String idStr = null;
        if (operation == DbCliOperation.LOAD) {
            // query record based id
            String recordId = record.getAttribute("id");
            System.out.println(String.format("Object id:\t%s", recordId));
            idStr = recordId;
            object = queryObject(URI.create(recordId), clazz);
        } else if (operation == DbCliOperation.CREATE) {
            // create new id for create record
            URI id = URIUtil.createId(clazz);
            object = clazz.newInstance();
            object.setId(id);
            System.out.println(String.format("Create new data object id:\t%s", object.getId()));
            idStr = object.getId().toString();
        }
        HashMap<String, String> fieldValueMap = new HashMap<String, String>();
        HashMap<String, Class> fieldTypeMap = new HashMap<String, Class>();
        HashMap<String, String> fieldLocationMap = new HashMap<String, String>();
        HashMap<String, Node> fieldNodeMap = new HashMap<String, Node>();
        NodeList fields = record.getElementsByTagName("field");
        // get field info from xml file
        for (int j = 0; j < fields.getLength(); j++) {
            Element field = (Element) fields.item(j);
            if (DEBUG) {
                System.out.println(field.getAttribute("name") + "\t" + field.getAttribute("type") + "\t" + field.getAttribute("value"));
            }
            fieldValueMap.put(field.getAttribute("name"), field.getAttribute("value"));
            fieldTypeMap.put(field.getAttribute("name"), Class.forName(field.getAttribute("type")));
            fieldLocationMap.put(field.getAttribute("name"), ((Element) docForPosition.getElementsByTagName("record").item(i)).getElementsByTagName("field").item(j).getUserData("lineNumber").toString());
            if (field.getElementsByTagName("wrapper").item(0) != null) {
                fieldNodeMap.put(field.getAttribute("name"), field.getElementsByTagName("wrapper").item(0));
            }
        }
        Iterator locationIt = fieldLocationMap.entrySet().iterator();
        while (locationIt.hasNext()) {
            Entry entry = (Entry) locationIt.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            if (DEBUG) {
                System.out.println("key:\t" + key + "\tvalue\t" + value);
            }
        }
        // update object's fields
        for (PropertyDescriptor pd : pds) {
            // skip class property, id property
            if (pd.getName().equals("class") || pd.getName().equals("id")) {
                continue;
            }
            Name name = pd.getReadMethod().getAnnotation(Name.class);
            if (name == null) {
                log.info("Ignore data object fields without @Name annotation, fieldName={}.", pd.getName());
                continue;
            }
            String objKey = name.value();
            String fieldValue = fieldValueMap.get(objKey);
            if (fieldValue == null) {
                // To support xml file that the old version dumped, it used method name not @Name value
                objKey = pd.getName();
            }
            fieldValue = fieldValueMap.get(objKey);
            Class fieldClass = fieldTypeMap.get(objKey);
            String fieldLocation = fieldLocationMap.get(objKey);
            Node fieldNode = fieldNodeMap.get(objKey);
            if (fieldValue != null) {
                Class type = pd.getPropertyType();
                if (DEBUG) {
                    System.out.print("\t" + objKey + " = " + type);
                }
                try {
                    if (type == URI.class) {
                        pd.getWriteMethod().invoke(object, URI.create(fieldValue));
                    } else if (type == NamedURI.class) {
                        pd.getWriteMethod().invoke(object, NamedURI.fromString(fieldValue));
                    } else if (type == Date.class) {
                    // Can not find records with value which owns this type. Remains to be verified correct or not.
                    // System.out.println("\ttype: Date ");
                    } else if (type == Calendar.class) {
                        Calendar calendar = FieldType.toCalendar(fieldValue);
                        if (!verifyField(calendar)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, calendar);
                    } else if (type == StringMap.class) {
                        StringMap newStringMap = FieldType.convertType(fieldNode, StringMapWrapper.class);
                        if (!verifyField(newStringMap)) {
                            throw new Exception("field format exception");
                        }
                        StringMap sMap = (StringMap) pd.getReadMethod().invoke(object);
                        if (sMap == null) {
                            sMap = new StringMap();
                        }
                        sMap.clear();
                        Set<String> keys = newStringMap.keySet();
                        for (String key : keys) {
                            sMap.put(key, newStringMap.get(key));
                        }
                        pd.getWriteMethod().invoke(object, sMap);
                    } else if (type == StringSet.class) {
                        StringSet stringSet = FieldType.convertType(fieldNode, StringSetWrapper.class);
                        if (!verifyField(stringSet)) {
                            throw new Exception("field format exception");
                        }
                        StringSet updateSet = (StringSet) pd.getReadMethod().invoke(object);
                        if (updateSet != null) {
                            updateSet.clear();
                            updateSet.addAll(stringSet);
                        } else {
                            pd.getWriteMethod().invoke(object, stringSet);
                        }
                    } else if (type == OpStatusMap.class) {
                        OpStatusMap opStatusMap = FieldType.convertType(fieldNode, OpStatusMapWrapper.class);
                        if (!verifyField(opStatusMap)) {
                            throw new Exception("field format exception");
                        }
                    } else if (type == StringSetMap.class) {
                        StringSetMap newSetMap = FieldType.convertType(fieldNode, StringSetMapWrapper.class);
                        if (!verifyField(newSetMap)) {
                            throw new Exception("field format exception");
                        }
                        StringSetMap sMap = (StringSetMap) pd.getReadMethod().invoke(object);
                        if (sMap == null) {
                            sMap = new StringSetMap();
                        }
                        Set<String> keys = sMap.keySet();
                        for (String key : keys) {
                            sMap.remove(key);
                        }
                        keys = newSetMap.keySet();
                        for (String key : keys) {
                            sMap.put(key, newSetMap.get(key));
                        }
                    } else if (type == FSExportMap.class) {
                        FSExportMap fSExportMap = FieldType.convertType(fieldNode, FSExportMapWrapper.class);
                        if (!verifyField(fSExportMap)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, fSExportMap);
                    } else if (type == SMBShareMap.class) {
                        SMBShareMap sMBShareMap = FieldType.convertType(fieldNode, SMBShareMapWrapper.class);
                        if (!verifyField(sMBShareMap)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, sMBShareMap);
                    } else if (type == ScopedLabelSet.class) {
                        ScopedLabelSet scopedLabelSet = FieldType.convertType(fieldNode, ScopedLabelSetWrapper.class);
                        if (!verifyField(scopedLabelSet)) {
                            throw new Exception("field format exception");
                        }
                        ScopedLabelSet updateSet = (ScopedLabelSet) pd.getReadMethod().invoke(object);
                        if (updateSet != null) {
                            updateSet.clear();
                            updateSet.addAll(scopedLabelSet);
                        } else {
                            pd.getWriteMethod().invoke(object, scopedLabelSet);
                        }
                    } else if (type == String.class) {
                        pd.getWriteMethod().invoke(object, fieldClass.cast(fieldValue));
                    } else if (type.isEnum()) {
                        Object enumTypeObject = null;
                        try {
                            enumTypeObject = Enum.valueOf(type, fieldValue);
                        } catch (Exception e) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, enumTypeObject);
                    } else if (type == Integer.class) {
                        Integer intNum = FieldType.toInteger(fieldValue);
                        if (!verifyField(intNum)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, intNum);
                    } else if (type == Boolean.class) {
                        Boolean boolVal = FieldType.toBoolean(fieldValue);
                        if (!verifyField(boolVal)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, boolVal);
                    } else if (type == Long.class) {
                        Long longNum = FieldType.toLong(fieldValue);
                        if (!verifyField(longNum)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, longNum);
                    } else if (type == Short.class) {
                        Short shortNum = FieldType.toShort(fieldValue);
                        if (!verifyField(shortNum)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, shortNum);
                    } else if (type == Double.class) {
                        Double doubleNum = FieldType.toDouble(fieldValue);
                        if (!verifyField(doubleNum)) {
                            throw new Exception("field format exception");
                        }
                        pd.getWriteMethod().invoke(object, doubleNum);
                    } else {
                        pd.getWriteMethod().invoke(object, fieldValue);
                    }
                } catch (Exception e) {
                    System.out.println(String.format("Exception in getting field:%s in xml file line:%s.", pd.getName(), fieldLocation));
                    log.error("Exception in getting field value in xml file line:{}.", fieldLocation, e);
                    throw new Exception(String.format("Exception in getting field value in line:%s.", fieldLocation));
                }
                if (DEBUG) {
                    Object fieldValue1 = pd.getReadMethod().invoke(object);
                    System.out.println("write " + fieldValue1 + "\ttype: " + type + " success");
                }
            }
        }
        if (operation == DbCliOperation.CREATE) {
            // Save model object.
            _dbClient.createObject(object);
        } else if (operation == DbCliOperation.LOAD) {
            _dbClient.persistObject(object);
        }
        log.info(String.format("Successfully update Column family:%s, \tdata object id:%s \tinto Cassandra, based on xml file %s", cfName, idStr, fileName));
    }
}
Also used : StringMap(com.emc.storageos.db.client.model.StringMap) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Set(java.util.Set) ScopedLabelSet(com.emc.storageos.db.client.model.ScopedLabelSet) StringSet(com.emc.storageos.db.client.model.StringSet) HashMap(java.util.HashMap) NamedURI(com.emc.storageos.db.client.model.NamedURI) Element(org.w3c.dom.Element) BeanInfo(java.beans.BeanInfo) Node(org.w3c.dom.Node) IntrospectionException(java.beans.IntrospectionException) OpStatusMap(com.emc.storageos.db.client.model.OpStatusMap) StringSetMapWrapper(com.emc.storageos.dbcli.wrapper.StringSetMapWrapper) Document(org.w3c.dom.Document) FSExportMap(com.emc.storageos.db.client.model.FSExportMap) NamedURI(com.emc.storageos.db.client.model.NamedURI) URI(java.net.URI) Name(com.emc.storageos.db.client.model.Name) Entry(java.util.Map.Entry) Iterator(java.util.Iterator) StringSet(com.emc.storageos.db.client.model.StringSet) StringSetWrapper(com.emc.storageos.dbcli.wrapper.StringSetWrapper) SMBShareMapWrapper(com.emc.storageos.dbcli.wrapper.SMBShareMapWrapper) StringSetMap(com.emc.storageos.db.client.model.StringSetMap) SMBShareMap(com.emc.storageos.db.client.model.SMBShareMap) PropertyDescriptor(java.beans.PropertyDescriptor) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Calendar(java.util.Calendar) FileInputStream(java.io.FileInputStream) DatabaseException(com.emc.storageos.db.exceptions.DatabaseException) IntrospectionException(java.beans.IntrospectionException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ScopedLabelSet(com.emc.storageos.db.client.model.ScopedLabelSet) DocumentBuilder(javax.xml.parsers.DocumentBuilder) DataObject(com.emc.storageos.db.client.model.DataObject) File(java.io.File)

Aggregations

OpStatusMap (com.emc.storageos.db.client.model.OpStatusMap)48 Operation (com.emc.storageos.db.client.model.Operation)29 NamedURI (com.emc.storageos.db.client.model.NamedURI)28 Volume (com.emc.storageos.db.client.model.Volume)15 URI (java.net.URI)15 StorageSystem (com.emc.storageos.db.client.model.StorageSystem)13 StringSet (com.emc.storageos.db.client.model.StringSet)13 FileShare (com.emc.storageos.db.client.model.FileShare)12 ArrayList (java.util.ArrayList)10 SMBFileShare (com.emc.storageos.db.client.model.SMBFileShare)9 StringMap (com.emc.storageos.db.client.model.StringMap)9 HashMap (java.util.HashMap)9 StoragePool (com.emc.storageos.db.client.model.StoragePool)8 InternalException (com.emc.storageos.svcs.errorhandling.resources.InternalException)7 MapFileShare (com.emc.storageos.api.mapper.functions.MapFileShare)6 BlockSnapshot (com.emc.storageos.db.client.model.BlockSnapshot)6 DataObject (com.emc.storageos.db.client.model.DataObject)6 CheckPermission (com.emc.storageos.security.authorization.CheckPermission)6 Consumes (javax.ws.rs.Consumes)6 AlternateIdConstraint (com.emc.storageos.db.client.constraint.AlternateIdConstraint)5